题目描述
Given a positive integer N, you should output the most right digit of N^N.
输入
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).
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
输出
For each test case, you should output the rightmost digit of N^N.
样例输入
2
3
4
样例输出
7
6
求n^n 次方的最后一位数,大概大家第一印象就是暴力循环吧,,但是有没有看到n可以到十亿,数据太大超出范围,效率也很低,这样循环显然是不行的,,其实这个是有规律的,即在n^n中最末尾的数是循环的,你可以发现是4一循环,,因此代码可以这样写
#include <stdio.h>
#include <math.h>
int main(){
int k, n, c, sum, m, i;
scanf("%d", &k);
for(i = 0; i < k; i++){
scanf("%d", &n);
c = n % 4;
if(c != 0){
sum = pow(n % 10, c);
m = sum % 10;
}
else{
sum = pow(n % 10, 4);
m = sum % 10;
}
printf("%d\n", m);
}
return 0;
}