杭電HDOJ 1061 解題報告

Rightmost Digit

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


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

解題思路:

方案1、每次取末位相乘。(該方法代碼就不貼了,學過c的基本都會寫吧,小心超時哦!當然>10應該先只取末位,而且要知道他是有規律的(跟4有關不多說了),代碼簡單)

方案2、找規律。

方案3、取對數,思路類似1060。

{有一個常規的方法是利用對數
可以令n^n=A*10^L A的範圍是(0,1),L是n^n計算出來後的位數
L的計算方法是對n^n取10的對數在取整,即L=[nlgn]
那麼n^n=A*10^([nlgn])
這樣之後,兩邊同時對10取對數
得到:nlgn=lgA+[nlgn]
於是乎lgA=nlgn-[nlgn]
A=10^(nlgn-[nlgn])
由於A是(0,1),此時只需計算[A*10]即可。

摘自http://blog.csdn.net/creazierhit/article/details/7787249

這貌似連樣例都過不了,大家也可以試試。有用方案3,AC的可以@我。


代碼2:(亮爆你的雙眼)

#include<stdio.h>
#include<string.h>
main()
{
	int n,i,t,s,l,m;
	char a[12];

	scanf("%d",&n);
	getchar();
	for(i=0;i<n;i++)
	{
		gets(a);
		l=strlen(a);
		t=a[l-1]-48;
		switch(t){
		case 1: s=1;break;
		case 5: s=5;break;
		case 6: s=6;break;
		case 0: s=0;break;
		case 2:	if(l>1) m=t+(a[l-2]-48)*10;
			else m=t;
			m%=4;
			switch (m){
			case 0:s=6;break;
			case 1:s=2;break;
			case 2:s=4;break;
			case 3:s=8;break;
			}break;
		case 3:if(l>1) m=t+(a[l-2]-48)*10;
			else m=t;
			m%=4;
			switch (m){
			case 0:s=1;break;
			case 1:s=3;break;
			case 2:s=9;break;
			case 3:s=7;break;
			}break;
		case 4:m=t;
			m%=2;
			switch (m){
			case 0:s=6;break;
			case 1:s=4;break;
			}break;
		case 7:
			if(l>1) m=t+(a[l-2]-48)*10;
			else m=t;
			m%=4;
			switch (m){
			case 0:s=1;break;
			case 1:s=7;break;
			case 2:s=9;break;
			case 3:s=3;break;
			}break;
		case 8:	if(l>1) m=t+(a[l-2]-48)*10;
			else m=t;
			m%=4;
			switch (m){
			case 0:s=6;break;
			case 1:s=8;break;
			case 2:s=4;break;
			case 3:s=2;break;
			}break;
		case 9:m=t;
			m%=2;
			switch (m){
			case 0:s=1;break;
			case 1:s=9;break;
			}break;
		}
		printf("%d\n",s);
	}
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章