HDU 1061 Rightmost Digit
来源:互联网 发布:霍金的宇宙知乎 编辑:程序博客网 时间:2024/06/05 17:19
Problem:
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.
Input Example:
234
Output Example:
76
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.本题考取算法为:快速幂取模
Code:
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); while (n-- > 0) { int x = sc.nextInt(); System.out.println(f(x, x, 10)); } sc.close(); } static int f(int a, int b, int c) { int ans = 1; a = a % c; while (b > 0) { if (b % 2 == 1) { ans = (ans * a) % c; } b = b / 2; a = (a * a) % c; } return ans; }}
阅读全文
0 0
- HDU 1061 Rightmost Digit
- hdu 1061 Rightmost Digit
- (hdu 1061)Rightmost Digit
- HDU 1061 Rightmost Digit
- HDU 1061 Rightmost Digit
- hdu rightmost digit 1061
- HDU 1061 Rightmost Digit
- HDU 1061 Rightmost Digit
- hdu 1061 Rightmost Digit
- HDU 1061 Rightmost Digit
- HDU 1061 Rightmost Digit
- HDU 1061 Rightmost Digit
- hdu 1061 Rightmost Digit
- HDU 1061 Rightmost Digit
- hdu 1061 Rightmost Digit
- hdu 1061 Rightmost Digit
- HDU 1061 Rightmost Digit
- HDU-1061-Rightmost Digit
- 集训第十八天(2017/8/17)看树状数组+第一次组队打比赛
- Banner(图片轮播)
- Eclipse项目如何切换成AS项目
- Boost库——signals2的用法总结
- HLS中SVD解最小二乘
- HDU 1061 Rightmost Digit
- hdu 6143 Killer Names 第二类斯特灵数 组合数学
- LeetCode-142 Linked List Cycle II
- Linux下HOOK动态链接库中API的方法
- spring
- lua读取CSV文件
- 《软件架构师的12项修炼》学习心得(二)
- 对称加密和非对称加密
- Android上的Notification