HDU 1013 Digital Roots

来源:互联网 发布:神盾局特工 知乎 编辑:程序博客网 时间:2024/04/29 19:10

题目链接: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;}


 

0 0
原创粉丝点击