百练 2764:数根 【九余数定理】

题目

这道题不能用int 来存,毕竟101000太大

如果一般的来做的话如下:

#include<stdio.h>
int main()
{
   char c;
   int digitroot =0,sum,flag=0;
     while((c = getchar()) != '
' && c != EOF)
    {
        digitroot += c - '0';
        sum = 0;
        while(digitroot)
        {
            sum+=digitroot%10;
            digitroot=digitroot/10;
            flag = 1;
        }
        if(flag == 1)
           {
                digitroot = sum;
                flag = 0;
           }
    }
    printf("%d
",digitroot);
    return 0;
}

如何能够想到就九余数定理话,那就。。

#include<stdio.h>
int main()
{
   char c;
   int digitroot =0;
     while((c = getchar()) != '
' && c != EOF)
    {
        digitroot += c - '0';
        digitroot = digitroot%9;
    }
    if(digitroot==0)
        printf("9
");
    else
        printf("%d
",digitroot);
    return 0;
}
原文地址:https://www.cnblogs.com/qie-wei/p/10160211.html