给定数字N ,任务是找到可被9整除的所有N个数字回文数字(由1到9的数字组成)的总和。
例子:
Input: N = 1
Output: 9
Explanation:
Only 9 is a palindromic number of 1 digit divisible by 9
Input: N = 3
Output: 4995
Explanation:
Three-digit Palindromic Numbers divisible by 9 are –
171, 252, 333, 414, 585, 666, 747, 828, 999
方法:该问题的主要观察结果是,如果一个数字可以被9整除,那么该数字的数字总和也可以被9整除。另一个观察结果是,如果我们使用来自的数字来计算N位数的回文数。 1到9,那么可以观察到
Occurrence of each digit = (count of N-digit numbers / 9)
所以,
- 首先找到可以被9整除的N位回文数的计数,如下所示:
- 然后,如果N为1或2,则总和将分别为9和99,因为它们是唯一的1位和2位数字。
- 如果N> 2,则第N个位数回文数的总和可被9整除为
下面是上述方法的实现:
C++
// C++ implementation to find the sum // of all the N digit palindromic // numbers divisible by 9 #include
using namespace std; // Function for finding count of // N digits palindrome which // are divisible by 9 int countPalindrome(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; } // Function for finding sum of N // digits palindrome which are // divisible by 9 int sumPalindrome(int n) { // count the possible // number of palindrome int count = countPalindrome(n); int res = 0; if (n == 1) return 9; if (n == 2) return 99; for (int i = 0; i < n; i++) { res = res * 10 + count * 5; } return res; } // Driver Code int main() { int n = 3; cout << sumPalindrome(n); return 0; }
Java
// Java implementation to find the sum // of all the N digit palindromic // numbers divisible by 9 import java.util.*; class GFG{ // Function for finding count of // N digits palindrome which // are divisible by 9 static int countPalindrome(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; } // Function for finding sum of N // digits palindrome which are // divisible by 9 static int sumPalindrome(int n) { // Count the possible // number of palindrome int count = countPalindrome(n); int res = 0; if (n == 1) return 9; if (n == 2) return 99; for(int i = 0; i < n; i++) { res = res * 10 + count * 5; } return res; } // Driver Code public static void main(String[] args) { int n = 3; System.out.println(sumPalindrome(n)); } } // This code is contributed by ANKITKUMAR34
Python3
# Python3 implementation to find the # sum of all the N digit palindromic # numbers divisible by 9 # Function for finding count of # N digits palindrome which # are divisible by 9 def countPalindrome(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 # Function for finding sum of N # digits palindrome which are # divisible by 9 def sumPalindrome(n): # Count the possible # number of palindrome count = countPalindrome(n) res = 0 if (n == 1): return 9 if (n == 2): return 99 for i in range(n): res = res * 10 + count * 5 return res # Driver Code n = 3 print(sumPalindrome(n)) # This code is contributed by ANKITKUMAR34
C#
// C# implementation to find the sum // of all the N digit palindromic // numbers divisible by 9 using System; class GFG{ // Function for finding count of // N digits palindrome which // are divisible by 9 static int countPalindrome(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; } // Function for finding sum of N // digits palindrome which are // divisible by 9 static int sumPalindrome(int n) { // Count the possible // number of palindrome int count = countPalindrome(n); int res = 0; if (n == 1) return 9; if (n == 2) return 99; for(int i = 0; i < n; i++) { res = res * 10 + count * 5; } return res; } // Driver Code public static void Main(String[] args) { int n = 3; Console.WriteLine(sumPalindrome(n)); } } // This code is contributed by Amit Katiyar
输出:4995