Code Jam 2017 Qualification Round Problem B. Tidy Numbers
来源:互联网 发布:订货软件 编辑:程序博客网 时间:2024/06/02 06:56
Problem B. Tidy Numbers
题意
定义 Tidy Numbers
是一个 10 进制非减数。如 8, 123, 555 和 224488 是 Tidy Numbers
,而 20, 321, 495 和 999990 不是。
问在 [1, N]
中最大的 Tidy Numbers
?
解法
迭代
对于 [1, x]
的最大 Tidy Number
,令截取 x 的前 k 位令为 Tidy Number
。若 Tidy Number
,补上后面若干位均为 9 即为最大值。若不是 Tidy Number
,迭代寻找 [1, x_k-1]
中最大的 Tidy Number
,再补若干位 9 。
代码
#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#include<fstream>using namespace std;long long n, init[20] = {1ll};bool check(long long x){ int pre = 9; while(x) { if(x%10 > pre) return false; pre = x % 10; x /= 10; } return true;}long long solve(long long x){ int num[20], idx = -1; while(x) num[++idx] = x%10, x/=10; long long ans = 0; for(int i=idx;i;--i) { ans *= 10; if(num[i] <= num[i-1]) ans += num[i]; else { ans += num[i]; ans--; if(!check(ans)) ans = solve(ans); return ans = ans * init[i] + init[i]-1; } } ans*=10; ans += num[0]; return ans;}int main(){ freopen("G:\\B.in", "r", stdin); freopen("G:\\B.out", "w", stdout); int T; scanf("%d",&T); for(int i=1;i<20;i++) init[i] = init[i-1]*10; for(int ica=1;ica<=T;ica++) { scanf("%I64d",&n); printf("Case #%d: %I64d\n", ica, solve(n)); }}
0 0
- Code Jam 2017 Qualification Round Problem B. Tidy Numbers
- 【google code jam Qualification Round 2017】【Tidy Numbers】【搜索】
- Google Code Jam 2010 Qualification Round 2012 Problem B && C
- Code Jam 2017 Qualification Round Problem A. Oversized Pancake Flipper
- Code Jam 2017 Qualification Round Problem C. Bathroom Stalls
- Code Jam 2017 Qualification Round Problem D. Fashion Show
- Google Code Jam Qualification Round 2014 Problem B. Cookie Clicker Alpha 题解
- Google Code Jam 2014 Qualification Round:Problem B. Cookie Clicker Alpha
- Google Code Jam 2015 Qualification Round: Problem B. Infinite House of Pancakes
- Google Code Jam Qualification Round 2015 Problem B. Infinite House of Pancakes
- Problem B. Tidy Numbers
- Google Code Jam 2014-Qualification Round-Problem C. Minesweeper Master
- Google Code Jam 2014 Qualification Round:Problem A. Magic Trick
- Google Code Jam 2015 Qualification Round: Problem A. Standing Ovation
- Google Code Jam 2015 Qualification Round: Problem C. Dijkstra
- Google Code jam Qualification Round 2015 --- Problem A. Standing Ovation
- Google Code Jam 2014 Qualification Round 2014 A B D
- Google Code Jam 2011 Qualification Round 资格赛 Problem B. Investing at the Market 问题B.市场投资
- 路径中 使用斜杠/和反斜杠\的区别
- [unity3D基础篇02]必备的3D数学基础1
- C++ #pragma pack()解析
- python中flask 常见问题
- Java异常 相关问题
- Code Jam 2017 Qualification Round Problem B. Tidy Numbers
- 篇章结构类型、层次及分析模式研究
- NGUI所见即所得之UITweener
- Linux命令中ssh远程登录
- 小安的字典
- 2.1 1973确定进制
- 集成学习
- okhttp-utils
- 回车和换行,以及不同软件对敲回车键的操作