POJ 1519 解题报告
2015-05-16 05:39
375 查看
简单题。需要注意的是最开始的输入可能会很大,因而不能假设为整形。当然sum一下就是整形了。
thestoryofsnow | 1519 | Accepted | 148K | 0MS | C++ | 1088B |
/* ID: thestor1 LANG: C++ TASK: poj1519 */ #include <iostream> #include <fstream> #include <cmath> #include <cstdio> #include <cstring> #include <limits> #include <string> #include <vector> #include <list> #include <set> #include <map> #include <queue> #include <stack> #include <algorithm> #include <cassert> using namespace std; const int MAXN = 81; int cache[MAXN]; int summing(int n) { int s = 0; while (n > 0) { s += n % 10; n /= 10; } return s; } int recursion(int s) { assert(s < MAXN); int ret; if (cache[s] >= 0) { ret = cache[s]; } else if (s <= 9) { ret = s; } else { ret = recursion(summing(s)); } assert(0 <= ret && ret <= 9); cache[s] = ret; return ret; } int main() { memset(cache, -1, sizeof(cache)); char str[2000]; while (scanf("%s", str) == 1) { if (strcmp(str, "0") == 0) { break; } int n = 0; for (int i = 0; str[i] != '\0'; ++i) { n += str[i] - '0'; } int s = summing(n); printf("%d\n", recursion(s)); } return 0; }
相关文章推荐
- POJ - 3264 Balanced Lineup解题报告(RMQ问题 ST算法 魔板题)
- POJ 3278解题报告(C语言版)//Catch That Cow
- poj1003 Hangover 解题报告
- POJ 3280 解题报告
- 无偏博弈(Impartial Game) 及几道相关的 POJ 解题报告
- POJ1753 解题报告
- poj解题报告——1002
- POJ 1001(浮点数高精度幂) 解题报告
- poj 2593 & poj 2479解题报告
- POJ 2240 解题报告
- POJ 1006(数论:同余式) 解题报告
- POJ 1008(映射) 解题报告
- POJ 1004Financial Management解题报告——【PKU ACM】
- POJ 1047——解题报告
- poj解题报告——1250
- POJ 2305 解题报告
- poj 2262 解题报告
- 【解题报告】 POJ 2318 TOYS -- 判断点在某个梯形内 叉积 + 二分
- POJ1247解题报告——我写的第一篇解题报告
- 区间第K大数——划分树(POJ2104解题报告)