我编写了以下代码,以找出指数 a ^ b (http://www.spoj.com/problems/LASTDIG/)的单元位置中的值。它适用于所有测试用例,除非 a = 1 。当a = 1时,它会在命令提示符(而不是在线编辑器上)开始无限地打印输出(即1)。此外,仅当输入如下所示时才会显示此意外行为: 1 1(这里作为指数的任何数字) 而不是在: 1 (除1以外的任何数字)(此处作为指数的任何数字)。
代码如下:
#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--){
int a;
scanf("%d",&a);
if(a==1){ //Although a==1 the continue statement behaves abnormally
printf("1\n");
continue;
}
printf("Hello man 2! :)\n");
int end=1,i,unit[500],temp=1;
long long b;
scanf("%lld",&b);
if(b==0){
printf("1\n");
continue;
}
unit[0]=1;
bool goOn=true,marker=false;
while(goOn){
temp*=a;
for(i=0; i<end; i++){
if(unit[i]==(temp%10) && (temp%10)!=1)
marker=true;
}
if(marker)
goOn=false;
if(!marker){
unit[end]=(temp%10);
end++;
}
}
int tmp=b%(end-1);
if(tmp==0)
printf("%d\n",unit[(end-1)]);
else
printf("%d\n",unit[(tmp)]);
}
return 0;
}
可能导致这种异常行为的原因是什么?如何纠正?
答案 0 :(得分:0)
我检查了代码,它工作正常。正如您所指出的,此错误仅发生在命令提示符而不是在线编辑器上,可能的原因可能是您的编译器。你用哪一个?或许您可以提供一些其他细节。
我在我的终端上附上了截图。仅供参考,我使用的是gcc版本4.9.1。
编辑:我现在将输出写入文件。它仍然没有出现任何问题。我需要你提供更多信息。在较轻的一面,您是否尝试关闭计算机并再次打开?