首页 > 试题广场 >

球的半径和体积

[编程题]球的半径和体积
  • 热度指数:23930 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入球的中心点和球上某一点的坐标,计算球的半径和体积

输入描述:
球的中心点和球上某一点的坐标,以如下形式输入:x0 y0 z0 x1 y1 z1


输出描述:
输入可能有多组,对于每组输入,输出球的半径和体积,并且结果保留三位小数

为避免精度问题,PI值请使用arccos(-1)。
示例1

输入

0 0 0 1 1 1 

输出

1.732 21.766

python solution, easy to understand

import math
while True:
    try:
        a,b,c,d,e,f=map(int,input().split())
        r=((d-a)**2+(e-b)**2+(f-c)**2)**(1/2)
        v=4/3*math.acos(-1)*r**3
        print("{0:.3f}".format(r)+" "+"{0:.3f}".format(v))



    except:
        break
发表于 2017-10-04 14:02:08 回复(0)
import java.lang.Math;
import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner in=new Scanner(System.in);
		while(in.hasNext()){
			double x0=in.nextFloat();
			double y0=in.nextFloat();
			double z0=in.nextFloat();
			double x1=in.nextFloat();
			double y1=in.nextFloat();
			double z1=in.nextFloat();
            //注意要在循环输入语句中输出结果
            //注意输入格式哟
			System.out.printf("%.3f",getRadius(x0,y0,z0,x1,y1,z1));
			System.out.print(" ");
			System.out.printf("%.3f",getVolume(x0,y0,z0,x1,y1,z1));
			System.out.println();
		}
		in.close();
	}
	//计算半径的方法,写的详细了点,快的话,可以直接return表达式
	public static double getRadius(double x0,double y0,double z0,double x1,double y1,double z1){
		double radius=0;
		double radiusSquare=0;
		radiusSquare=(x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)+(z1-z0)*(z1-z0);
		radius=Math.sqrt(radiusSquare);//开根号方法
		return radius;
	}
	//计算体积的方法
	public static double getVolume(double x0,double y0,double z0,double x1,double y1,double z1){
		double volume=0;
        //调用math中的方法,也可以用r*r*r
		volume=4*Math.acos(-1)*Math.pow(getRadius(x0,y0,z0,x1,y1,z1), 3)/3;
		return volume;
	}
}

发表于 2016-04-06 21:52:37 回复(0)
#include<stdio.h>
#include<math.h>
int main()
{
    double pi=acos(-1),x1,x2,x3,y1,y2,y3,r,v;
    while(scanf("%lf%lf%lf%lf%lf%lf",&x1,&x2,&x3,&y1,&y2,&y3)!=EOF)
    {
        r=sqrt(pow(x1-y1,2)+pow(x2-y2,2)+pow(x3-y3,2));
        v=4*pi*pow(r,3)/3;
        printf("%.3lf %.3lf\n",r,v);
    }
    return 0;
}
发表于 2018-01-15 15:56:17 回复(2)
Java 解法
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x1 = scanner.nextInt();
        int y1 = scanner.nextInt();
        int z1 = scanner.nextInt();
        int x2 = scanner.nextInt();
        int y2 = scanner.nextInt();
        int z2 = scanner.nextInt();
        
        double r = Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)+Math.pow(z1-z2,2));
        double v = 4/3.0*Math.acos(-1)*Math.pow(r,3);

        DecimalFormat format = new DecimalFormat("0.000");
        System.out.println(format.format(r)+" "+format.format(v));


    }
}


发表于 2020-03-17 22:38:21 回复(0)
小数点这个地方学习了一下,使用iomanip中的setiosflags与setprecision来控制double型小数点的输出,用float表示半径的话实测case中好像有后几位小数出现偏差的情况
#include<iostream>
(720)#include<cmath>
#include<iomanip>

using namespace std;

int main(){
    int x0,y0,z0;
    int x1,y1,z1;
    while(cin>>x0>>y0>>z0>>x1>>y1>>z1){
        double r = sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)+(z1-z0)*(z1-z0));
        cout<<setiosflags(ios::fixed)<<setprecision(3)<<r<<" "
            <<setiosflags(ios::fixed)<<setprecision(3)<<4*acos(-1)*r*r*r/3<<endl;
    }
    return 0;
}

发表于 2020-02-23 23:23:35 回复(1)
import math
while True:
    try:
        numList = list(map(int,input().split()))
        radius = ((numList[3]-numList[0])**2+(numList[4]-numList[1])**2+(numList[5]-numList[2])**2)**0.5
        volume = 4/3*math.acos(-1)*(radius**3)
        print('%.3f %.3f' % (round(radius,3),round(volume,3)))
    except Exception as message:
        break
