π的精准表示以及C++中如何保留小数点后几位
Description
给定圆的半径r,求圆的面积。
Input
输入包含一个整数r,表示圆的半径。
output
输出一行,包含一个实数,四舍五入保留小数点后7位,表示圆的面积。
Sample Input
4
Sample Output
50.2654825
Hint
1 < = r < = 10000。
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
double pi = acos(-1.0);
int r ;
double s ;
cin>>r;
s = pi * r * r ;
cout<<setiosflags(ios::fixed)<<setprecision(7)<<s<<endl
return 0 ;
}
嘤嘤嘤,第一次写,嗯…感觉还不错啊哈哈哈…
注意几个问题:
π的最精准表达:
double pi = acos(-1.0);
C++中输出保留小数点后几位的方法:
cout<<setiosflags(ios::fixed)<<setprecision(7)<<s<<endl;
#include<iomanip>是包含该函数的头文件;
setprecision(7)中的7表示保留小数点后7位。
若为C语言则为:printf("%.7f",s);