as12350991 2020-09-20 05:57 采纳率: 33.3%
浏览 50
已采纳

在线求助问题。初学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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页