ZOJ 1115 Digital Roots(简单,字符串与数)

题目

//好一道水水题,可是我居然也错了那么多次,后来百度来发现是因为数据数位可能很长很长,要用字符串数组。。。

//简单
//有坑啊——数据可能很大很大,要用字符串表示!


#include<stdio.h>
#include<string.h>
#include<algorithm>

using namespace std;

 int main()
 {
     char s[1010];
     while(scanf("%s",s)!=EOF)
     {
         if(strcmp(s,"0")==0)break;
         //无论如何,加一遍之后就不会超过int了,,
         int len=strlen(s);
         int i,n=0,m=0;
         for(i=0;i<len;i++)
         {
             n+=(s[i]-'0');
         }
         while(n%10!=n)
         {
             m=0;
             while(n)
             {
                 m+=(n%10);
                 n=n/10;
             }
             n=m;
         }
         printf("%d
",n);

     }
     return 0;
 }
View Code
一道又一道,好高兴!
原文地址:https://www.cnblogs.com/laiba2004/p/3813091.html