题目链接: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. html5中Canvas为什么要用getContext('2d')

    HTML DOM getContext() 方法 HTML DOM Canvas 对象 定义和用法 getContext() 方法返回一个用于在画布上绘图的环境. 语法 Canvas.getConte ...

  2. Java的多线程机制系列:(三)synchronized的同步原理

    synchronized关键字是JDK5之实现锁(包括互斥性和可见性)的唯一途径(volatile关键字能保证可见性,但不能保证互斥性,详细参见后文关于vloatile的详述章节),其在字节码上编译为 ...

  3. 洛谷P1203 [USACO1.1]坏掉的项链Broken Necklace

    题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A ...

  4. 在Main中定义student的结构体,进行年龄从大到小依次排序录入学生信息。(结构体的用法以及冒泡排序)

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  5. Deep Learning in a Nutshell: Core Concepts

    Deep Learning in a Nutshell: Core Concepts This post is the first in a series I’ll be writing for Pa ...

  6. [工作积累] Google/Amazon平台的各种坑

    所谓坑, 就是文档中没有标明的特别需要处理的细节, 工作中会被无故的卡住各种令人恼火的问题. 包括系统级的bug和没有文档化的限制. 继Android的各种坑后, 现在做Amazon平台, 遇到的坑很 ...

  7. 配置 Apache+php多端口多站点(转载)

    配置httpd.conf监听多个端口 #增加监听端口 等以下内容都设置以后,可以通过 netstat -n -a查看端口是否开启 开启虚拟站点 # Virtual hosts#Include conf ...

  8. 移动web调式利器---Rosin研究

    移动web调式利器---Rosin研究 阅读目录 关于Rosin Rosin在Fiddler中如何使用 回到顶部 一:关于Rosin Rosin是Fiddler的一个插件,它能接受页面中的JS的con ...

  9. Spring常用的接口和类(三)

    一.CustomEditorConfigurer类 CustomEditorConfigurer可以读取实现java.beans.PropertyEditor接口的类,将字符串转为指定的类型.更方便的 ...

  10. Spring AOP使用整理:自动代理以及AOP命令空间

    三.自动代理的实现 1.使用BeanNameAutoProxyCreator 通过Bean的name属性自动生成代理Bean. <bean class="org.springframe ...