1410231608-hd-Rightmost Digit

Rightmost Digit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33040    Accepted Submission(s): 12651


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.
题目大意
要求数N的N次方的最右边一位。
解题思路
一开始用直接模拟结果超时,后来觉得应该有规律,错用n%=10,发现不对。再后来将所有数据打出来之后发现20才是一个循环节。注意:当循环节20的时候要注意特殊判断。
代码
#include<stdio.h>
int main()
{
	int t,n;
	int i,j,k;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
	//把数据全部打出来会发现20是一个循环 
		if(n%20==0)
		    k=0;//分循环的时候注意循环节 
		else
		{
		    n%=20;
		    k=1;
		    for(i=0;i<n;i++)
		    {
			    k*=n;
			    if(k>9)
			        k%=10;
		    }
		}
		printf("%d\n",k);
	}
	return 0;
}

你可能感兴趣的:(1410231608-hd-Rightmost Digit)