代码求调
  • 板块学术版
  • 楼主Ellison2012
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/5/9 18:15
  • 上次更新2024/5/9 20:20:46
查看原帖
代码求调
1022959
Ellison2012楼主2024/5/9 18:15
#include <bits/stdc++.h>
using namespace std;

// 计算两点之间的距离
double distance(int x1, int y1, int x2, int y2) {
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}

int main() {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    
    int oil_station_x = x1; // 油田在 x 轴上
    int oil_station_y = 0;  // 油田在 y 轴上,即油田的 y 坐标为 0
    
    // 计算起点到油田的距离
    double distance_to_oil_station = distance(x1, y1, oil_station_x, oil_station_y);
    // 计算油田到终点的距离
    double distance_to_destination = distance(oil_station_x, oil_station_y, x2, y2);
    // 计算最短距离
    double shortest_distance = distance_to_oil_station + distance_to_destination;
    
    cout << fixed << setprecision(2) << shortest_distance << endl;

    return 0;
}

nana老师在拜访面条老师的路上,她的车子小桃su7快没油了。她需要去找油田获得油。可以假设nana老师现在在坐标 ( ? 1 , ? 1 ) (x 1 ​ ,y 1 ​ ) 上,面条老师的奢华别墅在 ( ? 2 , ? 2 ) (x 2 ​ ,y 2 ​ ),油田在 ? 轴上。

因此nana需要先去油田然后再去面条老师的别墅。求nana需要走的最短距离。

输入格式 第一行包含四个整数 ? 1 , ? 1 , ? 2 , ? 2 x 1 ​ ,y 1 ​ ,x 2 ​ ,y 2 ​ 。

输出格式 输出只有一个小数,保留两位小数。

输入数据 1

1 1 1 2

输出数据 1

3.00

样例解释

nana先去 (1,0) 处加油,再去 ( 1 , 2 ) 处拜访面条老师。

测评数据规模 对于 100 % 100% 的数据, 1 ≤ ? 1 , ? 1 , ? 2 , ? 2 ≤ 1 × 1 0 6 1≤x 1 ​ ,y 1 ​ ,x 2 ​ ,y 2 ​ ≤1×10 6 。

2024/5/9 18:15
加载中...