编辑于 2018-10-11 11:15:50 回复(0)
#include<stdio.h>
#include<math.h>
int main()
{
    double x0, y0, z0;
    double x1, y1, z1;
    scanf("%lf%lf%lf", &x0, &y0, &z0);
    scanf("%lf%lf%lf", &x1, &y1, &z1);
    double r = pow(pow(x1-x0, 2) + pow(y1-y0, 2) + pow(z1-z0, 2),1.0/2);//空间坐标算距离,平方和开方
    double area = 4.0/3 * acos(-1) * pow(r, 3);//球的体积公式
    printf("%.3lf %.3lf\n", r, area);
    return 0;
}

编辑于 2018-07-25 00:20:04 回复(0)
#include<cmath>
#include<iomanip>
#include<iostream>

using namespace std;
#define PI acos(-1)

int main()
{
    double x0,y0,z0;
    double x1,y1,z1;
    while(cin>>x0>>y0>>z0>>x1>>y1>>z1)
    {
        double x2 = x0-x1;
        double y2 = y0-y1;
        double z2 = z0-z1;

        double r = sqrt(x2*x2+y2*y2+z2*z2);
        double V = PI*r*r*r*4/3;

        cout<<setiosflags(ios::fixed)<<setprecision(3)<<r<<" "<<V;
    }

    return 0;
}

两点之间的距离公式✔[(x1-x2)2+(y1-y2)2+(z1-z2)2],球的体积公式4/3*π*R3
利用C++设置小数位数利用fixed以及setprecision()进行设置
注意需要包括头文件<iomanip>

发表于 2018-02-06 20:57:11 回复(0)
代码简陋,请勿见怪

#include <iostream>
#include <cmath>
using namespace std;
int main(int argc, char * argv[]){
double x0, y0, z0, x, y, z;while (cin >> x0 >> y0 >> z0 >> x >> y >> z){double R = sqrt((x0-x)*(x0-x)+(y0-y)*(y0-y)+(z0-z)*(z0-z));
printf("%.3lf %.3lf\n", R, 4.0/3*3.14159265358979323846*R*R*R);}return 0;}

编辑于 2017-06-27 00:44:33 回复(0)
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

double myfabs(double x, double y)
{
    return x-y > 0 ? x-y : y-x;
}

int main()
{
    double x0, y0, z0;
    double x1, y1, z1;
    while(cin >> x0 >> y0 >> z0 >> x1 >> y1 >> z1)
    {
        double r = sqrt(myfabs(x0,x1)*myfabs(x0,x1)+myfabs(y0,y1)*myfabs(y0,y1)+myfabs(z0,z1)*myfabs(z0,z1));
        double v = 4.0*r*r*r*acos(-1)/3.0;       // arccos 对应函数 acos
        printf("%.3lf %.3lf\n", r, v);
    }

    return 0;
}


发表于 2016-08-04 15:09:41 回复(0)
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main(){
   int a,b,c,d,e,f;
    double g;
    while(cin>>a){
        cin>>b>>c>>d>>e>>f;        
        g=(double)((d-a)*(d-a)+(e-b)*(e-b)+(f-c)*(f-c));
        g=sqrt(g);       
        cout<< fixed <<setprecision(3)<<g<<" "<<acos(-1)*g*g*g*4/3<<endl;
    }
    return 0;
}

发表于 2017-02-27 22:25:19 回复(0)
有个问题,为什么float不可以???
#include<iostream>
(720)#include<math.h>
using namespace std;
int main(){
    double x0,y0,z0,x1,y1,z1;
    double r,V;
    int temp;
    while(cin>>x0>>y0>>z0>>x1>>y1>>z1){
        r=sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)+(z1-z0)*(z1-z0));
        //temp=r*1000;
        //r=temp/1000;
        V=4*acos(-1)*r*r*r/3;
        printf("%.3lf %.3lf\n",r,V);
    }
    return 0;
}



发表于 2020-03-02 16:20:54 回复(2)
思路很简单,只要知道球半径为球面到球心距离即可,然后球的体积为即可
还有一个注意的为小数点位数的设置方法:头文件为iomanip,使用fixed<<setprecision(3)来设置
最后是弧度,arccos在cmath头文件中使用acos进行运算
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main(){
    double x0,y0,z0,x1,y1,z1;
    double r;
    double size;
    while(cin>>x0>>y0>>z0>>x1>>y1>>z1){
        r=sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)+(z1-z0)*(z1-z0));
        size=r*r*r*acos(-1)*4/3;
        cout<<fixed<<setprecision(3)<<r<<" ";
        cout<<fixed<<setprecision(3)<<size<<endl;
    }
    return 0;
}

