题目链接:
问题描述:
给出一个整数N,输出N^N(N的N次方)的十进制表示的末位数字。
Input
一个数N(1 <= N <= 10^9)
Output
输出N^N的末位数字
Input示例
13
Output示例
3
思路:
先计算出结尾数字循环数组,再取余,定位最后一个数字
/*************************************************************************
> File Name: 1004.cpp
> Author: dulun
> Mail: dulun@xiyoulinux.org
> Created Time: 2016年03月31日 星期四 20时27分19秒
************************************************************************/
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define LL long long
using namespace std;
const int N = 50086;
int a[N];
int main()
{
int n;
cin>>n;
if(n == 0)
{
cout<<"1"<<endl;
return 0;
}
int k = n % 10;
if(k == 0)
{
printf("0\n");
return 0;
}
a[0] = k;
int cnt = 1;
while(1)
{
a[cnt] = a[cnt-1] * k % 10;
if(a[cnt] == k) break;
cnt++;
}
int p = (n-1) % cnt ;
cout<<a[p];
return 0;
}
计算N^N末位数字
本文介绍了一种计算N的N次方(N^N)的末位数字的方法。通过分析末位数字的循环规律,利用取余运算快速定位结果。适用于1<=N<=10^9范围内的整数。
902

被折叠的 条评论
为什么被折叠?



