📜  所有N位回文数的XOR和OR

📅  最后修改于: 2021-04-24 15:50:58             🧑  作者: Mango

给定整数N。任务是找到所有N位回文数的XOR和OR。

例子

方法:

  • 通过以下方式找到N位回文数的开始和结束编号:
    starting number = pow(10, n - 1)
    ending number = pow(10, n) - 1
    
  • 遍历起始编号直到结束编号,并检查该编号是否为回文码。
  • 如果该数字是回文数,则分别进行该数字的XOR和OR。
  • 否则进行下一次迭代,并在所有迭代之后打印XOR和OR的值。

下面是上述方法的实现:

C++
// C++ program to find the XOR
// and OR of all palindrome numbers
// of N digits
#include 
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


输出:
XOR = 4606 OR = 16383

时间复杂度: O(10 n )