一个很简单的C语言程序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 foll
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/28 20:09:33
一个很简单的C语言程序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 foll
一个很简单的C语言程序
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. 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. 求各种答案,我老是超出时间限制,求救
一个很简单的C语言程序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 foll
不要先把总结果求出来再取最后一个数字
因为最后一个数字肯定有数字的个位数决定
比如13^13,就求3*13,但是每次记录最后一个数字
3×3=9 9
9×3=27 7
7×3=21 1
1×3=3 3
3×3 9
然后就可以看出是个最后结果处于每4次一个循环
因为13x13x13...x13总共有12个乘号,所以12%4=0所以结果是3
再举一个例子77^77,就求7^77
7×7=49 9
9×7=63 3
3×7=21 1
1×7=7 7
7×7=49 9
所以4次一循环
77x77x77..x77中有76个乘号,所以76%4=0,所以取循环的第四个数,答案是7
再举一个列子98×98,就求8^98
8×8=64 4
4×8=32 2
2×8=16 6
6×8=48 8
8×8=64 4
出现循环,4次一循环
98×98.×98中有97个乘号,97%4=1,取第1个数,所以结果为4
以下就是我自己编写的程序,程序中有详细的说明,你根据我举例好好想一下,如果再不懂,继续问
#include
int main()
{
int n;//输入的正整数
long aresult;//每次相乘得到的个位数
printf("please input a positive integer:");
scanf("%d",&n);
int m=n%10;//取出正整数n的个位数,比如123,m就是3,274,m就是4
int a[10];//用于存储aresult的数组,记录aresult的不重复的值
int aflag=0;//数组a的标志,也用来记录a数组的元素个数
int i,j;//用于循环
int forflag=0;//用途是如果aresult值与a数组中的值重复,那么退出所有循环,详细用途请阅读所有程序
for(i=0;i