首页 > 试题广场 >

球的计算

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

输入描述:
输入第一行为样例数m,接下来m行每行6个整数,分别表示球心和球上一点的坐标。


输出描述:
输出m行,每行2个浮点数分别表示球的半径和体积,保留到小数点后两位。
示例1

输入

1
0 0 0 1 0 0

输出

1.00 4.19
emmm,就按照最少的代码行数来的,只要知道两点距离公式和球的体积应该都不会做不出来,除非就是细节可能会有不注意的不小心错了
#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
int main(){
    int m,x0,x1,y0,y1,z0,z1;
    cin>>m;
    for(int i=0;i<m;i++){
        cin>>x0>>y0>>z0>>x1>>y1>>z1;
        double r=sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1)+(z0-z1)*(z0-z1));
        double area=r*r*r*3.1415926*4/3;
        cout<<fixed<<setprecision(2)<<r<<' '<<area<<endl;
    }
}

发表于 2019-02-28 11:16:50 回复(2)
#include<stdio.h>
#include<math.h>
#define Pi 3.1415926
int main()
{     int n,i,j,a[6];     double r,s;     scanf("%d",&n);     for(i=0;i<n;i++)     {         for(j=1;j<=6;j++)             scanf("%d",&a[j]);         r=sqrt((a[4]-a[1])*(a[4]-a[1])+(a[5]-a[2])*(a[5]-a[2])+(a[6]-a[3])*(a[6]-a[3]));         s=4.0/3*Pi*r*r*r;         printf("%.2lf %.2lf\n",r,s);     }
}

发表于 2019-05-20 23:35:53 回复(0)
//球的半径和体积
#include<stdio.h>
(737)#include<math.h>
#define PI 3.1415926
int main()
{
    float x,y,z,x1,y1,z1,d,r,v;
    int m,i;
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
        scanf("%f%f%f%f%f%f",&x,&y,&z,&x1,&y1,&z1);
        d=sqrt((x1-x)*(x1-x)+(y1-y)*(y1-y));
        r=sqrt(d*d+(z1-z)*(z1-z));
        v=(float)4/3*PI*r*r*r;
        printf("%.2f %.2f\n",r,v);
    }
    return 0;
}

发表于 2020-05-09 16:14:53 回复(0)
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 m = scanner.nextInt();
        for (int i = 0; i <m; i++) {
            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.0/3*Math.PI*Math.pow(r,3);

            DecimalFormat f = new DecimalFormat("0.00");
            System.out.println(f.format(r)+" "+f.format(v));
        }


    }
}


发表于 2020-03-20 17:36:13 回复(0)
    首先pi一般使用accos(-1)代替,接着就是公式中r与result要注意数据精度问题,最后就是小数的
输出问题。

#include<iostream>
using namespace std;
(807)#include<iomanip>
#include<cmath>

(808)#define maxsize 1000000
#define pi acos(-1)



int main(){
	int m;
	int x1,x2,x3;
	int y1,y2,y3;
	double result; 
	double r;
	cin>>m;
	for(int i=0;i<m;i++){
		cin>>x1>>x2>>x3>>y1>>y2>>y3;
		r = sqrt((y1-x1)*(y1-x1)+(y2-x2)*(y2-x2)+(y3-x3)*(y3-x3));
		result=4*pi*r*r*r/3;
//		cout<<x1<<x2<<x3<<y1<<y2<<y3<<endl;
		cout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(2)<<r<<" "<<result<<endl;
	}
}

发表于 2020-02-22 09:06:36 回复(2)
本题考查基本数据类型+常用数学函数的使用,要注意 整数/整数 会丢失精度。
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main(){
	int m;
	cin >> m;
	for (int i = 0; i<m;i++){
		int a1,a2,b1,b2,c1,c2;
		cin>> a1>>b1>>c1 >> a2>>b2>>c2;
		double r = sqrt(pow(double(a2-a1),2) + pow((b2-b1),2) + pow((c2-c1),2));
		double area = 4*M_PI*pow(r,3)/3;
		printf("%.2f %.2f",r,area);
		cout << endl;
	}
	return 0;
}


发表于 2020-02-09 21:02:29 回复(0)
import math
m = int(input())
for i in range(m):
    ball_axis = list(map(int, input().split()))
    radius = math.sqrt(abs(ball_axis[3]-ball_axis[0])**2 + abs(ball_axis[4]-ball_axis[1])**2 + abs(ball_axis[5]-ball_axis[2])**2)
    volume = radius**3 * math.pi * 4 / 3
    print('%.2f %.2f'%(radius, volume))

