题目链接

http://acm.hdu.edu.cn/showpro...

完整源码

#include <stdio.h>
#include <math.h>

int main() {
    int a[10] = {1, 1, 4, 4, 2, 1, 1, 4, 4, 2};
    int n, num, rmd, ans; // rmd = rightmost digit
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &num);
        rmd = num % 10;
        ans = (int) pow(rmd, num % a[rmd] ? num % a[rmd] : a[rmd]);
        printf("%d\n", ans % 10);
    }
}

简单解释

int a[10] = {1, 1, 4, 4, 2, 1, 1, 4, 4, 2}

个位为0的数字,1次为循环节;
个位为1的数字,1次为循环节;
个位为2的数字,4次为循环节;
以此类推。

ans = (int) pow(rmd, num % a[rmd] ? num % a[rmd] : a[rmd]);

个位是rmd,需要做num % a[rmd]次运算,但如果num % a[rmd]为0则不对,因此用问号表达式处理为0的情况。


KoreyLee
27 声望6 粉丝

Those who were seen dancing were thought to be insane by those who could not hear the music.


引用和评论

0 条评论