📜  所有N位回文数之和

📅  最后修改于: 2021-05-04 17:20:09             🧑  作者: Mango

给定数字N。任务是找到所有N个数字回文的总和。

例子:

Input: N = 2
Output: 495
Explanation: 
11 + 22 + 33 + 44 + 55 +
66 + 77 + 88 + 99
= 495

Input: N = 7
Output: 49500000000

天真的方法:

运行从10 ^(n-1)到10 ^(n)– 1的循环,并检查当前数字是否为回文数。如果添加了答案,那就很有价值。

下面是上述方法的实现:

CPP
// C++ program for the
// above approach
#include 
using namespace std;
  
// Function to check
// palindrome
bool isPalindrome(string& s)
{
    int left = 0, right = s.size() - 1;
    while (left <= right) {
        if (s[left] != s[right]) {
            return false;
        }
        left++;
        right--;
    }
    return true;
}
  
// Function to calculate
// the sum of n-digit
// palindrome
long long getSum(int n)
{
  
    int start = pow(10, n - 1);
    int end = pow(10, n) - 1;
  
    long long sum = 0;
  
    // Run a loop to check
    // all possible palindrome
    for (int i = start; i <= end; i++) {
        string s = to_string(i);
        // If palndrome
        // append sum
        if (isPalindrome(s)) {
            sum += i;
        }
    }
  
    return sum;
}
  
// Driver code
int main()
{
  
    int n = 1;
    long long ans = getSum(n);
    cout << ans << '\n';
  
    return 0;
}


Java
// Java program for the
// above approach
import java.util.*;
  
class GFG
{
  
// Function to check
// palindrome
static boolean isPalindrome(String s)
{
    int left = 0, right = s.length() - 1;
    while (left <= right)
    {
        if (s.charAt(left) != s.charAt(right))
        {
            return false;
        }
        left++;
        right--;
    }
    return true;
}
  
// Function to calculate
// the sum of n-digit
// palindrome
static long getSum(int n)
{
  
    int start = (int) Math.pow(10, n - 1);
    int end = (int) (Math.pow(10, n) - 1);
  
    long sum = 0;
  
    // Run a loop to check
    // all possible palindrome
    for (int i = start; i <= end; i++)
    {
        String s = String.valueOf(i);
          
        // If palndrome
        // append sum
        if (isPalindrome(s))
        {
            sum += i;
        }
    }
  
    return sum;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 1;
    long ans = getSum(n);
    System.out.print(ans);
}
}
  
// This code is contributed by 29AjayKumar


Python
# Python program for the above approach 
import math
  
# Function to check 
# palindrome 
def isPalindrome(s):
    left = 0
    right = len(s) - 1
    while (left <= right):
        if (s[left] != s[right]):
            return False
          
        left = left + 1
        right = right - 1
  
    return True
  
# Function to calculate 
# the sum of n-digit 
# palindrome 
def getSum(n):
    start = int(math.pow(10, n - 1)) 
    end = int(math.pow(10, n)) - 1
  
    sum = 0
  
    # Run a loop to check 
    # all possible palindrome 
    for i in range(start, end + 1):
        s = str(i) 
          
        # If palndrome 
        # append sum 
        if (isPalindrome(s)):
            sum = sum + i
  
    return sum
  
# Driver code 
  
n = 1
ans = getSum(n)
print(ans)
  
# This code is contributed by Sanjit_Prasad


C#
// C# program for the above approach
using System;
  
class GFG
{
      