发表于 2019-03-19 10:42:34 回复(0)
#include <iostream>
#include <algorithm>
#include <cmath> 
using namespace std;


 
int main(){
	int m;
	float x1,y1,z1,x2,y2,z2;
	cin>>m;
	while(m--){
		cin>>x1>>y1>>z1>>x2>>y2>>z2;
		float r,v;
		r = sqrt(pow(x2 - x1,2) + pow(y2 - y1,2) + pow(z2 - z1,2));
		v = ((float)4/3) * 3.14159265 * pow(r,3);
		printf("%.2f %.2f\n",r,v);
	}

}

发表于 2023-03-12 12:28:46 回复(0)
#include <cstdio>
#include <cmath>

int main(){
    int m;
    while(scanf("%d",&m) != EOF){
        for(int i = 0; i < m; ++i){
            int a,b,c; //球心坐标
            int x,y,z; //球上一点坐标
            float R,V;
            scanf("%d %d %d",&a,&b,&c);
            scanf("%d %d %d",&x,&y,&z);
            R = sqrt((x-a)*(x-a) + (y-b)*(y-b) + (z-c)*(z-c) );
            V = 4.0/3*3.1415926*R*R*R; //注意隐式转换,4/3是1,4.0/3才是1.33
            printf("%.2f %.2f\n",R,V);
        }
    }
    return 0;
}

发表于 2023-03-23 17:00:11 回复(0)
#include<iostream>
#include<cmath>
using namespace std;
void caculate(double x,double y,double z,double x1,double y1,double z1)
{
    double x_len,y_len,z_len;
    x_len = x1 - x;
    y_len = y1 - y;
    z_len = z1 - z;
    double r = sqrt(x_len * x_len + y_len * y_len + z_len * z_len);
    double v = 3.1415926 * pow(r, 3) * 4 / 3;
    printf("%.2lf %.2lf",r,v);
    cout << endl;
}
int main(void)
{
    int m;
    while(cin >> m)
    {
        while(m--)
        {
            double x,y,z;
            cin >> x >> y >> z;
            double x1,y1,z1;
            cin >> x1 >> y1 >> z1;
            caculate(x, y, z, x1, y1, z1);
        }
    }
    return 0;
}

发表于 2022-02-09 15:42:35 回复(0)
#include<stdio.h>
#include<math.h>

#define PI 3.1415926
int main()
{
    int n,i;
    int x,y,z,x0,y0,z0;
    double r,v;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d %d %d %d %d %d",&x0,&y0,&z0,&x,&y,&z);
        r=sqrt((x-x0)*(x-x0)+(y-y0)*(y-y0)+(z-z0)*(z-z0));
        v=(4*PI*r*r*r)/3.0;
        printf("%.2f %.2f\n",r,v);
    }
    return 0;
}
发表于 2021-05-16 10:46:23 回复(0)
#include <iostream>
#include <math.h>
#include <iomanip>
#define PI 3.1415926535
using namespace std;

class Point
{
public:
    Point(int x,int y,int z):x(x),y(y),z(z){}
    ~Point(){}
    int getX(){return x;}
    int getY(){return y;}
    int getZ(){return z;}
private:
    int x,y,z;
};
int main()
{
    int m;
    int x0,y0,z0,x1,y1,z1;
    double r,v;
    cin >> m;
    for(int i = 0; i < m; i++)
    {
        cin >> x0 >> y0 >> z0 >> x1 >> y1 >> z1;
        Point R(x0,y0,z0);
        Point S(x1,y1,z1);
        r = sqrt(pow(S.getX() - R.getX(),2) + pow(S.getY() - R.getY(),2) + pow(S.getZ() - R.getZ(),2));
        v = 4.0/3.0 * PI * pow(r,3);
        cout << fixed << setprecision(2) << r << " " << v << endl;
    }
    return 0;
}

发表于 2021-03-21 16:08:16 回复(0)
#include <iostream>
#include <cmath>
using namespace std;
const double pi = 3.1415926;
int main(){
    int m,x1,y1,z1,x2,y2,z2;
    double r;
    cin >> m;
    while(m--){
        cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
        r = sqrt( (x2-x1) * (x2-x1) + (y2-y1) * (y2-y1) + (z2-z1) * (z2-z1));
        double volume = (4 * pi * r * r * r) / 3;
        printf("%.2lf %.2lf\n",r,volume);
    }
}

发表于 2021-03-04 18:59:55 回复(0)
//应用数学公式:
//|AB|=√[(x2-x1)^2+(y2-y1)^2+(z2-z1)^2]
//V=(4/3)πr³
想体现一下Java的特性,有些繁琐了
import java.util.Scanner;
import static java.lang.Math.PI;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        while (m != 0) {
            double x1 = sc.nextDouble();
            double y1 = sc.nextDouble();
            double z1 = sc.nextDouble();
            double x2 = sc.nextDouble();
            double y2 = sc.nextDouble();
            double z2 = sc.nextDouble();

            Ball b = new Ball();
            b.setRadius(x1, x2, y1, y2, z1, z2);
            System.out.println(b.toString());  //限制精度后
            m--;
        }
    }
}
class Geometry {
    protected double side;
    protected double volume;

