题目描述
读入一个单精度浮点数,保留 3 位小数输出这个浮点数。
提示:就这题来说,请使用 float
类型的单精度浮点数。
输入格式
只有一行,一个单精度浮点数。
输出格式
也只有一行,读入的单精度浮点数。
输入输出样例
输入 #1
12.34521
输出 #1
12.345
思路
这道题有两种办法。一种是用 cout 保留小数(setprecidion),另外一种则是用 printf 保留小数。
AC Code1
#include<bits/stdc++.h>
using namespace std;
int main()
{
float a;
cin >> a;
cout << fixed << setprecision(3) << a;
return 0;
}
AC Code2
#include<bits/stdc++.h>
using namespace std;
int main()
{
float a;
scanf("%f", &a);
printf("%.3f", a);
return 0;
}