hdoj 1061 Rightmost Digit

来源:互联网 发布:大国医郭三贴 淘宝网 编辑:程序博客网 时间:2024/04/30 15:21

思路:对于每个数字,得到i次方之后(i=1,2,3...),你会发现个位数的值是循环的。不管多大的数,n次方之后,个位数字只与个位数有关。

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;int rdigit[11][10] = {{0,0},{1,1},{1,2,4,8,6},{1,3,9,7,1},{1,4,6},{1,5},{1,6},{1,7,9,3,1},{1,8,4,2,6},{1,9,1}};int num[11] = {1,1,4,4,2,1,1,4,4,2};int main(){    int t, n, d, cnt;    scanf("%d", &t);    while (t--) {        scanf("%d", &n);        d = n%10;        cnt = n%num[d];        if(cnt == 0)            cnt = num[d];        printf("%d\n", rdigit[d][cnt]);    }    return 0;}


原创粉丝点击