关闭

杭电1061

485人阅读 评论(0) 收藏 举报

                                                 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);
	}
				


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:264036次
    • 积分:6132
    • 等级:
    • 排名:第4189名
    • 原创:337篇
    • 转载:0篇
    • 译文:0篇
    • 评论:10条
    文章分类
    最新评论