HDU-1061-Rightmost Digit
2016-07-11 22:41
579 查看
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)打表法
2)用switch语句
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)打表法
#include<iostream> using namespace std; int main() { int a[10][2] = { {0,0},{1,1},{4,6},{7,3},{6,6},{5,5},{6,6},{3,7},{6,4},{9,9} }; int T, n; cin >> T; while (T--) { cin >> n; int x, y; x = n % 10;//个位上的数 y = (n / 10)%2;//十位上的数是否为偶数 cout << a[x][y] << endl; } return 0; }
2)用switch语句
#include<iostream> using namespace std; int main() { int T, n; cin >> T; while (T--) { cin >> n; int num, a, b; a = n % 10; b = n / 10; switch (a) { case 0: case 1: case 5: case 6: case 9: num = a; break; case 2: if (b % 2 == 0) num = 4; else num = 6; break; case 3: if (b % 2 == 0) num = 7; else num = 3; break; case 4: num = 6; break; case 7: if (b % 2 == 0) num = 3; else num = 7; break; case 8: if (b % 2 == 0) num = 6; else num = 4; break; } cout << num << endl; } return 0; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C++的template模板中class与typename关键字的区别分析
- C与C++之间相互调用实例方法讲解