题目链接: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. 【BZOJ-2588】Count on a tree 主席树 + 倍增

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 3749  Solved: 873[ ...

  2. 宿主机( win 7 系统) ping 虚拟机VMware( cent os 6.6 ) 出现“请求超时”或者“无法访问目标主机”的解决方法

    首先虚拟机的网络连接设置为"Host-only": 然后在 cmd 窗口中查看 VMnet1 的 ip 地址,这里是 192.168.254.1 接下来在 Linux 中设置网卡地 ...

  3. 【poj2234】 Matches Game

    http://poj.org/problem?id=2234 (题目链接) 题意 经典取火柴游戏 Solution 裸的Nim游戏,也就是取石子. 整个游戏的sg值为每一堆火柴(子游戏)的异或和. 代 ...

  4. 【uoj222】 NOI2016—区间

    http://uoj.ac/problem/222 (题目链接) 题意 有n个区间,当有m个区间有公共部分时,求m个区间长度的最大值与最小值之差的最小值. Solution 线段树+滑动窗口.这道题很 ...

  5. BZOJ1016 最小生成树计数

    Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同的 ...

  6. hihocoder #1285 智力竞赛

    传送门 总结: 1.仔细读题 2.仔细分析复杂度 3.不要想当然,乱下结论 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi.小Ho还有被小Hi强拉来的小Z,准备组队 ...

  7. POJ 2796 Feel Good

    传送门 Time Limit: 3000MS Memory Limit: 65536K Case Time Limit: 1000MS Special Judge   Description Bill ...

  8. a[1000][1000]程序崩溃

    1000 * 1000是大于65536的.如果不是需求需要,没必要开辟如此之多的空间.因为这些空间实在栈上申请的(如果是局部变量),栈的空间是有限的并且是宝贵的,所以呢,开辟太多的空间而不适用很可能会 ...

  9. C++处理一个动态规划的问题

    嗯哼,别人问的问题,看的我也头晕,百度了一下动态规划,看了看才想起来该怎么做,今天写了写代码,实现了~ 要求是递归,动态规划,想了想这种方法也是最简单的~ 所谓动态规划:把多阶段过程转化为一系列单阶段 ...

  10. --hdu 1231 最大连续子序列(动态规划)

    AC code: #include<stdio.h> int a[100005]; int main(void) { int n,i; int sum,maxn,tem,s,e,flag; ...