nBOJ8 - Rightmost Digit
Accept:169 | Submit:441 |
Time Limit:1000MS | Memory Limit:65536KB |
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.
其实这道题真的没什么好说…我觉得暴力都可以。
但是还是找了一下规律:(以x结尾的乘以x后的位数变化)
0->0
1->1
2->4,8,6,2
3->9,7,1,3
4->4,6
5->5
6->6
7->9,3,1,7
8->4,2,6,8
9->1,9
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
int number[10][4] = {
{0,0,0,0},
{1,1,1,1},
{6,2,4,8},
{1,3,9,7},
{6,4,6,4},
{5,5,5,5},
{6,6,6,6},
{1,7,9,3},
{6,8,4,2},
{1,9,1,9}
};
int main(void)
{
int t,n;
scanf("%d",&t);
for(int i(0);i != t;++i)
{
scanf("%d",&n);
printf("%d\n",number[n%10][n%4]);
}
return 0;
}
其实还有个规律,是AC后发现的。
见这里:http://www.cnblogs.com/jbelial/archive/2011/08/04/2127716.html
rightmost digit是以20为周期的。