Digital Roots
For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.
假設,數d的根爲d%9( 暫時不取0,整除時取9)
當d < 10時,1~9這9個數肯定成立;
當d >= 10時,d的根爲d%9 = (d-1)%9+1,即d的前一個數的數根加1.
得證.
<strong>#include<cstdio>
int main(void)
{
while(1)
{
int num=0;
char ch;
while(scanf("%c",&ch) && ch!='\n') num += ch-'0';
if(!num) return 0;
else if(num%9 == 0) puts("9");
else printf("%d\n",num%9);
}
return 0;
}</strong>
<span style="font-size:18px;">#include<cstdio>
int main(void)
{
int n,i,ans;
while(scanf("%d",&n),n)
{
ans = 1;
for(i = 0; i < n; i++)
ans = ans*n%9;
if(!ans) puts("9");
else printf("%d\n",ans);
}
return 0;
}</span>