【51nod】1004 n^n的末位数字

1004 n^n的末位数字 

 

 

题解:

由于题目是问末尾数字,所以我们可以直接对n模10,用最后一位数字进行运算。

我们可以列一个表格观察末尾数字自身乘积变化。

0000000000
1111111111
2486248624
3971397139
4646464646
5555555555
6666666666
7931793179
8426842684
9191919191

根据观察,末尾数字自身乘积最多4次,其所得积末尾数字就变回本身。

很快,最先想到的就是直接用二维数组存放。

#include <cstdio>
#include <cmath>
using namespace std;
int n;
int ans[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 main(){
    scanf("%d", &n);
    printf("%d", ans[n%10][(n-1)%4]);
//注意,由于数组跟取模的原因,这里用(n-1)%4而不是直接n%4
    return 0;
}

或者直接敲代码

#include <cstdio>
#include <cmath>
using namespace std;
int n, temp;
int main(){
    scanf("%d", &n);
    int temp = n % 4;
    if(temp==0)
        temp = 4;
    n %= 10;
    int ans = pow(n, temp);
    printf("%d", ans%10);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值