题意:求N^N最右端的数。
快速幂递归:
#include<iostream>
using namespace std;
int t,n;
int f(int m,int n)
{
int ans;
if(n==1) return m%10;
if(n%2==0)
{ ans=f(m,n/2) ;return ((ans%10)*(ans%10))%10;}
else
{ ans=f(m,n/2); return ((ans%10)*(ans%10)*(m%10))%10;}
}
int main()
{
cin>>t;
while(t--)
{
cin>>n;
int m=n;
cout<<f(m,n)<<endl;
}
return 0;
}
快速幂非递归:
#include <iostream>
using namespace std;
int rmd(int a, int b)
{
a = a%10;
int reslut = 1;
while (b != 0)
{
if (b&1)
{
reslut = (reslut*a)%10;
}
b = b>>1;
a = (a*a)%10;
}
return reslut;
}
int main()
{
int N;
int num;
cin >> N;
while (N != 0)
{
cin >> num;
cout << rmd(num, num) << endl;
--N;
}
return 0;
}
找规律:
#include<stdio.h>
int main()
{
int num[21]={0,1,4,7,6,5,6,3,6,9,0,1,6,3,6,5,6,7,4,9,0};
int n;
int num2;
while(scanf("%d",&n)>0)
{
for(int i=0;i<n;i++)
{
scanf("%d",&num2);
printf("%d\n",num[num2%20]);
}
}
return 0;
}