it Is my code for( Ciel and A-B Problem ) and I did not understand why my answer is wrong…!
#include <stdio.h>
int main()
{
int a ;
int b ;
int diff ;
scanf("%d", &a);
scanf("%d", &b);
diff = a - b ;
if ((diff%100) == diff )
{
if (diff <9 )
{
printf("%d\n",diff+1);
}
else
{
printf("%d\n",diff-1);
}
}
else if ((diff%100) < 99 )
{
printf("%d\n",diff+10);
}
else if ((diff%100) == 99 )
{
printf("%d\n",diff-10);
}
return 0;
}
best regards
Your code gives wrong output for this test case:
Input
298 100
Your Output
208
The actual difference is 198 and your code gives 208. As you can see, if differs at two places. Change your code to cover these type of cases.
However, this problem has a simpler solution.
Click to view
We want the result to differ at exactly one place. This means that we do not want any carry overs. Thus to avoid any carry overs, if the last digit of the answer is 9: subtract 1, else add 1. This way only the last digit will get changed.
if(diff % 10 == 9)
diff -= 1;
else
diff += 1;