错哪了

木可大大 2008-09-21 03:56:51
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.
我的代码是
#include<stdio.h>
#include<stdlib.h>
int main()
{
unsigned n,m,temp,flag;
while(1)
{
scanf("%d",&n);
while(m>0)
{
scanf("%d\n",&m);
temp=m^m;
while(temp/10>0)
{
flag=temp%10;
}

print("%d\n",flag);
}
}
return 0;
}错哪里了
...全文
205 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mifeixq 2008-09-22
  • 打赏
  • 举报
回复
写了个效率高点的…… 你看看能AC不…… 不能我再改改

#include <stdio.h> 
#include <math.h>
int get_result(int m, int n)
{
int store;
int count;
if(m==0||m==1||m==5||m==6)
return m;
switch(m)
{
case 2:
store = n%4;
if(n>4)
return pow(2, store)*6;
else
return pow(2, store);
case 3:
store = n%4;
return pow(3, store);
case 4:
store = n%2;
if(store)
return 4;
else
return 6;
case 7:
store = n%4;
return pow(7, store);
case 8:
store = n % 4;
return pow(8, store)*6;
case 9:
store = n%2;
if(store)
return 9;
else
return 1;
}
}
int main()
{
int count,num;
scanf("%d",&count);
while(count--)
{
int result;
int last;
scanf("%d",&num);
last = num % 10;
result=get_result(last, num)%10;
printf("%d\n", result);
}

return 0;
}
fanzf24 2008-09-22
  • 打赏
  • 举报
回复
好像是浙大ACM的题目,注释是修改的部分

#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned n,m,temp,flag;
while(1)
{
scanf("%d",&n); /*scanf("%d",&m);*/
while(m>0)
{
scanf("%d\n",&m);
temp=m^m;
while(temp/10>0)
{
flag=temp%10;
}

print("%d\n",flag); /*printf("%d/n",flag);*/
}
}
return 0;
}


ustczwx 2008-09-21
  • 打赏
  • 举报
回复
楼主一定要试一下我9楼的算法,效率可不是一般的高啊,嘿嘿
迷途的书童 2008-09-21
  • 打赏
  • 举报
回复
感觉4楼分析的很不错啊!俺把它改了一下;
如n^n;
先求m=n%10的最后一位;
再m=m×n%10求最后一位;
。。。。一共计算n次;代码如下:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


int main(int argc, char* argv[])
{
int m[100],temp,nNum;
int i=0;
printf("input the sample:\n");
scanf("%d",&nNum);
while(nNum--)
{
scanf("%d",&m[i++]);

}
printf("output thr right most digit :\n");
for(int j=0;j<i;j++)
{ temp=1;
for(int k=0;k<m[j];k++)
{
temp*=m[j];
temp=temp%10;
}
printf("%d\n",temp);


}
return 0;

}

kkndciapp 2008-09-21
  • 打赏
  • 举报
回复

void main()
{
int n,num;
cout<<"input n:"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>num;
int temp=num%10;
for(int j=0;j<num-1;j++)
{

temp*=num;
temp=temp%10;
}
cout<<temp<<endl;;
}

}
ustczwx 2008-09-21
  • 打赏
  • 举报
回复
错的太多了,第一行输入是测试数字的个数,后面包含N行输入。
unsiged int n,m,temp,result;
scanf("%d\n", &n);
while (n>0)
{
scanf("%d\n", &m);
result=temp=m%10;
if (temp==0||temp==1||temp==5||temp==6)
{printf("%d\n",temp); /*all these digits will be same as input*/}
else
{
result = 1;
while(m>0)
{
if (m & 0x01 == 0x01) /* m mod by 2 */
result = (result * temp) % 10;
temp = (temp * temp) % 10;
m = m >> 1; /* m divided by 2 */
}
printf("%d\n",result);
}
n--; /*count decrease*/
}
rollrock1987 2008-09-21
  • 打赏
  • 举报
回复
print("%d\n",flag); 你这个 是什么函数??

不是 printf吗??
zpnjit 2008-09-21
  • 打赏
  • 举报
回复
赞成楼上的
星羽 2008-09-21
  • 打赏
  • 举报
回复


#include "stdlib.h"

int get_last(int d)
{
while ((d %= 10) >= 10)
;
return d;
}

int main()
{
int* data = 0;
int num = 0;
int i = 0;

printf("please input the data : \n");
scanf("%d", &num);

if (num <= 0)
return 1;

data = (int*)malloc(sizeof(int) * num);

while (i < num)
scanf("%d", &data[i++]);

printf("the result is : \n");

for (i = 0; i < num; ++i)
{
int d = data[i];
int j = 0;
int w = get_last(d);
int r = w;

while (++j < d)
r = get_last(r * w);

printf("%d\n", r);
}

free(data);

return 0;
}


-------------

please input the data :
5
1
2
3
4
12345678
the result is :
1
4
7
6
4
请按任意键继续. . .





expert2000 2008-09-21
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned int n,m,temp,flag;
n = 0;
m = 0;
temp = 1;
//while(1)
//{
// scanf("%d",&n);
/// while(m>0)
//{
scanf("%d",&m);
while(n<m)
{
temp=temp*m;
n++;
}
printf("%d\n",temp);
if(temp/10>0)
{
flag=temp%10;
}

printf("%d\n",flag);
//}
//}
return 0;
}
bshawk 2008-09-21
  • 打赏
  • 举报
回复
你代码主要错误在:m^m并不是m的m次方预算,而是m和m做异或运算!

如果你只是简单的验证下:可以用如下代码

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
unsigned m,temp,flag;
while(1)
{
scanf("%d",&m);
if(m>0)
{
temp=pow(m,m);
flag =temp%10;
printf("%d\n",flag);
}
}
return 0;
}


但是上面的代码并不是本题要考你的地方,本题要考你是对很大的数(1,000,000,000)你该如何来实现? 显然,上面的算法对大数是不行的,需要重新考虑算法! 这怎么有点像ACM上的练习题?

Good Luck!



  • 打赏
  • 举报
回复
看见英语就头疼
rollrock1987 2008-09-21
  • 打赏
  • 举报
回复
can you。。。chinese???

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