Home > Back-end >  A question of values
A question of values

Time:01-03

# include
# include
Int main ()
{
Int a, n.
The scanf (" % d % d ", & amp; A, & amp; n);
Int I, sum=0, t=0;

for(i=0; iT=t + pow (10, I);
Printf (" \ n t=% d ", t);
The sum +=a * t;
Printf (" s=% d \ n ", sum);
}
Printf (" s=% d \ n ", sum);
return 0;
}

2, 3,
T=1
S=2
T=11
S=24
T=110
S=244
S=244

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
The Process exited with the return value 0
Press any key to continue...


When asked why, the third time that t fell 1, should not be 111

CodePudding user response:

Is it the compiler's problem,,,

CodePudding user response:

Modified as follows, for your reference:
 # include 
Int main ()
{
Int a, n.
The scanf (" % d % d ", & amp; A, & amp; n);
Int I, sum=0, t=0;

for(i=0; iT=t * 10 + 1;//t=t + pow (10, I);
Printf (" \ n t=% d ", t);
The sum +=a * t;
Printf (" s=% d \ n ", sum);
}
Printf (" s=% d \ n ", sum);

return 0;
}

Two ways of the result is the same:

CodePudding user response:

Many beginners always put the truncation error as, in fact, it is a mistake,
Floating point Numbers assigned to the integer is not rounded, when is truncated,
Assignment before you add 0.5 is responsible for yourself,
  • Related