📌  相关文章
📜  所有N位数字的计数,使得num + Rev(num)= 10 ^ N – 1

📅  最后修改于: 2021-05-04 21:21:37             🧑  作者: Mango

给定整数N ,任务是查找所有N个数字的计数,以使num + Rev(num)= 10 N – 1
例子:

方法有2种情况:
如果n为奇数,则答案将为0。

如果n为偶数,则答案将为9 * 10 (N / 2 – 1)

下面是上述方法的实现

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the
// count of such numbers
int countNumbers(int n)
{
 
    // If n is odd
    if (n % 2 == 1)
        return 0;
 
    return (9 * pow(10, n / 2 - 1));
}
 
// Driver code
int main()
{
    int n = 2;
    cout << countNumbers(n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function to return the
    // count of such numbers
    static int countNumbers(int n)
    {
 
        // If n is odd
        if (n % 2 == 1)
            return 0;
 
        return (9 * (int)Math.pow(10, n / 2 - 1));
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 2;
        System.out.print(countNumbers(n));
    }
}


Python3
# Python3 implementation of the approach
 
# Function to return the
# count of such numbers
def countNumbers(n):
 
    # If n is odd
    if n % 2 == 1:
        return 0
 
    return (9 * pow(10, n // 2 - 1))
 
# Driver code
if __name__ == "__main__":
 
    n = 2
    print(countNumbers(n))
 
# This code is contributed
# by Rituraj Jain


C#
// C# implementation of the approach
using System;
class GFG {
 
    // Function to return the
    // count of such numbers
    static int countNumbers(int n)
    {
 
        // If n is odd
        if (n % 2 == 1)
            return 0;
 
        return (9 * (int)Math.Pow(10, n / 2 - 1));
    }
 
    // Driver code
    public static void Main()
    {
        int n = 2;
        Console.WriteLine(countNumbers(n));
    }
}


PHP


Javascript


输出:
9

时间复杂度: O(1)