hdoj-1061-Rightmost Digit

来源:互联网 发布:js 定义对象数组 编辑:程序博客网 时间:2024/05/16 08:12

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
234
 

Sample Output
76
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.

水题水题。题意就是求n^n最右边的那一位数。

仔细一想可以发现,求最后一位完全只和最后一位有关系。有的就是本身,而有的则有循环节。

#include<cstdio>#include<cstring>#include<iostream>using namespace std;int main(){    int t,digit,r,a;    scanf("%d",&t);    while(t--)    {     scanf("%d",&digit);     r=digit%10;     if(r==1){        printf("1\n");     }     else if(r==2){         a=digit%4;         if(a==0) printf("6\n");         else if(a==2)  printf("4\n");     }     else if(r==3){        a=digit%4;        if(a==3) printf("7\n");        else if(a==1)  printf("3\n");     }     else if(r==4){            printf("6\n");     }     else if(r==5){        printf("5\n");     }     else if(r==6){        printf("6\n");     }     else if(r==7){            a=digit%4;     if(a==1)  printf("7\n");     else if(a==3) printf("3\n");     }     else if(r==8){        a=digit%4;        if(a==0) printf("6\n");        else if(a==2)  printf("4\n");     }     else if(r==9){        printf("9\n");     }     else if(r==0){        printf("0\n");     }    }}


0 0
原创粉丝点击