A - Rightmost Digit

https://vjudge.net/contest/387828#problem/A
Given a positive integer N, you should output the most right digit of N^N.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Output
For each test case, you should output the rightmost digit of N^N.
分析:这道题可以有很多做法,快速幂,数组维护,或者个位数法
要注意的是,一定要确切分析情况
该题可以形成规律:
0,1,5,6为n的个位时 输出一定为0,1,5,6
其余情况可发现
2 4 8 6 2 4 8 6.。。。
3 3 9 7 1 3 9 7 1.。。
4 6 4 6 4 6.。。。
7 7 9 3 1 7 9 3 1.。。。
8 8 4 2 6 8 4 2 6.。。。
9 9 1 9 1.。。
由此可以发现其余的均可视为4个一循环,因此可以提前建好一个ans数组去保存结果,这样效率最高
需要注意的是:赋值时令二维数组从1开始,因此当n%10为4的倍数时,结果会为0,这时应做一次判断将数组变为4jiike(我因为这个WA了5次。。。)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
int ans[15][10];
void solve()
{
    int i,j,t,sum;
    for(i=1;i<=9;i++)
    {
        sum=1;
        t=i;
        for(j=1;j<=4;j++)
        {
            sum=(sum*t)%10;
            ans[i][j]=sum%10;
        }
    }
}
int main()
{

    int n,i,j,t1,t2,sum,T;
    solve();
    /*
    for(i=0;i<=9;i++)
    {
        for(j=1;j<=4;j++)
        {
            printf("%d ",ans[i][j]);
        }
        printf("\n");
    }
    */


    while(~scanf("%d",&T))
    {
        while(T--)
        {
            scanf("%d",&n);
            t1=n%10;
            t2=n%4;
            if(t2==0) t2=4;
            printf("%d\n",ans[t1][t2]);
        }
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值