51nod 1004 n^n的末位数字

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
 收藏
 关注
给出一个整数N,输出N^N(N的N次方)的十进制表示的末位数字。
Input
一个数N(1 <= N <= 10^9)
Output
输出N^N的末位数字
Input示例
13
Output示例
3


题意:中文

思路:找规律


根据规律表,会发现规律周期为4

所以列出规律表后直接给出答案,时间复杂度降到O(1)


#include <iostream>
#include<math.h>
#include<cstdio>
using namespace std;

int main()
{
    int s[10][4]={{0,0,0,0},{1,1,1,1},{2,4,8,6},{3,9,7,1},{4,6,4,6},{5,5,5,5},{6,6,6,6},{7,9,3,1},{8,4,2,6},{9,1,9,1}};
    int n;
    scanf("%d",&n);
    printf("%d
",s[n%10][(n-1)%4]);
    return 0;
}



原文地址:https://www.cnblogs.com/bryce1010/p/9387319.html