HDU1013 Digital Roots

 

http://acm.hdu.edu.cn/showproblem.php?pid=1013

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

int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        int sum=0;
        while(true){
           while(n>0){
                sum+=n%10;
                n/=10;
            }
            if(sum>10)
            {
                 n=sum;sum=0;
            }
            else{
                break;
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

就是wrong就是wrong.....是何原因?是何原因??

感谢http://blog.csdn.net/rongyongfeikai2/article/details/7588273让我快速意识到,大数陷阱

#include "iostream"
#include "cstdio"
#include "string"
using namespace std;

int main()
{
    string str;
    while(cin>>str&&str!="0")
    {
        int len=str.length();
        int num=0;
        for(int i=0;i<len;i++){
            num+=str[i]-'0';
            if(num>9){num=num/10+num%10;}
        }
        cout<<num<<endl;
    }
    return 0;
}

 

posted @ 2017-03-07 23:37  kimsimple  阅读(129)  评论(0编辑  收藏  举报