51Nod--1004 n^n的末位数字

来源:互联网 发布:手机韩剧网下载软件 编辑:程序博客网 时间:2024/05/18 01:13

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1004

Input
一个数N(1 <= N <= 10^9)
Output
输出N^N的末位数字
Input示例
13
Output示例
3

N^N 相当于N个N相乘,其末尾数只和N 的末尾数有关,N的尾数只有0~9,这里注意下,0~9 的阶乘末尾数 4 位一循环,第 0 次等于第 4 次的值,拿 2 这个数字来说:2^0=1, 2^1 = 2, 2^2 = 4, 2^3= 8, 2^4 = 16 , 2^5=32, 2^6 = 64, 2^7 = 128, 2^8=256, 2^9 = 512。。。

当指数为 0 的时候,末尾数的值等于指数为 4 的时候的末尾数的值:
下面是代码

#include <iostream>#include <cmath>using namespace std;int main() {        int n, m;    cin >> n;    m = n%10;    n = n%4+4;    cout << ((int)pow(m,n))%10<<endl;    return 0;}
原创粉丝点击