HDU 1061 (51Nod 1004 )n^n的末位数字

题目链接:

1004 n^n的末位数字

问题描述:

给出一个整数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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值