hdu1013(Digital Roots)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxlen = 10000;
char a[maxlen];
//Accepted	1013	0MS	244K	630 B	C++	Achiberx

int get(int a) {
    int s=0;
    while(a) {
        s += a%10;
        a /= 10;
    }
    if(s>9) {
        return get(s);
    }
    else {
        return s;
    }
}

int work() {
    int len = strlen(a);
    int s = 0;
    for(int i = 0; i < len; i++) {
        int tmp = a[i] - '0';
        s += tmp;
    }
    return get(s);
}

int main() {
    while(scanf("%s", a) != EOF) {
      if(a[0] == '0') break;
      int res = work();
      printf("%d\n", res);
    }
    return 0;
}

简单的水题,注意一下,正整数可能会很多远远超过int。。。

你可能感兴趣的:(hdu1013(Digital Roots))