发表于 2019-02-05 21:55:59 回复(1)
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main(){
    double x0,y0,z0,x1,y1,z1;
    cin >> x0 >> y0 >> z0 >> x1 >> y1 >> z1;
    double dxy = sqrt( (x1-x0)*(x1-x0) + (y1-y0)*(y1-y0) );
    double r = sqrt(dxy*dxy + (z1-z0)*(z1-z0));
    double v = acos(-1)*pow(r,3)*4/3;
    printf("%.3f %.3f", r, v);
    return 0;
}

编辑于 2024-03-16 21:06:54 回复(0)
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
    double x0, y0, z0, x1, y1, z1;
    while (cin >> x0 >> y0 >> z0 >> x1 >> y1 >> z1) {
        double rx = x1 - x0;    //半径在x轴上的投影
        double ry = y1 - y0;    //半径在y轴上的投影
        double rz = z1 - z0;    //半径在z轴上的投影
        double r = sqrt(rx * rx + ry * ry + rz * rz);   //半径
        double pi = acos(-1);   //PI值
        double v = 4.0 / 3 * pi * r * r * r;    //体积
        printf("%.3f %.3f\n", r, v);
    }
    return 0;
}

发表于 2024-01-29 08:43:23 回复(0)
不知道为什么只能用double,用float精度就不一致
#include <cstdio>
#include <cmath>

int main(){
    int x0,y0,z0, x1,y1,z1;
    double R,V;
    while(scanf("%d %d %d %d %d %d",&x0,&y0,&z0,&x1,&y1,&z1) != EOF){
        R = sqrt((x1-x0)*(x1-x0) +  (y1-y0)*(y1-y0)  + (z1-z0)*(z1-z0) );
        V = 4.0/3*acos(-1)*R*R*R;
        printf("%.3lf %.3lf\n",R,V);
    }
    return 0;
}


发表于 2023-03-28 20:19:05 回复(0)
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main() {
    int x0, y0, z0, x1, y1, z1;
    while (scanf("%d%d%d%d%d%d", &x0, &y0, &z0, &x1, &y1, &z1) != EOF) {
        double r, v;
        r = sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0) + (z1 - z0) * (z1 - z0));
        v = 4 * acos(-1) * pow(r, 3) / 3;
        printf("%.3f %.3f", r, v);
    }
}
发表于 2023-03-16 21:05:52 回复(0)
#include <stdio.h>
#include <math.h>

int main() {
    int x0, y0, z0, x1, y1, z1;
    while (scanf("%d%d%d%d%d%d", &x0, &y0, &z0, &x1, &y1, &z1) != EOF) {
        double r = sqrt(pow(x0 - x1, 2) + pow(y0 - y1, 2) + pow(z0 - z1, 2));
        printf("%.3lf ", r);
        printf("%.3lf\n", ((double)4 / 3) * acos(-1) * pow(r, 3));
    }
    return 0;
}

发表于 2023-02-13 21:24:12 回复(0)
#涉及到精度了,那就用double,用float很可能精度不够,这道题就是这样
#include "stdio.h"
#include "math.h"
using namespace std;

#define Pi acos(-1)

int main(){
    double x0,y0,z0;
    double x1,y1,z1;
    double r;double a,b,c;
    double v;
    while (scanf("%lf%lf%lf%lf%lf%lf",&x0,&y0,&z0,&x1,&y1,&z1)!=EOF){
        a= pow(abs(x1-x0),2);
        b= pow(abs(y1-y0),2);
        c= pow(abs(z1-z0),2);
        r= sqrt(a+b+c);
        v=4*Pi*r*r*r/3;
        printf("%.3lf %.3lf",r,v);
    }
}
发表于 2023-02-12 16:22:47 回复(0)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int main() {
    int x0, y0, z0, x1, y1, z1;
    while (scanf("%d %d %d %d %d %d\n", &x0, &y0, &z0, &x1, &y1, &z1) != EOF) {
        double r = sqrt((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1) + (z0 - z1) * (z0 - z1));
        double v = 4.0000000 / 3.0000000 * acos(-1) * r * r * r;
        printf("%.3f %.3f\n", r, v);
    }
}

发表于 2022-03-21 16:11:13 回复(0)

问题信息

难度:
95条回答 12384浏览

热门推荐

通过挑战的用户

查看代码