📜  所有N位阿姆斯壮数字的XOR和OR

📅  最后修改于: 2021-05-06 18:30:23             🧑  作者: Mango

给定整数N ,任务是找到所有N位数字阿姆斯壮数字的XOROR值。

例子

方法:

  • 通过以下方法找到N位数字阿姆斯特朗编号的开始和结束编号:
    Starting N-digit Armstrong number = pow(10, n - 1)
    Ending N-digit Armstrong number   = pow(10, n) - 1
    
  • 从起始编号到结束编号对N位数字的阿姆斯特朗编号进行迭代,然后检查该编号是否为阿姆斯特朗。
  • 如果数字是Armstrong,则分别对该数字进行XOR和OR。
  • 否则进行下一次迭代,并在所有迭代之后打印XOR和OR的值。

下面是上述方法的实现:

C++
// C++ program to find the XOR 
// and OR of all Armstrong numbers 
// of N digits
#include 
using namespace std;
  
// Function to check if a number
// is Armstrong or not
bool isArmstrong(int x, int n) 
{
    int sum1 = 0;
    int temp = x;
    while (temp > 0) {
        int digit = temp % 10;
        sum1 += (int)pow(digit, n);
        temp /= 10;
    }
    return sum1 == x;
}
  
// Function to find XOR of all
// N-digits Armstrong number
void CalculateXORandOR(int n) 
{
  
    // To store the XOR and OR of all
    // Armstrong number
    int CalculateXOR = 0;
    int CalculateOR = 0;
  
    // Starting N-digit
    // Armstrong number
    int start = (int)pow(10, n - 1);
  
    // Ending N-digit
    // Armstrong number
    int end = (int)pow(10, n) - 1;
      
    // Iterate over starting and
    // ending number
    for (int i = start; i < end + 1; i++) 
    {
  
        // To check if i is
        // Armstrong or not
        if (isArmstrong(i, n)) {
            CalculateXOR = CalculateXOR ^ i;
            CalculateOR = CalculateOR | i;
        }
    }
      
    // Print the XOR and OR of all
    // Armstrong number
    cout << "XOR = " << CalculateXOR << endl;
    cout << "OR = " << CalculateOR << endl;
}
  
// Driver Code
int main() 
{
  
    int n = 4;
    CalculateXORandOR(n);
}
  
// This code is contributed by shivanisinghss2110


Java
// Java program to find the XOR 
// and OR of all Armstrong numbers 
// of N digits 
class GFG 
{
      
    // Function to check if a number
    // is Armstrong or not
    static boolean isArmstrong(int x, int n) {
        int sum1 = 0;
        int temp = x;
        while (temp > 0) {
            int digit = temp % 10;
            sum1 += Math.pow(digit, n);
            temp /= 10;
        }
        return sum1 == x;
    }
  
    // Function to find XOR of all
    // N-digits Armstrong number
    static void CalculateXORandOR(int n) {
  
        // To store the XOR and OR of all
        // Armstrong number
        int CalculateXOR = 0;
        int CalculateOR = 0;
  
        // Starting N-digit
        // Armstrong number
        int start = (int) Math.pow(10, n - 1);
  
        // Ending N-digit
        // Armstrong number
        int end = (int) (Math.pow(10, n)) - 1;
          
        // Iterate over starting and
        // ending number
        for (int i = start; i < end + 1; i++) {
  
            // To check if i is
            // Armstrong or not
            if (isArmstrong(i, n)) {
                CalculateXOR = CalculateXOR ^ i;
                CalculateOR = CalculateOR | i;
            }
        }
          
        // Print the XOR and OR of all
        // Armstrong number
        System.out.println("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 29AjayKumar


Python3
# Python3 program to find the XOR 
# and OR of all Armstrong numbers 
# of N digits 
  
# Function to check if a number 
# is Armstrong or not 
def isArmstrong (x, n): 
    sum1 = 0  
    temp = x  
    while temp > 0: 
        digit = temp % 10
        sum1 += digit **n 
        temp //= 10
    return sum1 == x
  
# Function to find XOR of all 
# N-digits Armstrong number 
def CalculateXORandOR(n) : 
  
    # To store the XOR and OR of all 
    # Armstrong number 
    CalculateXOR = 0
    CalculateOR = 0
  
    # Starting N-digit 
    # Armstrong number 
    start = 10 ** (n - 1)
  
    # Ending N-digit 
    # Armstrong number 
    end = (10**n) - 1
    # Iterate over starting and 
    # ending number 
    for i in range( start, end + 1) : 
  
        # To check if i is 
        # Armstrong or not 
        if (isArmstrong(i, n)) : 
            CalculateXOR = CalculateXOR ^ i
            CalculateOR = CalculateOR | i
  
    # Print the XOR and OR of all 
    # Armstrong number 
    print("XOR = ", CalculateXOR) 
    print("OR = ", CalculateOR)
  
# Driver Code 
if __name__ == "__main__" : 
  
    n = 4; 
    CalculateXORandOR(n);


C#
// C# program to find the XOR 
// and OR of all Armstrong numbers 
// of N digits 
using System;
  
class GFG 
{
      
    // Function to check if a number
    // is Armstrong or not
    static bool isArmstrong(int x, int n) {
        int sum1 = 0;
        int temp = x;
        while (temp > 0) {
            int digit = temp % 10;
            sum1 += (int)Math.Pow(digit, n);
            temp /= 10;
        }
        return sum1 == x;
    }
  
    // Function to find XOR of all
    // N-digits Armstrong number
    static void CalculateXORandOR(int n) {
  
        // To store the XOR and OR of all
        // Armstrong number
        int CalculateXOR = 0;
        int CalculateOR = 0;
  
        // Starting N-digit
        // Armstrong number
        int start = (int) Math.Pow(10, n - 1);
  
        // Ending N-digit
        // Armstrong number
        int end = (int) (Math.Pow(10, n)) - 1;
          
        // Iterate over starting and
        // ending number
        for (int i = start; i < end + 1; i++) {
  
            // To check if i is
            // Armstrong or not
            if (isArmstrong(i, n)) {
                CalculateXOR = CalculateXOR ^ i;
                CalculateOR = CalculateOR | i;
            }
        }
          
        // Print the XOR and OR of all
        // Armstrong number
        Console.WriteLine("XOR = " + CalculateXOR);
        Console.WriteLine("OR = " + CalculateOR);
    }
  
    // Driver Code
    public static void Main(String[] args) {
  
        int n = 4;
        CalculateXORandOR(n);
    }
}
  
// This code is contributed by PrinciRaj1992


输出:
XOR =  880
OR =  10098