蓝桥杯练习 圆的面积
给定圆的半径r,求圆的面积。
输入格式
输入包含一个整数r,表示圆的半径。
输出格式
输出一行,包含一个实数,四舍五入保留小数点后7位,表示圆的面积。
样例输入
4
样例输出
50.2654825
数据规模与约定
1 <= r <= 10000。
提示
本题对精度要求较高,请注意π的值应该取较精确的值。你可以使用常量来表示π,比如PI=3.14159265358979323,也可以使用数学公式来求π,比如PI=atan(1.0)*4。
这道题目主要是掌握如何得到数据的指定精度。如果不是很清楚,可以看下这篇文章
https://blog.csdn.net/JB666M/article/details/124353940
可以拿这道题做个练习
这道题代码如下:
1:DecimalFormat类
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
double s = Math.PI*r*r;
DecimalFormat df = new DecimalFormat("0.0000000");
System.out.println(df.format(s));
}
}
2:Printf
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
double s = Math.PI*r*r;
System.out.printf("%.7f",s);
}
}