as12350991 2020-09-20 05:44 采纳率: 50%
浏览 46
已采纳

在线求助问题。初学C++ 有一道题不会写。想求助学习。

Write a program that calculates the distance of 2 points from the origin, as well as from each other. The distance between 2 points can be calculated using the formula:
d=√(〖(x_2-x_1)〗^2+〖(y_2-y_1)〗^2+〖(z_2-z_1)〗^2 )

  1. Read the coordinates of 2 3D points from the user. A point is represented by its x, y, and z coordinates.
  2. Calculate and print the distance of the first point from the origin, (0, 0, 0).
  3. Calculate and print the distance of the second point from the origin.
  4. Calculate and print the distance between the two points.
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-09-20 09:05
    关注

    这不是都有公式了么

    #include <iostream>
    #include <math>
    using namespace std;
    
    double dist(double x1,double x2,double y1,double y1,double z1,double z2)
    {
    return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1));
    }
    int main()
    {
    double x1,x2,y1,y2,z1,z2;
    cin >> x1 >> y1 >> z1;
    cin >> x2 >> y2 >> z2;
    cout << dist(x1,0,y1,0,z1,0) << endl; 
    cout << dist(x2,0,y2,0,z2,0) << endl; 
    cout << dist(x1,x2,y1,y2,z1,z2) << endl; 
    return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)