题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5062

题目意思:给出 N,找出 1 ~ 10^N 中满足 Beautiful Palindrome Numbers (BPN)的数量有多少。 满足 BPN 的条件有两个:(1)回文串   (2)对称的部分从左到右递增排放。

(1)版本 1 (比较麻烦,建议看版本2)        46ms

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int N = + ;
int ans[N];
int num[N];
int l, ll; bool is_Palindrome(int tmp)
{
l = ;
while (tmp > )
{
num[l++] = tmp % ;
tmp /= ;
}
l--;
for (int i = ; i <= l/; i++)
{
if (num[i] != num[l-i])
return false;
if (num[i] >= num[i+] && i != l/)
return false;
}
return true;
} int main()
{
int cnt;
ans[] = ;
ans[] = ;
//ans[6] = 258;
ll = , cnt = ;
for (int i = ; i <= 1e6; i++)
{
if (is_Palindrome(i))
cnt++;
if (i == )
ans[ll++] = cnt;
if (i == )
ans[ll++] = cnt;
if (i == )
ans[ll++] = cnt;
if (i == )
ans[ll++] = cnt;
if (i == 1e6)
ans[ll] = cnt;
}
int T, n;
while (scanf("%d", &T) != EOF)
{
while (T--)
{
scanf("%d", &n);
printf("%d\n", n == ? : ans[n]);
}
}
return ;
}

(2)版本 2 (主要利用各种字符串处理函数)   250ms

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N = + ;
char str1[N], str2[N];
int ans[N]; int main()
{
for (int i = ; i <= 1e6; i++)
{
sprintf(str1, "%d", i);
int len = strlen(str1);
strcpy(str2, str1);
reverse(str2, str2+len);
if (strcmp(str1, str2))
continue;
bool flag = true;
for (int j = ; j < (len+)/; j++)
{
if (str1[j-] >= str2[j])
{
flag = false;
break;
}
}
if (flag)
ans[len]++;
}
for (int i = ; i <= ; i++)
ans[i] += ans[i-]; int t, n;
while (scanf("%d", &t) != EOF)
{
while (t--)
{
scanf("%d", &n);
printf("%d\n", n == ? : ans[n]); // 10^0 也要考虑,即1
}
}
return ; }

听说还可以用组合数学做,一个一个数当然也行

BestCoder13 1001.Beautiful Palindrome Number(hdu 5062) 解题报告的更多相关文章

  1. BestCoder24 1001.Sum Sum Sum(hdu 5150) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5150 题目意思:就是直接求素数. 不过 n = 1,也属于答案范围!!只能说,一失足成千古恨啊---- ...

  2. BestCoder10 1001 Revenge of Fibonacci(hdu 5018) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5018 题目意思:给出在 new Fibonacci 中最先的两个数 A 和 B(也就是f[1] = A ...

  3. HDU 5062 Beautiful Palindrome Number(数学)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can re ...

  4. BestCoder8 1001.Summary(hdu 4989) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4989 题目意思:给出 n 个数,然后将这些数两两相加,得到 n*(n-1) /2 对和,把重复的和去掉 ...

  5. BestCoder19 1001.Alexandra and Prime Numbers(hdu 5108) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5108 题目意思:给出一个数正整数 N,N <= 1e9,现在需要找出一个最少的正整数 M,使得 ...

  6. BestCoder17 1001.Chessboard(hdu 5100) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5100 题目意思:有一个 n * n 的棋盘,需要用 k * 1 的瓷砖去覆盖,问最大覆盖面积是多少. ...

  7. BestCoder12 1001.So easy(hdu 5058) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5058 (格式有点问题,为了方便阅读---整个复制下来吧) 题目意思:给出两个长度都为 n 的集合你,问 ...

  8. BestCoder7 1001 Little Pony and Permutation(hdu 4985) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4985 题目意思:有 n 个数,对于第 i 个数给出 σ(i) 的值.求出互不相交的循环的个数,并输出每 ...

  9. BestCoder3 1001 Task schedule(hdu 4907) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4907 题目意思:给出工作表上的 n 个任务,第 i 个任务需要 ti 这么长的时间(持续时间是ti ~ ...

随机推荐

  1. 粒子群优化算法-python实现

    PSOIndividual.py import numpy as np import ObjFunction import copy class PSOIndividual: ''' individu ...

  2. 移动H5页面,keyup事件不好使用处理解决

    1.mouse事件换成touch,pointer事件,keyup换成textInput事件

  3. [NOIP2011] 提高组 洛谷P1003 铺地毯

    题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有 n 张地毯,编号从 1 到n .现在将这些地毯按照编号从小到大的顺序平行于 ...

  4. Linux Systemcall Int0x80方式、Sysenter/Sysexit Difference Comparation

    目录 . 系统调用简介 . Linux系统调用实现方式的演进 . 通过INT 0x80中断方式进入系统调用 . 通过sysenter指令方式直接进入系统调用 . sysenter/sysexit编程示 ...

  5. [IOS UIalert模版]

    1.alertview创建 UIAlertView *alert; alert = [[UIAlertView alloc] initWithTitle:@"提示" message ...

  6. 如何在 Arch Linux 的终端里设定 WiFi 网络

    如果你使用的是其他 Linux 发行版 而不是 Arch CLI,那么可能会不习惯在终端里设置 WiFi.尽管整个过程有点简单,不过我还是要讲一下.在这篇文章里,我将带领新手们通过一步步的设置向导,把 ...

  7. python学习易错点1

    1.>>> d = {'x': 'A', 'y': 'B', 'z': 'C' } >>> for k, v in d.iteritems(): ... print ...

  8. FITTING A MODEL VIA CLOSED-FORM EQUATIONS VS. GRADIENT DESCENT VS STOCHASTIC GRADIENT DESCENT VS MINI-BATCH LEARNING. WHAT IS THE DIFFERENCE?

    FITTING A MODEL VIA CLOSED-FORM EQUATIONS VS. GRADIENT DESCENT VS STOCHASTIC GRADIENT DESCENT VS MIN ...

  9. Metasploit自动攻击和选择模块攻击详解

    Author:魔术@Freebuf.com 0×1自动攻击 终端启动Metasploit,因为我现在Source Code,所以这样启动! 连接数据库 安装方法,执行以下命令即可(请用ROOT执行). ...

  10. WINDOWS渗透与提权总结(2)

    vbs 下载者: 01 1: 02   03 echo Set sGet = createObject("ADODB.Stream") >>c:\windows\cft ...