您的位置:首页 > 其它

POJ 1519 解题报告

2015-05-16 05:39 375 查看
简单题。需要注意的是最开始的输入可能会很大,因而不能假设为整形。当然sum一下就是整形了。

thestoryofsnow1519Accepted148K0MSC++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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: