HDU OJ1061 Rightmost Digit

来源:互联网 发布:算法分析的两个方面是 编辑:程序博客网 时间:2024/05/17 23:12

HDU OJ1061 Rightmost Digit

Problem Description
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.

Sample Input
2
3
4

Sample Output
7
6

Hint
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.
In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.

法一:找规律,依次列出

这里需要注意
1】x的范围,所以定义为长整形__int64d输入,%I64d输出
2】输出时,sum的下标

#include <stdio.h>int main(){     __int64 x;    int n,y;    int sum[10][4]={{0},{1},{6,2,4,8},{1,3,9,7},{6,4},{5},{6},{1,7,9,3},{6,8,4,2},{1,9}};    scanf("%d",&n);    while(n--){        scanf("%I64d",&x);        y=x%10;        if(y==0||y==1||y==5||y==6)            printf("%d\n",sum[y][0]);        else if(y==4||y==9)            printf("%d\n",sum[y][x%2]);        else             printf("%d\n",sum[y][x%4]);     }       return 0;} 

快速幂取余可参看
http://www.cnblogs.com/PegasusWang/archive/2013/03/13/2958150.html

0 0