总成绩和平均分计算
总成绩和平均分计算
http://www.nowcoder.com/questionTerminal/0fa5132c156b434da4347ad051c4be22
分析:
读入数据之后,设置输出精度输出即可,分别提供了printf和cout的精度设置方法。
题解:
#include <bits/stdc++.h>
using namespace std;
int main() {
float a = 0.f, b = 0.f, c = 0.f;
//读入三门课的成绩
cin >> a >> b >> c;
//设置输出格式和精度,最后输出总和和平均分即可
cout << setiosflags(ios::fixed);
cout << setprecision(2) << a+b+c << ' ' << (a+b+c)/3.f << endl;
return 0;
}题解2:
#include <bits/stdc++.h>
using namespace std;
int main() {
float a = 0.f, b = 0.f, c = 0.f;
//读入三门成绩
scanf("%f %f %f", &a, &b, &c);
//使用printf输出更为方便
printf("%.2f %.2f\n", a+b+c, (a+b+c)/3.f);
return 0;
}总结:
简单表达式的计算和输出格式控制。