    // Function to check
    // palindrome
    static bool isPalindrome(string s)
    {
        int left = 0, right = s.Length - 1;
        while (left <= right)
        {
            if (s[left] != s[right])
            {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
      
    // Function to calculate
    // the sum of n-digit
    // palindrome
    static long getSum(int n)
    {
      
        int start = (int) Math.Pow(10, n - 1);
        int end = (int) (Math.Pow(10, n) - 1);
      
        long sum = 0;
      
        // Run a loop to check
        // all possible palindrome
        for (int i = start; i <= end; i++)
        {
            string s = i.ToString();;
              
            // If palndrome
            // append sum
            if (isPalindrome(s))
            {
                sum += i;
            }
        }
      
        return sum;
    }
      
    // Driver code
    public static void Main()
    {
        int n = 1;
        long ans = getSum(n);
        Console.Write(ans);
    }
}
  
// This code is contributed by AnkitRai01


C++
// C++ program for
// the above approach
#include 
using namespace std;
  
// Function to calculate
// sum of n digit number
long double getSum(int n)
{
  
    long double sum = 0;
    // Corner case
    if (n == 1) {
        sum = 45.0;
    }
    // Using above approach
    else {
        sum = (99.0 / 2.0) * pow(10, n - 1)
              * pow(10, (n - 1) / 2);
    }
    return sum;
}
  
// Driver code
int main()
{
  
    int n = 3;
    long double ans = getSum(n);
    cout << setprecision(12) << ans << '\n';
  
    return 0;
}


Java
// Java program for
// the above approach
  
class GFG
{
  
    // Function to calculate
    // sum of n digit number
    static double getSum(int n)
    {
  
        double sum = 0;
          
        // Corner case
        if (n == 1) 
        {
            sum = 45.0;
        }
          
        // Using above approach
        else 
        {
            sum = (99.0 / 2.0) * 
                    Math.pow(10, n - 1) * 
                    Math.pow(10, (n - 1) / 2);
        }
        return sum;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 3;
        double ans = getSum(n);
        System.out.print(ans);
    }
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python program for
# the above approach
  
# Function to calculate
# sum of n digit number
def getSum(n):
  
    sum = 0;
  
    # Corner case
    if (n == 1):
        sum = 45.0;
      
    # Using above approach
    else:
        sum = (99.0 / 2.0) * pow(10, n - 1)\
        * pow(10, (n - 1) / 2);
      
    return sum;
  
# Driver code
if __name__ == '__main__':
    n = 3;
    ans = int(getSum(n));
    print(ans);
  
# This code is contributed by 29AjayKumar


C#
// C# program for
// the above approach
using System;
  
class GFG
{
  
    // Function to calculate
    // sum of n digit number
    static double getSum(int n)
    {
        double sum = 0;
          
        // Corner case
        if (n == 1) 
        {
            sum = 45.0;
        }
          
        // Using above approach
        else
        {
            sum = (99.0 / 2.0) * 
                    Math.Pow(10, n - 1) * 
                    Math.Pow(10, (n - 1) / 2);
        }
        return sum;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int n = 3;
        double ans = getSum(n);
        Console.Write(ans);
    }
}
  
// This code is contributed by 29AjayKumar


输出:
45

时间复杂度: O(n * log(n))

高效的方法:

仔细观察n位回文的总和便形成了一个序列,即45、495、49500、495000、49500000、495000000。因此,在将其推导为数学公式时,我们得到n = 1 sum = 45且n> 1 put总和=(99/2)* 10 ^ n-1 * 10 ^(n-1)/ 2

下面是上述方法的实现:

C++

// C++ program for
// the above approach
#include 
using namespace std;
  
// Function to calculate
// sum of n digit number
long double getSum(int n)
{
  
    long double sum = 0;
    // Corner case
    if (n == 1) {
        sum = 45.0;
    }
    // Using above approach
    else {
        sum = (99.0 / 2.0) * pow(10, n - 1)
              * pow(10, (n - 1) / 2);
    }
    return sum;
}
  
// Driver code
int main()
{
  
    int n = 3;
    long double ans = getSum(n);
    cout << setprecision(12) << ans << '\n';
  
    return 0;
}

Java

// Java program for
// the above approach
  
class GFG
{
  
    // Function to calculate
    // sum of n digit number
    static double getSum(int n)
    {
  
        double sum = 0;
          
        // Corner case
        if (n == 1) 
        {
            sum = 45.0;
        }
          
        // Using above approach
        else 
        {
            sum = (99.0 / 2.0) * 
                    Math.pow(10, n - 1) * 
                    Math.pow(10, (n - 1) / 2);
        }
        return sum;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 3;
        double ans = getSum(n);
        System.out.print(ans);
    }
}
  
// This code is contributed by PrinciRaj1992

Python3

# Python program for
# the above approach
  
# Function to calculate
# sum of n digit number
def getSum(n):
  
    sum = 0;
  
    # Corner case
    if (n == 1):
        sum = 45.0;
      
    # Using above approach
    else:
        sum = (99.0 / 2.0) * pow(10, n - 1)\
        * pow(10, (n - 1) / 2);
      
    return sum;
  
# Driver code
if __name__ == '__main__':
    n = 3;
    ans = int(getSum(n));
    print(ans);
  
# This code is contributed by 29AjayKumar

C#

// C# program for
// the above approach
using System;
  
class GFG
{
  
    // Function to calculate
    // sum of n digit number
    static double getSum(int n)
    {
        double sum = 0;
          
        // Corner case
        if (n == 1) 
        {
            sum = 45.0;
        }
          
        // Using above approach
        else
        {
            sum = (99.0 / 2.0) * 
                    Math.Pow(10, n - 1) * 
                    Math.Pow(10, (n - 1) / 2);
        }
        return sum;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int n = 3;
        double ans = getSum(n);
        Console.Write(ans);
    }
}
  
// This code is contributed by 29AjayKumar
输出:
49500

时间复杂度: O(1)