BestCoder13 1001.Beautiful Palindrome Number(hdu 5062) 解题报告
题目链接: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) 解题报告的更多相关文章
- BestCoder24 1001.Sum Sum Sum(hdu 5150) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5150 题目意思:就是直接求素数. 不过 n = 1,也属于答案范围!!只能说,一失足成千古恨啊---- ...
- BestCoder10 1001 Revenge of Fibonacci(hdu 5018) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5018 题目意思:给出在 new Fibonacci 中最先的两个数 A 和 B(也就是f[1] = A ...
- HDU 5062 Beautiful Palindrome Number(数学)
主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can re ...
- BestCoder8 1001.Summary(hdu 4989) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4989 题目意思:给出 n 个数,然后将这些数两两相加,得到 n*(n-1) /2 对和,把重复的和去掉 ...
- BestCoder19 1001.Alexandra and Prime Numbers(hdu 5108) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5108 题目意思:给出一个数正整数 N,N <= 1e9,现在需要找出一个最少的正整数 M,使得 ...
- BestCoder17 1001.Chessboard(hdu 5100) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5100 题目意思:有一个 n * n 的棋盘,需要用 k * 1 的瓷砖去覆盖,问最大覆盖面积是多少. ...
- BestCoder12 1001.So easy(hdu 5058) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5058 (格式有点问题,为了方便阅读---整个复制下来吧) 题目意思:给出两个长度都为 n 的集合你,问 ...
- BestCoder7 1001 Little Pony and Permutation(hdu 4985) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4985 题目意思:有 n 个数,对于第 i 个数给出 σ(i) 的值.求出互不相交的循环的个数,并输出每 ...
- BestCoder3 1001 Task schedule(hdu 4907) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4907 题目意思:给出工作表上的 n 个任务,第 i 个任务需要 ti 这么长的时间(持续时间是ti ~ ...
随机推荐
- Cocos2d-X3.0 刨根问底(七)----- 事件机制Event源码分析
这一章,我们来分析Cocos2d-x 事件机制相关的源码, 根据Cocos2d-x的工程目录,我们可以找到所有关于事件的源码都存在放在下图所示的目录中. 从这个event_dispatcher目录中的 ...
- 从TP、FP、TN、FN到ROC曲线、miss rate、行人检测评估
从TP.FP.TN.FN到ROC曲线.miss rate.行人检测评估 想要在行人检测的evaluation阶段要计算miss rate,就要从True Positive Rate讲起:miss ra ...
- 洛谷P1363 幻想迷宫
题目描述 背景 Background (喵星人LHX和WD同心协力击退了汪星人的入侵,不幸的是,汪星人撤退之前给它们制造了一片幻象迷宫.) WD:呜呜,肿么办啊…… LHX:momo...我们一定能走 ...
- TCP/IP Four Layer Protocol Format Learning
相关学习资料 tcp-ip详解卷1:协议.pdf 目录 . 引言 . 应用层 . 传输层 . 网络层 0. 引言 协议中的网络字节序问题 在学习协议格式之前,有一点必须明白,否则我们在观察抓包数据的时 ...
- SPOJ Pouring Water
传送门 POUR1 - Pouring water #gcd #recursion Given two vessels, one of which can accommodate a litres o ...
- iBatisnet系列(二) 配置运行环境和日志处理
http://hjf1223.cnblogs.com/archive/2006/04/24/383119.aspx 刚爬完鼓山回来,想到这篇刚刚开始,不敢怠慢,洗完澡休息一下就到电脑旁边来了.现在我开 ...
- Servlet------(声明式)异常处理
Test.java 其他方法不变,重写 protected void service()方法 public void init(ServletConfig config) throws Servlet ...
- Cannot attach the file as database 'membership'.
Cannot attach the file 'D:\GitHome\cae\CAE\App_Data\membership.mdf' as database 'membership'. 说明: 执行 ...
- 如何有效的保护 JAVA 程序
从头到尾保护 JAVA 目前关于 JAVA 程序的加密方式不外乎 JAVA 模糊处理(Obfuscator)和运用 ClassLoader 方法进行加密处理这两种方式(其他的方式亦有,但大多是这两种的 ...
- App架构设计经验谈:服务端接口的设计
App与服务器的通信接口如何设计得好,需要考虑的地方挺多的,在此根据我的一些经验做一些总结分享,旨在抛砖引玉. 安全机制的设计 现在,大部分App的接口都采用RESTful架构,RESTFul最重要的 ...