简体   繁体   中英

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/ ). It is working fine for all the test cases, except when a=1 . When a=1, then it starts printing the output (ie, 1) infinitely, on the command prompt (not on the online editors though). 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).

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.

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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