    public Geometry() {}

    public void setSide(double side) {
        this.side = side;
    }
    public double getSide() {
        return side;
    }
    public double getVolume() {
        volume = side * side * side;
        return volume;
    }
}
class Ball extends Geometry{
    private double x1, x2, y1, y2, z1, z2;
    private double radius;
    private double volume;
    public Ball(){}

    public void setRadius(double x1, double x2, double y1, double y2, double z1, double z2) {
        this.x1 = x1;
        this.y1 = y1;
        this.z1 = z1;
        this.x2 = x2;
        this.y2 = y2;
        this.z2 = z2;
    }

    public double getRadius() {
        return Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2) + Math.pow((z2 - z1), 2));
    }

    public double getVolume() {
        return (4.0 / 3.0) * PI * Math.pow(getRadius(), 3);
    }

    @Override
    public String toString() {
        return String.format("%.2f", getRadius()) + " " + String.format("%.2f", getVolume());
    }
}


发表于 2020-07-10 23:59:12 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI acos(-1)

int main()
{
    int m;
    while(~scanf("%d", &m))
    {
        for(int i = 0; i < m; i++)
        {
            int x0, y0, z0, x, y, z;
            scanf("%d%d%d%d%d%d", &x0, &y0, &z0, &x, &y, &z);
            float radius = sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0) + (z - z0) * (z - z0));
            float volume = 4.0 * PI * pow(radius, 3) / 3.0;
            printf("%.2f %.2f\n", radius, volume);
        }
    }

    return 0;
}

编辑于 2020-04-03 01:49:13 回复(0)
不指明π的取值属实坑爹
发表于 2020-01-30 09:56:36 回复(0)
好像用C的人比较少.....
#include<stdio.h>
#include<math.h>
#define PI 3.1415926
int main(void)
{
	int m;
	double a[3],b[3],c[3];
	double r,v,sum;
	while(!scanf("%d",&m))
		while(getchar()!='\n')
			continue;
	for (int i=0;i<m;i++)
	{
        int j;
		for (j=0;j<3;j++)
		{
			scanf("%lf",&a[j]);
		}
		for (j=0;j<3;j++)
		{
			scanf("%lf",&b[j]);
		}
		sum=0;
		for (j=0;j<3;j++)
		{
			c[j]=(b[j]-a[j])*(b[j]-a[j]);
			sum+=c[j];
		}
		r=sqrt(sum);
		v=4.0/3.0*PI*pow(r,3);
		printf("%.2f %.2f\n",r,v);		
	}
	return 0;
}

发表于 2020-01-09 19:56:23 回复(0)
其实很简单,注意代码输入的时候不要输错以及细节方面的小错误就好了
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main(){
    int m;
    const double Pi=3.1415926;
    cin>>m;
    for(int i=0;i<m;i++){
        double x1,x2,y1,y2,z1,z2,radius,volume;
        cin>>x1>>y1>>z1>>x2>>y2>>z2;
        radius=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1));
        volume=4*Pi*radius*radius*radius/3;
        cout<<fixed<<setprecision(2)<<radius<<" "<<volume<<endl;
    }
    return 0;
}

发表于 2019-12-31 21:52:28 回复(0)
这个首先PI的值为3.1415926,小数点后位数必须尽可能多,这样才能保证样例全部通过
#include<iostream>
#include<iomanip>
#include<cmath>
const double PI = 3.1415926;
using namespace std;

int main()
{
    int m, i;
    cin>>m;
    for(i=0; i<m; i++)
    {
        int x, y, z, x0, y0, z0;
        double r, v;
        cin>>x>>y>>z>>x0>>y0>>z0;
        r = sqrt( (x-x0) * (x-x0) + (y-y0) * (y-y0) + (z-z0) * (z-z0));
        v = 4.0 / 3 * PI * r * r * r ;
        cout<<setiosflags(ios::fixed)<<setprecision(2)<<r<<" "<<v<<endl;
    }
    return 0;
}


发表于 2019-12-17 15:42:44 回复(0)
/*HI LWQ
author @Rh
time@2019/9/9/13:52
*/
#include <stdio.h>
#include <math.h>
using namespace std;

#define PI 3.1415926

int main() {
	int m;
	int x0, x1, y0, y1, z0, z1;
	while (scanf("%d", &m) != EOF) {
		for (int i = 0; i< m; i++) {
			scanf("%d%d%d%d%d%d", &x0, &y0, &z0, &x1, &y1, &z1);
			double r = sqrt((x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1) + (z0 - z1)*(z0 - z1));
			printf("%.2lf %.2lf\n", r, 4.0 / 3 * PI*r*r*r);
		}
	}
	return 0;
}

发表于 2019-09-09 13:53:06 回复(0)