BestCoder Round #13 Beautiful Palindrome Number解题报告

来源:互联网 发布:青岛海尔人工智能 编辑:程序博客网 时间:2024/05/22 08:05

Beautiful Palindrome Number

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 404    Accepted Submission(s): 257


Problem Description
A positive integer x can represent as (a1a2akaka2a1)10 or (a1a2ak1akak1a2a1)10 of a 10-based notational system, we always call x is a Palindrome Number. If it satisfies0<a1<a2<<ak9, we call x is a Beautiful Palindrome Number.
Now, we want to know how many Beautiful Palindrome Numbers are between 1 and 10N.
 

Input
The first line in the input file is an integer T(1T7), indicating the number of test cases.
Then T lines follow, each line represent an integer N(0N6).
 

Output
For each test case, output the number of Beautiful Palindrome Number.
 

Sample Input
216
 

Sample Output
9258
 题意:
将一个整数用一个序列表示(序列上的值就是该位上的数字),如果序列满足给的条件,就算一个XX数,算有多少个数,可以先暴力,再打表!

/*其实思路很简单,但是<span style="font-family:MathJax_Math;">写这个代码的时候,别人总烦着我,弄得心情超不爽,静不下来好好写,弄得被几种特殊情况搞混乱了</span>*/#include<cstdio>#include<math.h>#include<iostream>using namespace std;int an[8];int get(int num)    //这个函数可移植还是很高的,它专门用来<span style="font-family:MathJax_Math;">将一个整数分解成一个序列(不过记得这个实现的是倒序表示的,当然在这里无所谓)</span>{int cnt=0;while(num>0){an[cnt++]=num%10;num/=10;}return cnt;}bool check(int cnt)  //这里,<span style="font-family:MathJax_Math;">分了两步来讨论,其实主要是一个关注a[i]>a[i+1]另一个是回文数的条件,这两个一起考虑,自己容易乱,这样分开两个步骤来写,</span>{               <span style="font-family:MathJax_Math;">    其实时间复杂度并没有因为多了个循环而增大(这个我以前一直以为会增大),反而编码的难度小了,自己好写代码!</span>int k=cnt%2==0? (cnt/2)-1:(cnt/2);if(cnt>=3){for(int i=k;i>=1;i--)  //<span style="font-family:MathJax_Math;">关注a[i]>a[i+1]</span>{if(an[i]<=an[i-1]){return false;}}}for(int i=0;i<cnt/2;i++)  <span style="font-family:MathJax_Math;">//关注回文数条件</span>{if(an[i]!=an[cnt-1-i])return false;}return true;}int main(){int t;scanf("%d",&t);while(t--){int n;scanf("%d",&n);int ans=0;for(int i=1;i<=pow(10,n);i++){if(check(get(i)))ans++;}printf("%d\n",ans);}} 



0 0
原创粉丝点击