Rightmost Digit
2015-11-28 21:55
441 查看
[align=left]Problem Description[/align]
Given a positive integer N, you should output the most right digit of N^N.
[align=left]Input[/align]
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).
[align=left]Output[/align]
For each test case, you should output the rightmost digit of N^N.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
求结果的最后一位,快速幂加同余定理(%10),以前做过类似的题。。。
Given a positive integer N, you should output the most right digit of N^N.
[align=left]Input[/align]
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).
[align=left]Output[/align]
For each test case, you should output the rightmost digit of N^N.
[align=left]Sample Input[/align]
2 3 4
[align=left]Sample Output[/align]
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.
求结果的最后一位,快速幂加同余定理(%10),以前做过类似的题。。。
#include<stdio.h> int f(int a,int b) { int ans=1; a=a%10; while(b>0) { if(b&1) ans=(ans*a)%10; b= b>>1; a=(a*a)%10; } return ans; } int main() { int n,t; int result; scanf("%d",&t); while(t--) { scanf("%d",&n); result=f(n,n); printf("%d\n",result); } return 0; }
相关文章推荐
- Linux下的磁盘分割和文件系统
- hdoj--1556--Color the ball(模拟&&树状数组)
- CentOS Linux 监控安装之Zabbix
- 【HTML5+css3】学习笔记之实体、元数据和全局属性
- BestCoder Round #64 (div.2)
- 爬取豆瓣网电影信息
- hdoj 绝对值排序
- Visual Studio 2015 快捷键(C++篇可通用)
- I/0端口有关的寻址方式
- 【HTML5+css3】学习笔记之表单元素
- 使用鼠标在面板上移动消息
- NodeJS中使用benchmark进行基准测试-@alsotang
- 进程状态之间的转换*小记【操作系统】
- C语言运算符小结
- ACM学习历程—HDU5586 Sum(动态规划)(BestCoder Round #64 (div.2) 1002)
- 【HTML5+css3】学习笔记之音频元素和视频元素
- IOS之保存登录状态
- 《学会提问:批判性思维指南》
- Android 的 Binder 机制概念介绍
- iOS开发:国际化之app支持多种语言切换