Home > Back-end >  A novice for help! Don't () of money/100 * 100 * 1.2 and (money/100) * 120 different?
A novice for help! Don't () of money/100 * 100 * 1.2 and (money/100) * 120 different?

Time:02-09

Recently I was followed a book - "informatics orsay a tong (c + + edition)" learning c + +, book exercises in question is as follows:

With pocket money management has always been himself, mother to the beginning of each month with 300 yuan, with will budget the cost of this month, and always can do actual cost and budget of the same,
In order to make with learning how to save, the mother is put forward, with can always put the best money in her there, to the end she would add 20% also enjoyed, so with a savings plan: every month, the beginning of each month after get mother to pocket money, if she expected to the end of this month there will be extra 100 yuan or 100 yuan, she would bring the whole money in my mother there, the rest of the money in their own hands,
In early November, for example, with 83 yuan in the hand, love mother gave 300 yuan, with the expected cost of November is 180 yuan, then she will be in her mother there to deposit 200 yuan, his left 183 yuan. By the end of November, with hands will leave 3 yuan,
Now please according to each month from January 2020 to December with budget, calculated by the end of 2020, the mother will take money in the normal and 20% back with later, with how many money,
Input:
Including 12 rows, each row contains one of the nonnegative integers smaller than 350, January to December, respectively with the budget,
Output:
Contains only one integer, if savings plan appeared in the process of implementing a ago enough, output - X, X said that happens the first month; Otherwise the output to the end of 2020 with how many money,
The sample input 1:
290
230
280
200
300
170
340
50
90
80
200
60
Sample output 1:
7

The sample input 2:
290
230
280
200
300
170
330
50
90
80
200
60
Sample output 2:
1580

My program is as follows:

# include & lt; Cstdio>
using namespace std;
Int main ()
{
Int money=0, budget, deposit=0, the back=0, result=0;
//define the money (with money) in his hand, budget (budget) a month, deposit money to the mother's hand (with), the back (last mother with interest rates back to tianjin
//tianjin money), and the result (final with how much money in hand)
For (int I=1; I & lt;=12; + + I)//use a for loop input 12 times monthly budget
{
Money +=300;//mother to with an allowance of 300 yuan a month
The scanf (" % d ", & amp; Budget);//the user to enter the month budget
Money -=budget;//how much is left and thrown to the month budget money
If (money & lt; 0)//it is not enough money
{
Printf (" % d \ n ", I);
return 0;//output - X, the end of the program
}
The else
{
Deposit +=(money/100 * 100);
Money %=100;//problem (in the) calculate how many of the remaining money hundreds of money, in the mother (deposit, money)
}
}
The back=deposit * 1.2;//problem (in the) with 1.2 times the money with interest
Result=+ money back;//add with 12 months remaining at the hands of the money is the final result
Printf (" % d \ n ", result);
return 0;//end program
}

The book has a matching evaluation website, http://ybt.ssoier.cn:8088/, say after I transmit the program went wrong, I am very confused, refer to the answer after check again, the answer is as follows:

Reference [program]
# include & lt; iostream>
# include & lt; Cstdio>
using namespace std;
int x; Int I, n, t; Bool f=1;
Int main ()
{
t=0; n=0;//t deposit with left hand money, n save money with there mother, initial values are assigned to 0
for(i=1; i<=12; + + I)
{
The scanf (" % d ", & amp; X);
T=t + 300 - x;
If (t<0)
{
Printf (" % d \ n ", I);
return 0;
}
If (t>
=100){
N=n + t/100; T %=100;
}
}
Printf (" % d \ n ", n * 120 + t);
return 0;
}

I wonder, the answer and I don't have the difference? After many tests, I have discovered, the measurement site think I * 100 * 1.2 is wrong again, first should be like the answer directly by 120, the two aren't the same??????? 100 * 1.2=120? !   is because after by 1.2 type changed or not???????

To help you, thank you!!!!!! Thank you very much!!!!!!

CodePudding user response:

The two of you are not very good, should not use the int data type, at least with a float type, such as 100 * 1.2=120 is not wrong, but if 3 * 1.2?

CodePudding user response:

reference 1/f, Simple, Soft reply:
the two of you are not very good, should not use the int data type, at least with a float type, such as 100 * 1.2=120 is not wrong, but if 3 * 1.2?


Thanks for comment! Thank you thank you!!!!!!
  • Related