hdu 5062 Beautiful Palindrome Number(Bestcodeer Round #13)

Beautiful Palindrome Number

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


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 satisfies  0<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
   
   
   
   
2 1 6
 

Sample Output
   
   
   
   
9 258
 

Source
BestCoder Round #13
 

结果就只有七个,交上去一个表就行了,n最大为6,暴力代码也能过。附上打表代码和暴力代码。

打表代码:
//0ms
#include <cstdio>
#include <cstring>
#include <iostream>
int a[7]={1,9,18,54,90,174,258};
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        printf("%d\n",a[n]);
    }
}

暴力代码:
//46ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[10];
bool judge(int x)
{
    int n=0;
    while(x>0)
    {
        a[++n]=x%10;
        x=x/10;
    }
    //printf("%d\n",n);
    if(a[1]!=a[n])
    return false;
    for(int j=2,k=n-1;j<=k;j++,k--)
    {
        if(a[j]>a[j-1]&&a[j]==a[k])
        ;
        else
        return false;
    }
    return true;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int temp=1;
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            temp*=10;
        }
        //printf("%d",temp);
        for(int i=1;i<=temp;i++)
        {
            if(judge(i))
            {
                ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(Algorithm)