Rightmost Digit
来源:互联网 发布:java接口开发demo 编辑:程序博客网 时间:2024/05/21 22:44
对于我写的程序而言,这一题还是比较简单的,只要能够想到解题思路即可。但是我觉得应该有更简单更高级的算法,只不过现在还没想到,继续奋斗吧
不管如何,这个一题是ac了,心里还是比较高兴的。
问题 B: RightmostDigit
题目描述
Given a positive integer N, you shouldoutput the most right digit of N^N.
输入
The input contains several test cases.The first line of the input is a single integer T which is the number of testcases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
输出
For each test case, you should outputthe rightmost digit of N^N.
样例输入
2
3
4
样例输出
7
6
提示
解答思路:
用a表示输入的n值的最后一位,那么就有
当a=0、1、5、6时最后n^n的最后一位为endp;
当a=2时,有 2 4 8 6 2 4 8 6.......
即当n=4k时 n^n的最后一位为6,
当n=4k+1时 n^n的最后一位为2,
当n=4k+2时 n^n的最后一位为4,
当n=4k+3时 n^n的最后一位为8,
当endp=3时,有 3 9 7 1 3 9 7 1.......
即当n=4k时 n^n的最后一位为1,
当n=4k+1时 n^n的最后一位为3,
当n=4k+2时 n^n的最后一位为9,
当n=4k+3时 n^n的最后一位为7,
当a=4时,有 4 6 4 6 4 6 4.......
即当n为偶数时 n^n的最后一位为6,
当n为奇数时 n^n的最后一位为4,
又因为当n的尾数为4时,n一定为偶数,综合得当a=4时, n^n的最后一位为6。
当a=7时,有 7 9 3 1 7 9 31.......
即当n=4k时 n^n的最后一位为1,
当n=4k+1时 n^n的最后一位为7,
当n=4k+2时 n^n的最后一位为9,
当n=4k+3时 n^n的最后一位为3,
当a=8时,有 8 4 2 6 8 4 2 6.......
即当n=4k时 n^n的最后一位为6,
当n=4k+1时 n^n的最后一位为8,
当n=4k+2时 n^n的最后一位为4,
当n=4k+3时 n^n的最后一位为2,
当a=9时,有 9 1 9 1 9 1 9.......
即当n为偶数时 n^n的最后一位为1,
当n为奇数时 n^n的最后一位为9,
又因为当n的尾数为9时,n一定为奇数,综合得当a=9时, n^n的最后一位为9。
#include<stdio.h>
int main()
{
int T,n,m,k,a;
scanf("%d\n",&T);
while(T--)
{
scanf("%d",&n);
m=n%4;
k=n%10;
if(k==0||k==1||k==5||k==6||k==9)
{
a=k;
}
else if(k==2)
{
if(m==0)
a=6;
elseif(m==1)
a=2;
elseif(m==2)
a=4;
elseif(m==3)
a=8;
}
else if(k==3)
{
if(m==0)
a=1;
elseif(m==1)
a=3;
elseif(m==2)
a=9;
elseif(m==3)
a=7;
}
else if(k==7)
{
if(m==0)
a=1;
elseif(m==1)
a=7;
elseif(m==2)
a=9;
elseif(m==3)
a=3;
}
else if(k==8)
{
if(m==0)
a=6;
elseif(m==1)
a=8;
elseif(m==2)
a=4;
elseif(m==3)
a=2;
}
else if(k==4)
a=6;
printf("%d\n",a);
}
return 0;
}
- Rightmost Digit
- Rightmost Digit
- Rightmost Digit
- Rightmost Digit
- Rightmost digit
- Rightmost Digit
- Rightmost Digit
- Rightmost Digit
- Rightmost Digit
- Rightmost Digit
- Rightmost Digit
- Rightmost Digit
- Rightmost Digit
- Rightmost Digit
- Rightmost Digit
- Rightmost Digit
- Rightmost Digit
- Rightmost Digit
- 经验之谈
- 插入排序 冒泡 选择 快速
- 开启 C++ 学习之路
- DSHOW 显示视频
- 各种内排序算法源码汇总--c语言
- Rightmost Digit
- iOS-Xcode8之调用系统相册
- SauceLabs + Selenium+ Nunit For Web Ui Test
- 深度学习框架PaddlePdddle学习( 二)
- Zookeeper总概
- MultipartFile-使用策略模式实现可拓展文件分类上传
- 究竟怎样写代码才算是好代码[转载]
- centos7 mini rpm安装mysql5.6.31
- [乐意黎]开通CSDN 博客快七周年,访问量终于超4000000次