It must be the bob == 0
and alice == 0
case. Put an print stamement in the else statement you will know why. Your execution is making that case somehow.
Also in issquare
you should have the loop like this
for(i = 0; i <= num; i++)
because for numbers like 1
it would fail.
Also there is a flaw in your logic. You should form the number
8 10
you should check 81
and 18
. But in your case you don’t do this.(You check 90
and 108
).
#include <stdio.h>
#include <stdlib.h>
int reverse(int num)
{
int rev=0,inter = 0;
while(num){
inter = rev*10;
if( inter/10 != rev){
fprintf(stderr, "%s\n", "Overflow\n");
exit(1);
}
rev=inter+num%10;
num/=10;
}
return rev;
}
int issquare(int num)
{
for(int i = 0; i <= num; i++){
long long ii = i;
long long mul = ii*ii;
if( ii!= 0 && mul/ii != ii){
fprintf(stderr, "%s\n", "Overflow\n");
exit(1);
}
if(mul == num)
return 1;
else if(mul > num){
break;
}
}
return 0;
}
int main()
{
int num1, num2, normal, rev, alice, bob;
if( scanf("%d%d", &num1, &num2)!= 2){
fprintf(stderr,"Error in input\n");
exit(1);
}
printf("%d %d", num1, num2);
rev = reverse(num1+num2);
normal = num1+ num2;
bob = issquare(rev);
alice = issquare(normal);
if(bob==alice)
printf("Tie");
else if(bob > alice)
printf("Bob wins.");
else
printf("Alice wins.");
return 0;
}
solved Who is the winner? [closed]