我是新手程序员和编码员。所以,我在HackerRank中花了30天的编码挑战,但是当我在C中运行the "Day 2: Operators" problem时,它没有显示任何错误。代码是:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
int total;
double meal_cost;
scanf("%lf", &meal_cost);
int tip_percent;
scanf("%i", &tip_percent);
int tax_percent;
scanf("%i", &tax_percent);
double tip=(meal_cost*tip_percent)/100;
double tax=(meal_cost*tax_percent)/100;
total=(int)(meal_cost + tip + tax);
printf("The total meal cost is %d dollars.",total);
printf("The total meal cost is %d dollars.",total);
return 0;
}
输入:
12.00
20
8
预期产出:
The total meal cost is 15 dollars.
实际输出:
Wrong Answer
答案 0 :(得分:2)
这是更清晰的方案。因为发现提示不征税。
计算在double
上完成,以避免早期截断。最终结果打印为double
或根据需要Hackerrank
四舍五入到int
个数字。
非常感谢Weather Vane
的贡献和帮助。
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
double total, meal_cost, tip_percent, tax_percent, tip, tax;
scanf("%lf", &meal_cost);
scanf("%lf", &tip_percent);
scanf("%lf", &tax_percent);
tip = (meal_cost*tip_percent/100.0);
tax = (meal_cost*tax_percent)/100.0;
// Tip taxed:
// tax = (meal_cost + tip) * (tax_percent/100.0);
// no tax on the tip:
tax = meal_cost * (tax_percent/100.0);
total= meal_cost + tip + tax;
printf("The total meal cost is %.2f dollars.\n",total);
printf("The total meal cost is %d dollars.\n", (int)(total+0.5));
return 0;
}
输出:
12
20
8
The total meal cost is 15.36 dollars.
The total meal cost is 15 dollars.
答案 1 :(得分:0)
-给定餐费(餐的基本费用)的小费百分比(
膳食价格的百分比作为小费)和税率百分比
(一顿饭中所含进餐价格的百分比),
在HackerRank中查找并打印餐点的总费用
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
double total, meal_cost, tip_percent, tax_percent, tip, tax;
scanf("%lf", &meal_cost);
scanf("%lf", &tip_percent);
scanf("%lf", &tax_percent);
tip = (meal_cost*tip_percent/100.0);
tax = (meal_cost*tax_percent)/100.0;
// Tip taxed:
// tax = (meal_cost + tip) * (tax_percent/100.0);
// no tax on the tip:
tax = meal_cost * (tax_percent/100.0);
total= meal_cost + tip + tax;
printf("%d", (int)(total+0.5));
return 0; }
12.0
20
8
15