现在的位置: 首页 > 综合 > 正文

HDOJ1013 Digital Roots

2016年09月28日 ⁄ 综合 ⁄ 共 234字 ⁄ 字号 评论关闭

原题链接

略坑的一道题。

#include <stdio.h>
char str[1000];

int f(int n){
    int s = 0;
    while(n){
        s += n % 10;
        n /= 10;
    }
    return s;
}

int main(){
    int n, s;
    while(scanf("%s", str), str[0] - '0'){
        s = 0;
		for(int i = 0; str[i] != '\0'; ++i)
			s += str[i] - '0';
        while(s > 9)
            s = f(s);
        printf("%d\n", s);
    }
    return 0;
}

抱歉!评论已关闭.