【杭电oj】1061 - Rightmost Digit(打表)
2016-01-16 19:05
489 查看
Rightmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 43080 Accepted Submission(s): 16192
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.
Author
Ignatius.L
需要观察规律,然后跟着打表就完了,不是很难。
首先观察0-9的n次方的最后一位:2,3,7,8,9 是4次方一个循环;4,9是二次方一循环;0,1,5,6末尾不变。
这里我提个建议,4,9虽然是2次方一循环,但是建议还是按照4次一循环来处理,这样不容易因为打代码时粗心而出bug,就像我,急着出去,结果WA了两次。
代码如下:
#include <stdio.h> int num[10][5]= { {0}, {0,1}, {0,2,4,8,6}, {0,3,9,7,1}, {0,4,6}, {0,5}, {0,6}, {0,7,9,3,1}, {0,8,4,2,6}, {0,9,1}, }; int main() { // for (int i=0;i<10;i++) // { // for (int j=0;j<5;j++) // { // printf ("%d ",num[i][j]); // } // printf ("\n"); // } int u; int n; int m; //n的末尾 scanf ("%d",&u); while (u--) { scanf ("%d",&n); m=n%10; if (m==0) { printf ("0\n"); } else if (m==1) { printf ("1\n"); } else if (m==2) { int t=n%4; if (t==0) t=4; printf ("%d\n",num[2][t]); } else if (m==3) { int t=n%4; if (t==0) t=4; printf ("%d\n",num[3][t]); } else if (m==4) { int t=n%2; if (t==0) t=2; printf ("%d\n",num[4][t]); } else if (m==5) { printf ("5\n"); } else if (m==6) { printf ("6\n"); } else if (m==7) { int t=n%4; if (t==0) t=4; printf ("%d\n",num[7][t]); } else if (m==8) { int t=n%4; if (t==0) t=4; printf ("%d\n",num[8][t]); } else if (m==9) { int t=n%2; if (t==0) t=2; printf ("%d\n",num[9][t]); } } return 0; }
相关文章推荐
- [乡土民间故事_徐苟三传奇]第卅三回_考学问哄走夺馆人
- Swift学习之continue、break、fallthrough
- volley加载图片 及imageloader使用
- 苹果审核通过后包体大了200多MB!都是Xcode 7的错?
- 数据库中增加操作insert into的用法和查询select的用法
- 链表结点移动
- 机器学习系列(4)_机器学习算法一览,应用建议与解决思路
- Android 如何抓取开机Log
- Servlet的线程安全
- qt嵌入式版本源码编译的各种问题
- Java基础(五)——final修饰符
- hbase安装(使用已经安装的HDFS和zookeeper)
- [乡土民间故事_徐苟三传奇]第卅二回_小财东学乖闹笑话
- 如何快速研究透一篇 Paper 并提出有价值的问题?
- c语言的数据链路层和冒泡法的一些知识
- 一种单例的实现方法
- 深入分析JavaWeb 技术内幕
- 戒不掉
- UNP第四章基本TCP编程-笔记
- 途牛谭俊青:多数据中心状态同步&两地三中心的理论