1577 - 球的计算
时间限制 : 1 秒
内存限制 : 32 MB
输入球的中心点和球上某一点的坐标,计算球的半径和体积。
题目输入
输入第一行为样例数m,接下来m行每行6个整数,分别表示球心和球上一点的坐标。
题目输出
输出m行,每行2个浮点数分别表示球的半径和体积,保留到小数点后两位。
输入/输出样例
输入格式
1 0 0 0 1 0 0
输出格式
1.00 4.19
C语言解答
#include<stdio.h> #include<math.h> #define PI 3.14159265; int main() { double r,s; int n,x1,y1,z1,x2,y2,z2; while(scanf("%d",&n)!=EOF) { while(n--) { scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2); r=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2)); s=4*r*r*r*PI; printf("%.2lf %.2lf\n",r,s/3); } } return 0; }
C++解答
#include <math.h> #include <stdio.h> using namespace std; const double pi = acos(-1.0); int main() { //freopen("test.in", "r", stdin); //freopen("test.out", "w", stdout); int t; scanf("%d", &t); while (t--) { double r; double ax, ay, az, x, y, z; scanf("%lf%lf%lf", &ax, &ay, &az); scanf("%lf%lf%lf", &x, &y, &z); r=sqrt((x-ax)*(x-ax)+(y-ay)*(y-ay)+(z-az)*(z-az)); printf("%.2f %.2f\n", r, 4*pi*r*r*r/3); } return 0; }
Java解答
import java.text.DecimalFormat; import java.util.Scanner; public class Main { private static Scanner s = new Scanner (System.in) ; static DecimalFormat df = new DecimalFormat("0.00") ; public static void main(String[] args) { int t = s.nextInt() ; for (int i = 0; i < t; i++) { int x1 = s.nextInt() ; int y1 = s.nextInt() ; int z1 = s.nextInt() ; int x2 = s.nextInt() ; int y2 = s.nextInt() ; int z2 = s.nextInt() ; int r = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2) ; double R = Math.sqrt(r) ; double V = 4*Math.PI*R*R*R/3 ; System.out.println(df.format(R)+" "+df.format(V)); } } }