简单数学训练-B
来源:互联网 发布:海报设计制作软件 编辑:程序博客网 时间:2024/04/29 21:17
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).
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
234
Sample Output
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.
通常循环超时, 本题利用尾数循环规律。
#include <stdio.h>int main(){ int T; scanf("%d", &T); while (T--){ int n, num, op ; scanf("%d", &n); num = n % 10; int flag[10] ={1,1,4,4,2,1,1,4,4,2}; int i, p; if (n >= flag[num] && (n % flag[num]) ) p = n % flag[num]; else if (n >= flag[num] && !(n % flag[num])) p = flag[num]; else p = n;// printf("p = %d\n", p); op = 1; for ( i = 1; i <= p; i++){ if (op > 10){ op = op % 10; op *= num; } else op *= num; } op = op % 10; printf("%d\n", op); }}
快速幂带取余做法
#include <stdio.h>int fastpow(int x, int n,int mods) //快速幂带取余,非递归--不调用自身
{
int sum = 1; while (n > 0) { if (n & 1){ // n & 1 等价于 (n % 2) == 1 x = x % mods; sum *= x; sum = sum % mods; n-- ; } else { x = x % mods; x *= x; x = x % mods; n /= 2; // n >>= 1 等价于 n /= 2 } } sum = sum % mods; return sum;}int main(){ int n,a,b,ends; scanf("%d", &n); while (n--){ scanf("%d", &a); b = a; ends = fastpow(a,b,10); printf("%d\n", ends); }}
0 0
- 简单数学训练-B
- 简单数学训练-A
- 简单数学训练—C
- codeforces #230 (Div. 2) B(简单数学)
- 【简单题】【数学】-CF-397B-On Corruption and Numbers
- hdu 1576 A/B(数学:简单水题)
- [数学][第二阶段-简单数学题][HDOJ-2033]人见人爱A+B
- [数学][第二阶段-简单数学题][HDOJ-2075]A|B?
- [数学][第二阶段-简单数学题][HDOJ-2096]小明A+B
- Codeforces 570B Simple Game 概率求解,简单数学思维
- Codeforces 577 B Modulo Sum 简单数学+dp
- CodeForces 538B Quasi Binary(简单数学)
- codeforces-758-B Blown Garland(简单数学)
- 51nod 1632B君的连通(简单数学)
- 数学专题训练2
- 数学专题训练3
- 数学专题训练4
- 数学训练----数论Sum
- 自己实现线程的start、suspend、resume、stop方法
- jdbcTemplate配置文件
- 习题10-23 Hendrie序列 UVa10479
- JavaIO-字符流
- PADS学习之路09-PADS LOGIC创建原理图工程文件
- 简单数学训练-B
- Activity启动模式
- Testing Level - 读书笔记 ( 二 )
- JSTL+BeanUtils+分页+文件上传,sitemesh用来进行前端模板
- c++实现split()字符串切片
- 简单数学训练-A
- JavaIO-字节流和字符流的区别
- leetcode--Gas Station
- 解决sqlplus中输错命令时的删除、光标键不能使用问题