📜  计算满足给定条件的所有可能的 N 位数字

📅  最后修改于: 2021-10-25 06:47:24             🧑  作者: Mango

给定一个整数N ,任务是计算所有可能的N 位数字,使得A + reverse(A) = 10 N – 1其中A是 N 位数字,而 reverse(A) 是 A 的反面。 A不应该有任何前导 0。
例子:

方法:首先我们必须得出结论,如果 N 是奇数,那么没有满足给定条件的数字,让我们证明N = 3

现在查找N 为偶数时的答案。例如,N=4,

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to return the count of required numbers
string getCount(int N)
{
 
    // If N is odd then return 0
    if (N % 2 == 1)
        return 0;
 
    string result = "9";
 
    for (int i = 1; i <= N / 2 - 1; i++)
        result += "0";
 
    return result;
}
 
// Driver Code
int main()
{
 
    int N = 4;
    cout << getCount(N);
 
    return 0;
}


Java
// Java implementation of above approach
class GFG
{
    // Function to return the count of required numbers
    static String getCount(int N)
    {
     
        // If N is odd then return 0
        if (N % 2 == 1)
            return "0";
     
        String result = "9";
        for (int i = 1; i <= N / 2 - 1; i++)
            result += "0";
        return result;
    }
     
    // Driver Code
    public static void main(String []args)
    {
     
        int N = 4;
        System.out.println(getCount(N));
    }
}
 
// This code is contributed by ihritik


Python3
# Python3 implementation of above approach
 
# Function to return the count of required numbers
def getCount(N):
 
    # If N is odd then return 0
    if (N % 2 == 1):
        return "0"
 
    result = "9"
 
    for i in range (1, N // 2 ):
        result = result + "0"
 
    return result
 
# Driver Code
N = 4
print(getCount(N))
 
# This code is contributed by ihritik


C#
// C# implementation of above approach
using System;
 
class GFG
{
    // Function to return the count of required numbers
    static string getCount(int N)
    {
     
        // If N is odd then return 0
        if (N % 2 == 1)
            return "0";
        string result = "9";
        for (int i = 1; i <= N / 2 - 1; i++)
            result += "0";
        return result;
    }
     
    // Driver Code
    public static void Main()
    {
     
        int N = 4;
        Console.WriteLine(getCount(N));
    }
}
 
// This code is contributed by ihritik


PHP


Javascript


输出:
90

时间复杂度: O(N)