前 K 个偶数长度回文数之和
给定一个整数 k,求前 k 个偶数长度回文数之和。
这里的偶数长度是指一个数的位数是偶数。
例子:
Input : k = 3
Output : 66
Explanation: 11 + 22 + 33 = 66 (Sum
of first three even-length palindrome
numbers)
Input : 10
Output : 1496
Explanation: 11+22+33+44+55+66+77+88+
99+1001 = 1496
一种天真的方法是检查每个偶数长度,如果它是回文数,那么我们将其相加。我们对前 K 个偶数长度的回文数重复相同的过程,并将它们相加得到总和。
在这种情况下,由于偶数长度从 10-99 到 1000-9999 等等,复杂性会变高……
10-99, 1000-9999, 100000-999999.. 分别有 9, 90, 900 个回文数,所以要检查 k 个数字,我们必须检查很多数字,但效率不够。
一种有效的方法是观察偶数长度素数的模式。
11, 22, 33, 44, 55, 66, 77, 88, 99, 1001, 1111, 1221, 1331, 1441, 1551, 1661…
第一个数字是 11,第二个是 22,第三个是 33,第 16 个是16-rev(16) 即 1661 。
所以第 N 个数字将 int(字符串(n)+rev(字符串(n))。
有关整数到字符串和字符串到整数的转换,请参见此处。
下面是上述方法的实现:
C++
#include
#include
using namespace std;
// function to return the sum of
// first K even length palindrome numbers
int sum(int k)
{
// loop to get sum of first K even
// palindrome numbers
int sum = 0;
for (int i = 1; i <= k; i++) {
// convert integer to string
string num = to_string(i);
// Find reverse of num.
string revNum = num;
reverse(revNum.begin(), revNum.end());
// string(n)+rev(string(n)
string strnum = (num + revNum);
// convert string to integer
int number = boost::lexical_cast(strnum);
sum += number; // summation
}
return sum;
}
// driver program to check the above function
int main()
{
int k = 3;
cout << sum(k);
return 0;
}
Java
// Java implementation to find sum of
// first K even-length Palindrome numbers
import java.util.*;
import java.lang.*;
public class GfG{
public static String reverseString(String str)
{
StringBuilder sb = new StringBuilder(str);
sb.reverse();
return sb.toString();
}
// function to return the sum of
// first K even length palindrome numbers
static int sum(int k)
{
// loop to get sum of first K even
// palindrome numbers
int sum = 0;
for (int i = 1; i <= k; i++) {
// convert integer to string
String num = Integer.toString(i);
// Find reverse of num.
String revNum = num;
revNum = reverseString(num);
// string(n)+rev(string(n)
String strnum = (num + revNum);
// convert string to integer
int number = Integer.parseInt(strnum);
sum += number; // summation
}
return sum;
}
// driver function
public static void main(String argc[])
{
int n = 3;
System.out.println(sum(n));
}
}
// This code is contributed by Prerna Saini
Python3
# Python3 implementation of the approach
# function to return the sum of
# first K even length palindrome numbers
def summ(k):
# loop to get sum of first K even
# palindrome numbers
sum = 0
for i in range(1, k + 1):
# convert integer to string
num = str(i)
# Find reverse of num.
revNum = num
revNum = ''.join(reversed(revNum))
# string(n)+rev(string(n)
strnum = num + revNum
# convert string to integer
number = int(strnum)
sum += number # summation
return sum
# Driver Code
if __name__ == "__main__":
k = 3
print(summ(k))
# This code is contributed by
# sanjeev2552
C#
// C# implementation to find sum of
// first K even-length Palindrome numbers
using System;
class GfG
{
// function to return the sum of
// first K even length palindrome numbers
static int sum(int k)
{
// loop to get sum of first K even
// palindrome numbers
int sum = 0;
for (int i = 1; i <= k; i++)
{
// convert integer to string
String num = Convert.ToString(i);
// Find reverse of num.
String revNum = num;
revNum = reverse(num);
// string(n)+rev(string(n)
String strnum = (num + revNum);
// convert string to integer
int number = Convert.ToInt32(strnum);
sum += number; // summation
}
return sum;
}
static String reverse(String input)
{
char[] temparray = input.ToCharArray();
int left, right = 0;
right = temparray.Length - 1;
for (left = 0; left < right; left++, right--)
{
// Swap values of left and right
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.Join("",temparray);
}
// Driver code
public static void Main(String []argc)
{
int n = 3;
Console.WriteLine(sum(n));
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
66