您的位置:首页 > 编程语言 > C语言/C++

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)打表法

#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++ ACM 打表