现在的位置: 首页 > 综合 > 正文

杭电1061

2014年09月05日 ⁄ 综合 ⁄ 共 1052字 ⁄ 字号 评论关闭

                                                 Rightmost Digit

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

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
 
 
这是我用递归写的此题,代码如下:
#include <stdio.h>
int main()
{
	int f(int n,int s);
    int n,m,s,x,j;
    scanf("%d",&m);
    while(m--)
    {
		x=0;
       scanf("%d",&n);
	   s=n%10;
	   j=f(n,s);
	   printf("%d\n",j);
    }
    return 0;
}
   
    int f(int n,int s)
	{
		int j;
		if(n==1)
		{
			return (s);
		}else if(n==2)
		{
			return (s*s%10);
		}else
		{
			if(n%2==0)
			{
				j=f(n/2,s);
				j=j*j;
				j=j%10;
			}else
			{
				j=f(n/2,s);
				j=j*j*s;
				j=j%10;
			}
		}
		return (j);
	}
				

抱歉!评论已关闭.