HDU 5062 Beautiful Palindrome Number(数学)

主题链接:http://acm.hdu.edu.cn/showproblem.php?

pid=5062


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


题意:

求1到10的n次方的范围内,满足:

1、是回文数;

2、回文的前半部分满足升序。


打表代码:

#include <cstdio>
#include <cstring>
int find_num(int num)
{
    int a[17];
    memset(a,0,sizeof(a));
    int L = 0;
    while(num)
    {
        a[++L] = num%10;
        num/=10;
    }
    for(int i = 1; i <= L/2; i++)//回文
    {
        if(a[i] != a[L-i+1])
            return 0;
    }
    for(int i = 1; i < L/2+L%2; i++)//升序
    {
        if(a[i+1] <= a[i])
            return 0;
    }
    return 1;
}
int main()
{
    int sum[17];
    memset(sum,0,sizeof(sum));
    sum[0] = 1;
    for(int i = 1; i <= 1000000; i++)
    {
        int flag = find_num(i);
        if(flag)
        {
            //printf("num::%d
",i);
            if(i <= 10)
                sum[1]++;
            if(i <= 100)
                sum[2]++;
            if(i <= 1000)
                sum[3]++;
            if(i <= 10000)
                sum[4]++;
            if(i <= 100000)
                sum[5]++;
            if(i <= 1000000)
                sum[6]++;
        }
    }
    int t;
    for(int i = 0; i <= 6; i++)
    {
        printf("%d::%d
",i,sum[i]);
    }
    return 0;
}



代码例如以下:

#include <cstdio>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        if(n == 0)
            printf("1
");
        else if(n == 1)
            printf("9
");
        else if(n == 2)
            printf("18
");
        else if(n == 3)
            printf("54
");
        else if(n == 4)
            printf("90
");
        else if(n == 5)
            printf("174
");
        else if(n == 6)
            printf("258
");
    }
    return 0;
}


版权声明:本文博主原创文章。博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/hrhguanli/p/4818903.html