Eddy's digital Roots
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4939 Accepted Submission(s): 2765
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.
The Eddy's easy problem is that : give you the n,want you to find the n^n's digital Roots.
2 4 0
4 4
JGShining | We have carefully selected several similar problems for you: 1164 1157 1194 1141 1178
分析:本题有两个重要的考点,一是九余数定理,二是快速幂取余。
九余数定理:1.一个数对九取余,得到的数称之为九余数;
2.一个数的九余数等于它的各个数位上的数之和的九余数!
代码一(九余数+快速幂):
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int fastmod(int a,int b,int k)
{
int i,j,ans=1;
a=a%k;
while(b)
{
if(b%2==1)
ans=ans*a%k;
b/=2;
a=a*a%k;
}
return ans;
}
int main()
{
int n,i;
int m;
while(scanf("%d",&n)!=EOF&&n)
{
m=1;
m=fastmod(n,n,9);
if(m)
printf("%d\n",m);
else
printf("9\n");
}
return 0;
}
代码2(九余数):
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int n,i;
int m;
while(scanf("%d",&n)!=EOF&&n)
{
m=n;
for(i=2;i<=n;i++)
{
m=(m*n)%9;
}
if(m)
printf("%d\n",m);
else
printf("9\n");
}
return 0;
}
代码3(规律):
include<stdio.h>
int main()
{
int n;
char s[20]="149429719159479789";
while(~scanf("%d",&n),n)
printf("%c\n",s[(n-1)%18]);
return 0;
}