Beautiful Palindrome Number 美丽的回文数 HD 5062



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 (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
题目的意思就是输出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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值