高次方数
codeblocks吧
全部回复
仅看楼主
level 2
#include <stdio.h>
int main()
{
int x,y,z,t,last_3,i,n;
last_3=481;
while(scanf("%d",&n)!=EOF)
{
for(i=12;i<n;i++)
{
t=last_3*13;
x=t%10;
y=t/10%10;
z=t/100%10;
last_3=z*100+y*10+x;
}
printf("%d\n",last_3);
}
return 0;
}
求13的n次方的后三位,只有13的结果是
正确的
为什么?(n>12)
2016年03月09日 14点03分 1
level 2
解决方法:在while循环里,for循环前加一句:last_3=481;
原因:因为last_3=z*100+y*10+x;所以每次运行for语句时,last_3的值是改变的,当下一次你输入n的值再次运行时,last_3不再是481,所以得出来的结果不正确。
表达能力有限,见谅。[汗]
程序:
#include <stdio.h>
int main()
{
int x,y,z,t,last_3,i,n;
last_3=481;
while(scanf("%d",&n)!=EOF)
{
last_3=481;
for(i=12; i<n; i++)
{
t=last_3*13;
x=t%10;
y=t/10%10;
z=t/100%10;
last_3=z*100+y*10+x;
}
printf("%d\n",last_3);
}
return 0;
}
2016年03月09日 15点03分 2
对了,谢谢哈[真棒]
2016年03月10日 14点03分
不过我两处都改了之后显示时间超限
2016年03月10日 14点03分
level 2
取t的后三位可以用:t%1000
2016年03月09日 15点03分 3
level 2
#include<stdio.h>
int main()
{
int last_3=481;
int n,i;
while (scanf("%d",&n)!=EOF)
{
for(i=12;i<n;i++)
{
last_3=last_3*13%1000;
}
printf("%d\n",last_3);
}
return 0;
}
这样可还是有问题?
2016年03月10日 14点03分 4
level 2
#include<stdio.h>
int main()
{
int last_3=481;
int n,i;
while (scanf("%d",&n)!=EOF)
{
last_3=481;//漏了这里!!!
for(i=12; i<n; i++)
{
last_3=last_3*13%1000;
}
printf("%d\n",last_3);
}
return 0;
}
2016年03月10日 15点03分 5
加了那里,可是时间超限啊?
2016年03月11日 00点03分
什么时间超限?我的能运行出来啊。
2016年03月11日 12点03分
是不是你输的数太大?如果这样那就要找周期,看13的n次方的后三位多久一循环。
2016年03月11日 12点03分
找到周期了,改好了,谢谢了
2016年03月13日 07点03分
1