简体   繁体   English

特定输入的异常行为

[英]Unexpected behavior for a particular input

I have written the following code to find out the value in the unit place of the exponentiation a^b ( http://www.spoj.com/problems/LASTDIG/ ). 我编写了以下代码,以求出幂a ^ bhttp://www.spoj.com/problems/LASTDIG/ )的单位位置的值。 It is working fine for all the test cases, except when a=1 . 对于所有测试用例,它都工作正常,除非a = 1 When a=1, then it starts printing the output (ie, 1) infinitely, on the command prompt (not on the online editors though). 当a = 1时,它将开始在命令提示符下(尽管不在在线编辑器上)无限打印输出(即1)。 Also, this unexpected behavior is shown only if the input is as below: 1 1 (any number here as the exponent) and not when: 1 (any number other than 1) (any number here as the exponent). 另外,仅当输入如下时,才会显示此意外行为:1 1(此处为指数的任何数字),而不是以下情况:1(1以外的任何数字)(此处为指数的任何数字)。

The code is as below: 代码如下:

#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;
}

What might be causing this abnormal behavior and how do I rectify this? 是什么原因导致这种异常行为,我该如何纠正?

在此处输入图片说明 I checked the code, and it works fine. 我检查了代码,它工作正常。 As you have pointed out, this error only occurs on command prompt and not on online editors, a possible reason may be your compiler. 如您所指出的,此错误仅在命令提示符下发生,而不在在线编辑器上发生,可能的原因可能是编译器。 Which one do you use? 您使用哪一个? Or maybe you could provide some other details. 或者,您可以提供其他一些详细信息。

I have attached a screenshot below of it working in my terminal. 我已经附上了下面的截图,该截图可以在我的终端上使用。 Just for reference, I am using gcc version 4.9.1. 仅供参考,我使用的是gcc版本4.9.1。

EDIT: I now wrote the output to a file. 编辑:我现在将输出写入文件。 It still didn't present any issues. 它仍然没有出现任何问题。 I need more information from you. 我需要您提供更多信息。 On the lighter side, did you try turning the computer off and on again? 在较轻的一面,您是否尝试过重新打开计算机电源?

在此处输入图片说明在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM