int min1, min2, won;
printf("parking minutes(분)? ");
scanf("%d", &min1);
min2 = (min1 - 30) % 10;
if (min1 <= 39)
won = 2000;
else {
if (min2 = 0)
won = 2000 + 1000 * (min1 - 30) % 10;
else
won = 2000 + 1000 * (min1 - min2 - 20) % 10;
}
printf("parking fee: %d", won);
该程序的条件
我以为'%'表示余数,所以我这样写,但是当我输入52时,结果为5200
!我想使结果为5000
。我想知道在条件3和条件4下该怎么办?我应该使用“ for”和“ sum”吗?
答案 0 :(得分:0)
问题在于您在else块中的内部条件。
if(min2 = 0)
一个等号是赋值运算符,必须使用==进行相等性检查。
if(min2 == 0)
答案 1 :(得分:0)
让我们按照与作业相同的顺序来编程步骤:
int min1, min2, won;
printf("parking minutes(분)? ");
if (scanf("%d", &min1) != 1) {
printf("invalid input\n");
return 1; // invalid input.
}
won = 2000; // 1. until 30min, 2000won, minimum price
if (min1 > 30) {
// 2. after 30min, 1000won per 10min
min2 = min1 - 30; // minutes after 30
// add 1000won for every slice or 10min or portion thereof
won += ((min2 + 9) % 10) * 1000;
// 3. max 25000won per a day
if (won > 25000)
won = 25000;
}
// 4. parking minutes cannot be over than 24 hours
if (min1 > 24 * 60) {
// reject request
printf("parking time exceeds 24 hours\n");
} else {
printf("parking fee: %d\n", won);
}