题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1013
题 意:如24,找到他的各位数之和为6,若大于9,则继续计算如39得到12再到3.
思 路:找规律,每九个一个循环 0 1 2 3 4 5 6 7 8 9 10 11 12 13 ......... 100 101 102 103 ....
0 1 2 3 4 5 6 7 8 9 1 2 3 4 .......1 2 3 4....
代码如下:
#include <iostream>
using namespace std;
#include <string.h>
#include <stdio.h>
#include <cmath>
#include <algorithm>
int main()
{
char str[2000];
while( scanf ( "%s", str ) != EOF )
{
if( str[0] == '0' ) break;
int ans = 0;
for( int i = 0 ; str[i] != '\0'; i ++ )
ans+=(str[i]-'0');
int p = ans%9;
if( p == 0 ) printf("9\n");
else printf("%d\n",p);
}
return 0;
}