📜  N位回文数被9整除的计数

📅  最后修改于: 2021-05-07 09:54:19             🧑  作者: Mango

给定整数N ,任务是计算包含从1到9的数字并可被9整除的N位回文数。

例子:

方法:该问题的主要观察结果是,如果该数字可以被9整除,那么该数字的位数之和也可以被9整除。因此,可以根据奇偶性对问题进行分类。

  • 如果N为奇数:我们可以将1到9之间的任何数字放在1到(N-1)/ 2的位置,类似地,以相反的顺序选择其他数字以形成回文数,并且选择中间的元素以形成回文数。可被9整除的数字总和。因此,该数字的(N-1)/ 2个数字的每个位置有9个选择,因此该数字的计数为:
    Count of N-digit Palindromic numbers =
                     9(N-1)/2
    
  • 如果N是偶数:我们可以将1到9的任何数字放在1到(N-2)/ 2的位置,类似地,其他数字以相反的顺序选择以形成回文数,而中间的元素则选择到形成可以被9整除的数字总和。因此,该数字的(N-2)/ 2个数字的每个位置有9个选择,因此该数字的计数为:
    Count of N-digit Palindromic numbers =
                     9(N-2)/2
    
C++
// C++ implementation to count the
// number of N digit palindromic
// numbers divisible by 9
  
#include 
  
using namespace std;
  
// Function to find the count of 
// N digits palindromic numbers 
// which are divisible by 9
int countPalindromic(int n)
{
    int count;
  
    // if N is odd
    if (n % 2 == 1) {
        count = pow(9, (n - 1) / 2);
    }
    // if N is even
    else
    {
        count = pow(9, (n - 2) / 2);
    }
    return count;
}
  
// Driver Code
int main()
{
    int n = 3;
    cout << countPalindromic(n);
    return 0;
}


Java
// Java implementation to count the
// number of N digit palindromic
// numbers divisible by 9
import java.util.*;
  
class GFG{
      
// Function to find the count of 
// N digits palindromic numbers 
// which are divisible by 9
static int countPalindromic(int n)
{
    int count;
  
    // If N is odd
    if (n % 2 == 1)
    {
        count = (int) Math.pow(9, (n - 1) / 2);
    }
      
    // If N is even
    else
    {
        count = (int) Math.pow(9, (n - 2) / 2);
    }
    return count;
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 3;
    System.out.println(countPalindromic(n));
}
}
  
// This code is contributed by ANKITKUMAR34


Python3
# Python3 implementation to count the
# number of N digit palindromic
# numbers divisible by 9
  
# Function to find the count of 
# N digits palindromic numbers 
# which are divisible by 9
def countPalindromic(n):
      
    count = 0
      
    # If N is odd
    if (n % 2 == 1):
        count = pow(9, (n - 1) // 2)
          
    # If N is even
    else:
        count = pow(9, (n - 2) // 2)
          
    return count
  
# Driver Code
n = 3
print(countPalindromic(n))
  
# This code is contributed by ANKITKUMAR34


C#
// C# implementation to count the
// number of N digit palindromic
// numbers divisible by 9
using System;
class GFG{
      
// Function to find the count of 
// N digits palindromic numbers 
// which are divisible by 9
static int countPalindromic(int n)
{
    int count;
  
    // If N is odd
    if (n % 2 == 1)
    {
        count = (int) Math.Pow(9, (n - 1) / 2);
    }
      
    // If N is even
    else
    {
        count = (int) Math.Pow(9, (n - 2) / 2);
    }
    return count;
}
  
// Driver Code
public static void Main()
{
    int n = 3;
    Console.Write(countPalindromic(n));
}
}
  
// This code is contributed by Nidhi_biet


输出:
9