Rightmost Digit

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次冪的個位上的數;

任何兩個數相乘的最低位一定是它們最低位相乘所得結果的最低位,並且循環不會超過4次;


import java.util.Scanner;
public class PracticeIII {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int t,a,b,c,d;
long n;
t = sc.nextInt();
while((t--)>0){
n = sc.nextLong();
a = (int) (n%10);
b = a * a % 10;
c = b * a % 10;
d = c * a % 10;
switch((int)n % 4){
case 0 : 
System.out.println(d);
break;
case 1 :
System.out.println(a);
break;
case 2 :
System.out.println(b);
break;
case 3 :
System.out.println(c);
break;
}
}
}
}

發佈了50 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章