Beautiful Palindrome Number
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 526 Accepted Submission(s): 330
Problem Description
A positive integer x can represent as
(a
1
a
2
…a
k
a
k
…a
2
a
1
)
10![]()
or
(a
1
a
2
…a
k−1
a
k
a
k−1
…a
2
a
1
)
10![]()
of a 10-based notational system, we always call x is a Palindrome Number. If it satisfies
0<a
1
<a
2
<…<a
k
≤9
, we call x is a Beautiful Palindrome Number.
Now, we want to know how many Beautiful Palindrome Numbers are between 1 and 10
N![]()
.
Now, we want to know how many Beautiful Palindrome Numbers are between 1 and 10
Input
The first line in the input file is an integer
T(1≤T≤7)
, indicating the number of test cases.
Then T lines follow, each line represent an integer N(0≤N≤6)
.
Then T lines follow, each line represent an integer N(0≤N≤6)
Output
For each test case, output the number of Beautiful Palindrome Number.
Sample Input
2 1 6
Sample Output
9 258题目的意思就是输出1~10^N中上升回文数的个数(上升回文数就是该数是一个回文数且满足左侧各个位数前一位小于后一位)创建两个数组,一个用来存储输出结果,另一个用来存储数字的字符形式————个人觉得转换为字符型更好;中间用到了一个itoa函数————用来将数字转换为字符串————头文件为stdlib.h#include <iostream> #include <cstdlib> #include <cmath> #include <cstring> using namespace std; int T,i,j,k; char st[20]; int ans[7];
bool judge(int m)//不想用字符串判断的话可以在分离数字的同时比较当前数字与上一个数字的大小,不过要注意位数的判断 { int i,sum = 0,k = m; while( k != 0) { i = k % 10; sum = sum * 10 + i; k /= 10; } return (sum == m); } void creatList() { int i,j,len,flag,k; for (i = 1;i < 1000005;i++) { if(judge(i)) { flag = 1; itoa(i,st,10);//转换为字符串——10的意思是以10进制形式转换 len = strlen(st); if(len % 2 != 0) len++; for(j = 0;j < len/2 - 1;j++) { if(st[j] >= st[j + 1]) {flag = 0;break;} } k = log10(i); if(flag) ans[k+1]++;//利用log10运算的特性判断出i属于哪个区间 } } ans[0] = 1; for(i = 2; i < 7; i++) { ans[i] += ans[i - 1]; }//累加 } int main() { cin >> T; creatList(); while(T--) { cin >> i; cout << ans[i] << endl; } }