📜  数字的幂加和等于数字本身的数字计数

📅  最后修改于: 2021-04-22 07:17:39             🧑  作者: Mango

给定一个整数N ,任务是对小于或等于N的所有整数进行计数,该整数遵循该属性的性质,即它们的位数加总到幂(从1开始,每次加1)等于该整数本身即,如果D 1 D 2 D 3 …D NN位数字,则要满足给定的属性(D 1 1 + D 2 2 + D 3 3 +…+ D N N )必须等于D 1 D 2 D 3 …D N。

例子:

方法:初始化count = 0 ,对于从0N的每个数字,找到提高到幂的数字总和,如果结果总和等于数字本身,则增加count 。最后,打印计数

下面是上述方法的实现:

CPP
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the
// count of digits of n
int countDigits(int n)
{
    int cnt = 0;
    while (n > 0) {
        cnt++;
        n /= 10;
    }
    return cnt;
}
  
// Function to return the sum of
// increasing powers of N
int digitPowSum(int n)
{
  
    // To store the required answer
    int sum = 0;
  
    // Count of digits in n which will
    // be the power of the last digit
    int pw = countDigits(n);
  
    // While there are digits left
    while (n > 0) {
  
        // Get the last digit
        int d = n % 10;
  
        // Add the last digit after raising
        // it to the required power
        sum += pow(d, pw);
  
        // Decrement the power for
        // the previous digit
        pw--;
  
        // Remove the last digit
        n /= 10;
    }
    return sum;
}
  
// Function to return the count
// of integers which satisfy
// the given conditions
int countNum(int n)
{
    int count = 0;
  
    for (int i = 0; i <= n; i++) {
  
        // If current element satisfies
        // the given condition
        if (i == digitPowSum(i)) {
            count++;
        }
    }
    return count;
}
  
// Driver code
int main()
{
    int n = 200;
  
    cout << countNum(n);
  
    return 0;
}


Java
// Java implementation of the approach 
  
class GFG 
{
      
    // Function to return the 
    // count of digits of n 
    static int countDigits(int n) 
    { 
        int cnt = 0; 
        while (n > 0) 
        { 
            cnt++; 
            n /= 10; 
        } 
        return cnt; 
    } 
      
    // Function to return the sum of 
    // increasing powers of N 
    static int digitPowSum(int n) 
    { 
      
        // To store the required answer 
        int sum = 0; 
      
        // Count of digits in n which will 
        // be the power of the last digit 
        int pw = countDigits(n); 
      
        // While there are digits left 
        while (n > 0)
        { 
      
            // Get the last digit 
            int d = n % 10; 
      
            // Add the last digit after raising 
            // it to the required power 
            sum += Math.pow(d, pw); 
      
            // Decrement the power for 
            // the previous digit 
            pw--; 
      
            // Remove the last digit 
            n /= 10; 
        } 
        return sum; 
    } 
      
    // Function to return the count 
    // of integers which satisfy 
    // the given conditions 
    static int countNum(int n) 
    { 
        int count = 0; 
      
        for (int i = 0; i <= n; i++) 
        { 
      
            // If current element satisfies 
            // the given condition 
            if (i == digitPowSum(i)) 
            { 
                count++; 
            } 
        } 
        return count; 
    } 
      
    // Driver code 
    public static void main (String[] args)
    { 
        int n = 200; 
      
        System.out.println(countNum(n)); 
      
    } 
}
  
// This code is contributed by AnkitRai01


Python
# Python3 implementation of the approach
  
# Function to return the
# count of digits of n
def countDigits(n):
    cnt = 0
    while (n > 0):
        cnt += 1
        n //= 10
    return cnt
  
# Function to return the sum of
# increasing powers of N
def digitPowSum(n):
  
    # To store the required answer
    sum = 0
  
    # Count of digits in n which will
    # be the power of the last digit
    pw = countDigits(n)
  
    # While there are digits left
    while (n > 0):
  
        # Get the last digit
        d = n % 10
  
        # Add the last digit after raising
        # it to the required power
        sum += pow(d, pw)
  
        # Decrement the power for
        # the previous digit
        pw -= 1
  
        # Remove the last digit
        n //= 10
    return sum
  
# Function to return the count
# of integers which satisfy
# the given conditions
def countNum(n):
  
    count = 0
  
    for i in range(n + 1):
  
        # If current element satisfies
        # the given condition
        if (i == digitPowSum(i)):
            count += 1
  
    return count
  
# Driver code
n = 200
  
print(countNum(n))
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System;
  
class GFG 
{ 
      
    // Function to return the 
    // count of digits of n 
    static int countDigits(int n) 
    { 
        int cnt = 0; 
        while (n > 0) 
        { 
            cnt++; 
            n /= 10; 
        } 
        return cnt; 
    } 
      
    // Function to return the sum of 
    // increasing powers of N 
    static int digitPowSum(int n) 
    { 
      
        // To store the required answer 
        int sum = 0; 
      
        // Count of digits in n which will 
        // be the power of the last digit 
        int pw = countDigits(n); 
      
        // While there are digits left 
        while (n > 0) 
        { 
      
            // Get the last digit 
            int d = n % 10; 
      
            // Add the last digit after raising 
            // it to the required power 
            sum += (int) Math.Pow(d, pw); 
      
            // Decrement the power for 
            // the previous digit 
            pw--; 
      
            // Remove the last digit 
            n /= 10; 
        } 
        return sum; 
    } 
      
    // Function to return the count 
    // of integers which satisfy 
    // the given conditions 
    static int countNum(int n) 
    { 
        int count = 0; 
      
        for (int i = 0; i <= n; i++)
        { 
      
            // If current element satisfies 
            // the given condition 
            if (i == digitPowSum(i)) 
                count++; 
        } 
        return count; 
    } 
      
    // Driver code 
    public static void Main (String[] args)
    { 
        int n = 200; 
      
        Console.WriteLine(countNum(n)); 
      
    } 
} 
  
// This code is contributed by
// sanjeev2552


输出:
13