Rightmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 72194 Accepted Submission(s): 26684
Problem Description
Given a positive integer N, you should output the most right digit of N^N.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Output
For each test case, you should output the rightmost digit of N^N.
Sample Input
2
3
4
Sample Output
7
6
Hint
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.
In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
Author
Ignatius.L
题目简述:
给出数n,算出n的n次方的个位是什么。
题目分析:
快速幂很快就可以算出来。或者你也可以试一下我的zz方法(手动滑稽)
代码实现:
#include<bits/stdc++.h>
using namespace std;
int powq(int a,int b)
{
int sum=1;
while(b)
{
if(b&1)
{
sum*=a;
sum%=10;
}
a*=a;
a%=10;
b>>=1;
}
return sum;
}
int main()
{
int T,n,t;
long long sum;
scanf("%d",&T);
while(T--)
{
sum=1;
scanf("%d",&n);
printf("%d\n",powq(n%10,n));
}
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
int s2[4]={6,2,4,8};
int s3[4]={1,3,9,7};
int s4[2]={6,4};
int s7[4]={1,7,9,3};
int s8[4]={6,8,4,2};
int s9[2]={1,9};
int T,n,m,t;
cin>>T;
while(T--)
{
t=1;
scanf("%d",&n);
m=n%10;
if(m==1||m==5||m==6||m==0)
{
cout<<m<<endl;
continue;
}
if(m==2)
{
cout<<s2[n%4]<<endl;
continue;
}
if(m==3)
{
cout<<s3[n%4]<<endl;
continue;
}
if(m==4)
{
cout<<s4[n%2]<<endl;
continue;
}
if(m==7)
{
cout<<s7[n%4]<<endl;
continue;
}
if(m==8)
{
cout<<s8[n%4]<<endl;
continue;
}
if(m==9)
{
cout<<s9[n%2]<<endl;
continue;
}
}
}