1085 - 计算圆的周长
时间限制 : 1 秒
内存限制 : 32 MB
如果告诉你圆的直径,那么计算圆的周长是非常简单的。但是,这次不告诉你圆的直径。
现给你圆周上三个点的坐标,请你计算圆的周长。
题目输入
输入包含多组测试数据。每组输入包含6个实数x1,y1,x2,y2,x3,y3,表示圆周上三个点的坐标。
圆的直径不会超过1000000。
题目输出
对于每组输入,输出圆的周长,结果保留2位小数。(pi的取值为3.141592653589793)
输入/输出样例
输入格式
0.0 -0.5 0.5 0.0 0.0 0.5 0.0 0.0 0.0 1.0 1.0 1.0 5.0 5.0 5.0 7.0 4.0 6.0 0.0 0.0 -1.0 7.0 7.0 7.0 50.0 50.0 50.0 70.0 40.0 60.0 0.0 0.0 10.0 0.0 20.0 1.0 0.0 -500000.0 500000.0 0.0 0.0 500000.0
输出格式
3.14 4.44 6.28 31.42 62.83 632.24 3141592.65
C语言解答
#include<stdio.h> #include<math.h> #define pi 3.141592653589793 double sqr(double x) { return x*x; } int main() { double x1,y1,x2,y2,x3,y3,a,b,c,r; while(scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3)!=EOF) { a=sqr(x1-x2)+sqr(y1-y2); b=sqr(x1-x3)+sqr(y1-y3); c=sqr(x2-x3)+sqr(y2-y3); r=sqrt(c/4/(1-sqr(a+b-c)/4/a/b)); printf("%.2lf\n",2*pi*r); } return 0; }
C++解答
#include<stdio.h> #include<math.h> #define pi 3.141592653589793 double sqr(double x) { return x*x; } int main() { double x1,y1,x2,y2,x3,y3,a,b,c,r; while(scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3)!=EOF) { a=sqr(x1-x2)+sqr(y1-y2); b=sqr(x1-x3)+sqr(y1-y3); c=sqr(x2-x3)+sqr(y2-y3); r=sqrt(c/4/(1-sqr(a+b-c)/4/a/b)); printf("%.2lf\n",2*pi*r); } return 0; }
Java解答
import java.text.DecimalFormat; import java.util.Scanner; public class Main { private static Scanner s = new Scanner(System.in) ; private static double PI = 3.141592653589793 ; private static DecimalFormat df = new DecimalFormat("0.00") ; public static void main(String[] args) { while(s.hasNext()){ double x1 = s.nextDouble() ; double y1 = s.nextDouble() ; double x2 = s.nextDouble() ; double y2 = s.nextDouble() ; double x3 = s.nextDouble() ; double y3 = s.nextDouble() ; double d = 2*(x1-x2)*(y3-y2) - 2*(y1-y2)*(x3-x2) ; double d1 = (y3-y2)*(x1*x1+y1*y1-x2*x2-y2*y2) - (y1-y2)*(x3*x3+y3*y3-x2*x2-y2*y2); double d2 = (x1-x2)*(x3*x3+y3*y3-x2*x2-y2*y2) - (x3-x2)*(x1*x1+y1*y1-x2*x2-y2*y2); double x = d1/d ; double y = d2/d ; double R = Math.sqrt((x2-x)*(x2-x)+(y2-y)*(y2-y)) ; System.out.println(df.format(PI*R*2)); } } }