Dado un entero D , la tarea es encontrar todos los números palíndromos de dígitos D. Ejemplos:
Entrada: D = 1
Salida: 1 2 3 4 5 6 7 8 9
Entrada: D = 2
Salida: 11 22 33 44 55 66 77 88 99
Enfoque: los números con dígitos D comienzan desde 10 (D – 1) hasta 10 D – 1 . Entonces, comience a verificar cada número de este intervalo, ya sea palíndromo o no.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the // reverse of num int reverse(int num) { int rev = 0; while (num > 0) { rev = rev * 10 + num % 10; num = num / 10; } return rev; } // Function that returns true // if num is palindrome bool isPalindrome(int num) { // If the number is equal to the // reverse of it then it // is a palindrome if (num == reverse(num)) return true; return false; } // Function to print all the // d-digit palindrome numbers void printPalindromes(int d) { if (d <= 0) return; // Smallest and the largest d-digit numbers int smallest = pow(10, d - 1); int largest = pow(10, d) - 1; // Starting from the smallest d-digit // number till the largest for (int i = smallest; i <= largest; i++) { // If the current number // is palindrome if (isPalindrome(i)) cout << i << " "; } } // Driver code int main() { int d = 2; printPalindromes(d); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the // reverse of num static int reverse(int num) { int rev = 0; while (num > 0) { rev = rev * 10 + num % 10; num = num / 10; } return rev; } // Function that returns true // if num is palindrome static boolean isPalindrome(int num) { // If the number is equal to the // reverse of it then it // is a palindrome if (num == reverse(num)) return true; return false; } // Function to print all the // d-digit palindrome numbers static void printPalindromes(int d) { if (d <= 0) return; // Smallest and the largest d-digit numbers int smallest = (int)Math.pow(10, d - 1); int largest = (int)Math.pow(10, d) - 1; // Starting from the smallest d-digit // number till the largest for (int i = smallest; i <= largest; i++) { // If the current number // is palindrome if (isPalindrome(i)) System.out.print(i + " "); } } // Driver code public static void main (String[] args) { int d = 2; printPalindromes(d); } } // This code is contributed by AnkitRai01
Python3
# Python implementation of the approach # Function to return the # reverse of num def reverse(num): rev = 0; while (num > 0): rev = rev * 10 + num % 10; num = num // 10; return rev; # Function that returns true # if num is palindrome def isPalindrome(num): # If the number is equal to the # reverse of it then it # is a palindrome if (num == reverse(num)): return True; return False; # Function to print all the # d-digit palindrome numbers def printPalindromes(d): if (d <= 0): return; # Smallest and the largest d-digit numbers smallest = pow(10, d - 1); largest = pow(10, d) - 1; # Starting from the smallest d-digit # number till the largest for i in range(smallest, largest + 1): # If the current number # is palindrome if (isPalindrome(i)): print(i, end = " "); # Driver code d = 2; printPalindromes(d); # This code is contributed by 29AjayKumar
C#
// C# implementation of the approach using System; class GFG { // Function to return the // reverse of num static int reverse(int num) { int rev = 0; while (num > 0) { rev = rev * 10 + num % 10; num = num / 10; } return rev; } // Function that returns true // if num is palindrome static bool isPalindrome(int num) { // If the number is equal to the // reverse of it then it // is a palindrome if (num == reverse(num)) return true; return false; } // Function to print all the // d-digit palindrome numbers static void printPalindromes(int d) { if (d <= 0) return; // Smallest and the largest d-digit numbers int smallest = (int)Math.Pow(10, d - 1); int largest = (int)Math.Pow(10, d) - 1; // Starting from the smallest d-digit // number till the largest for (int i = smallest; i <= largest; i++) { // If the current number // is palindrome if (isPalindrome(i)) Console.Write(i + " "); } } // Driver code public static void Main (String[] args) { int d = 2; printPalindromes(d); } } // This code is contributed by Rajput-Ji
Javascript
<script> // JavaScript implementation of the approach // Function to return the // reverse of num function reverse(num) { let rev = 0; while (num > 0) { rev = rev * 10 + num % 10; num = parseInt(num / 10, 10); } return rev; } // Function that returns true // if num is palindrome function isPalindrome(num) { // If the number is equal to the // reverse of it then it // is a palindrome if (num == reverse(num)) return true; return false; } // Function to print all the // d-digit palindrome numbers function printPalindromes(d) { if (d <= 0) return; // Smallest and the largest d-digit numbers let smallest = Math.pow(10, d - 1); let largest = Math.pow(10, d) - 1; // Starting from the smallest d-digit // number till the largest for (let i = smallest; i <= largest; i++) { // If the current number // is palindrome if (isPalindrome(i)) document.write(i + " "); } } let d = 2; printPalindromes(d); </script>
Producción:
11 22 33 44 55 66 77 88 99
Complejidad del tiempo: O(d)
Espacio Auxiliar: O(1)