Dado un número entero N . La tarea es encontrar el XOR y el OR de todos los números palindrómicos de N dígitos.
Ejemplos
Entrada: 3
Salida: XOR = 714 y OR = 1023
Entrada: 4
Salida: XOR = 4606 y OR = 16383
Acercarse:
- Encuentre el número inicial y final del número palindrómico de N dígitos por:
starting number = pow(10, n - 1) ending number = pow(10, n) - 1
- Itere sobre el número inicial hasta el número final y verifique si ese número es palindrómico o no.
- Si el número es palindrómico, entonces toma XOR y OR de ese número por separado.
- De lo contrario, continúe con la siguiente iteración e imprima el valor de XOR y OR después de todas las iteraciones.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the XOR // and OR of all palindrome numbers // of N digits #include <bits/stdc++.h> using namespace std; // Function to check if a number // is palindrome or not bool ispalin(int num) { // Convert the num n to string string s = to_string(num); int st = 0, ed = s.size() - 1; // Iterate over string to // check whether it is // palindromic or not while (st <= ed) { if (s[st] != s[ed]) return false; st++; ed--; } return true; } // Function to find XOR of all // N-digits palindrome number void CalculateXORandOR(int n) { // To store the XOR and OR of all // palindromic number int CalculateXOR = 0; int CalculateOR = 0; // Starting N-digit // palindromic number int start = pow(10, n - 1); // Ending N-digit // palindromic number int end = pow(10, n) - 1; // Iterate over starting and /// ending number for (int i = start; i <= end; i++) { // To check if i is // palindromic or not if (ispalin(i)) { CalculateXOR = CalculateXOR ^ i; CalculateOR = CalculateOR | i; } } // Print the XOR and OR of all // palindromic number cout << "XOR = " << CalculateXOR; cout << " OR = " << CalculateOR; } // Driver Code int main() { int n = 4; CalculateXORandOR(n); return 0; }
Java
// Java program to find the XOR // and OR of all palindrome numbers // of N digits class GFG { // Function to check if a number // is palindrome or not static boolean ispalin(int num) { // Convert the num n to string String s = Integer.toString(num); int st = 0, ed = s.length() - 1; // Iterate over string to // check whether it is // palindromic or not while (st <= ed) { if (s.charAt(st) != s.charAt(ed)) return false; st++; ed--; } return true; } // Function to find XOR of all // N-digits palindrome number static void CalculateXORandOR(int n) { // To store the XOR and OR of all // palindromic number int CalculateXOR = 0; int CalculateOR = 0; // Starting N-digit // palindromic number int start = (int)Math.pow(10, n - 1); // Ending N-digit // palindromic number int end = (int)Math.pow(10, n) - 1; // Iterate over starting and /// ending number for (int i = start; i <= end; i++) { // To check if i is // palindromic or not if (ispalin(i)) { CalculateXOR = CalculateXOR ^ i; CalculateOR = CalculateOR | i; } } // Print the XOR and OR of all // palindromic number System.out.print("XOR = " + CalculateXOR); System.out.println(" OR = " + CalculateOR); } // Driver Code public static void main (String[] args) { int n = 4; CalculateXORandOR(n); } } // This code is contributed by AnkitRai01
Python3
# Python3 program to find the XOR # and OR of all palindrome numbers # of N digits # Function to check if a number # is palindrome or not def ispalin(num) : # Convert the num n to string s = str(num); st = 0; ed = len(s) - 1; # Iterate over string to # check whether it is # palindromic or not while (st <= ed) : if (s[st] != s[ed]) : return False; st += 1; ed -= 1; return True; # Function to find XOR of all # N-digits palindrome number def CalculateXORandOR(n) : # To store the XOR and OR of all # palindromic number CalculateXOR = 0; CalculateOR = 0; # Starting N-digit # palindromic number start = 10 ** (n - 1); # Ending N-digit # palindromic number end = (10**n) - 1; # Iterate over starting and # ending number for i in range( start, end + 1) : # To check if i is # palindromic or not if (ispalin(i)) : CalculateXOR = CalculateXOR ^ i; CalculateOR = CalculateOR | i; # Print the XOR and OR of all # palindromic number print("XOR =", CalculateXOR,end = " "); print("OR = ", CalculateOR); # Driver Code if __name__ == "__main__" : n = 4; CalculateXORandOR(n); # This code is contributed by AnkitRai01
C#
// C# program to find the XOR // and OR of all palindrome numbers // of N digits using System; class GFG { // Function to check if a number // is palindrome or not static bool ispalin(int num) { // Convert the num n to string string s = num.ToString(); int st = 0; int ed = s.Length - 1; // Iterate over string to // check whether it is // palindromic or not while (st <= ed) { if (s[st] != s[ed]) return false; st++; ed--; } return true; } // Function to find XOR of all // N-digits palindrome number static void CalculateXORandOR(int n) { // To store the XOR and OR of all // palindromic number int CalculateXOR = 0; int CalculateOR = 0; // Starting N-digit // palindromic number int start = (int)Math.Pow(10, n - 1); // Ending N-digit // palindromic number int end = (int)Math.Pow(10, n) - 1; // Iterate over starting and /// ending number for (int i = start; i <= end; i++) { // To check if i is // palindromic or not if (ispalin(i)) { CalculateXOR = CalculateXOR ^ i; CalculateOR = CalculateOR | i; } } // Print the XOR and OR of all // palindromic number Console.Write("XOR = " + CalculateXOR); Console.WriteLine(" OR = " + CalculateOR); } // Driver Code public static void Main (string[] args) { int n = 4; CalculateXORandOR(n); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript program to find the XOR // and OR of all palindrome numbers // of N digits // Function to check if a number // is palindrome or not function ispalin(num) { // Convert the num n to string let s = num.toString(); let st = 0, ed = s.length - 1; // Iterate over string to // check whether it is // palindromic or not while (st <= ed) { if (s[st] != s[ed]) return false; st++; ed--; } return true; } // Function to find XOR of all // N-digits palindrome number function CalculateXORandOR(n) { // To store the XOR and OR of all // palindromic number let CalculateXOR = 0; let CalculateOR = 0; // Starting N-digit // palindromic number let start = Math.pow(10, n - 1); // Ending N-digit // palindromic number let end = Math.pow(10, n) - 1; // Iterate over starting and /// ending number for (let i = start; i <= end; i++) { // To check if i is // palindromic or not if (ispalin(i)) { CalculateXOR = CalculateXOR ^ i; CalculateOR = CalculateOR | i; } } // Print the XOR and OR of all // palindromic number document.write("XOR = " + CalculateXOR); document.write(" OR = " + CalculateOR); } // Driver Code let n = 4; CalculateXORandOR(n); </script>
Producción:
XOR = 4606 OR = 16383
Complejidad del tiempo: O(10 n )
Publicación traducida automáticamente
Artículo escrito por bhagatsunny96 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA