在线编译器 C

#include <stdio.h> int main() { int price, quantity, discount; scanf("%d%d%d", &price, &quantity, &discount); double total; total = price * quantity * (100 - discount) / 100.0; printf("C=%.2f\n",total); return 0; }
这段代码的功能是:从用户输入商品单价、数量和折扣百分比,计算折扣后的总价并保留两位小数输出。

代码中有一个潜在问题需要注意:

- 整数除法的问题:表达式 `(100 - discount) / 100.0` 中,虽然除以100.0能保证结果是浮点数,但 `price * quantity * (100 - discount)` 这部分仍然是整数乘法。如果 `price * quantity * (100 - discount)` 的值很大,可能会超出int的范围,导致溢出。

- 建议考虑将 `price` 或 `quantity` 转换为浮点数后再进行乘法运算,或者使用 `double` 类型的变量来存储中间结果。