给定整数N ,任务是找到N位回文数的计数。
例子:
Input: N = 1
Output: 9
{1, 2, 3, 4, 5, 6, 7, 8, 9} are all the possible
single digit palindrome numbers.
Input: N = 2
Output: 9
方法:第一个数字可以是9个数字中的任何一个(非0),最后一个数字必须与第一个数字相同才能成为回文,第二个和第二个最后一个数字可以是10个数字中的任何一个其他数字也一样。因此,对于任何N值, N位数回文数将为9 * 10 (N – 1)/ 2 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the count
// of N-digit palindrome numbers
int nDigitPalindromes(int n)
{
return (9 * pow(10, (n - 1) / 2));
}
// Driver code
int main()
{
int n = 2;
cout << nDigitPalindromes(n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the count
// of N-digit palindrome numbers
static int nDigitPalindromes(int n)
{
return (9 * (int)Math.pow(10,
(n - 1) / 2));
}
// Driver code
public static void main(String []args)
{
int n = 2;
System.out.println(nDigitPalindromes(n));
}
}
// This code is contributed by Code_Mech
Python3
# Python3 implementation of the approach
# Function to return the count
# of N-digit palindrome numbers
def nDigitPalindromes(n) :
return (9 * pow(10, (n - 1) // 2));
# Driver code
if __name__ == "__main__" :
n = 2;
print(nDigitPalindromes(n));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the count
// of N-digit palindrome numbers
static int nDigitPalindromes(int n)
{
return (9 * (int)Math.Pow(10,
(n - 1) / 2));
}
// Driver code
public static void Main(String []args)
{
int n = 2;
Console.WriteLine(nDigitPalindromes(n));
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
9