码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5062 Beautiful Palindrome Number(Bestcodeer Round #13)

时间:2014-10-12 11:38:27      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:algorithm

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 (a1a2ak?1akak?1a2a1)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
 

结果就只有七个,交上去一个表就行了,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;
}


hdu 5062 Beautiful Palindrome Number(Bestcodeer Round #13)

标签:algorithm

原文地址:http://blog.csdn.net/caduca/article/details/40014941

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!