HDU 1013 题 解

HDU 1013

[Title] [string] read
the title


Main Idea:

    正整数的数字根是通过对整数的数字求和而得出的。如果结果值是一个数字,那么该数字就是数字根。如果结果值包含两个或多个数字,则将这些数字相加并重复该过程。只要获得一位数字就可以继续进行。
    

Problem Solving Idea:

    模拟+字符串处理。
    

Summary:

这题有坑。。。他没说这个数字有多大,所以即使long long也可能爆掉,所以使用字符串(高精度)。

AC Code:

#include<bits/stdc++.h>
using namespace std;
char n[10000];
 int main()
 {
 
     int t,i,sum;
     while(cin>>n)
     {sum=0;
         if(strcmp(n,"0")==0)break;
        for(i=0;i<strlen(n);i++)
            sum=sum+n[i]-48;
         t=sum%9;
         if(t!=0)
         cout<<t<<endl;
         else cout<<"9"<<endl;
     }
     return 0;
 }

Guess you like

Origin www.cnblogs.com/Fhr2001/p/11988716.html