题解 | 学生基本信息输入输出
学生基本信息输入输出
https://www.nowcoder.com/practice/58b6a69b4bf943b49d2cd3c15770b9fd
第一种方法使用round()函数
round() 是数学四舍五入函数,专门把小数变成最近的整数。
规则:
- 小数部分 ≥ 0.5 → 进 1
- 小数部分 < 0.5 → 舍去
#include <stdio.h>
#include <math.h>
int main() {
long long i;
double a,b,c;
scanf("%lld;%lf,%lf,%lf",&i, &a, &b,&c);
a = round(a * 100) / 100;
b = round(b * 100) / 100;
c = round(c * 100) / 100;
printf("The each subject score of No. %lld is %.2f, %.2f, %.2f.", i, a, b,c);
return 0;
}
第二种不用引入库函数的方法:
#include <stdio.h>
int main() {
long long i;
double a, b, c;
scanf("%lld;%lf,%lf,%lf", &i, &a, &b, &c);
// 不用round(),手动实现四舍五入到两位小数
a = (int)(a * 100 + 0.5) / 100.0;
b = (int)(b * 100 + 0.5) / 100.0;
c = (int)(c * 100 + 0.5) / 100.0;
printf("The each subject score of No. %lld is %.2f, %.2f, %.2f.", i, a, b, c);
return 0;
}