0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[AtCoder備忘録記事]AtCoder Beginner Contest 348 B問題

Posted at

AtCoder Beginner Contest 348 B問題
こちらの問題です。

問題

image.png

入出力例

image.png

解法

二点間の距離のうち最大のもののindexを返す関数を作成し、それぞれの座標に対して関数を実行します。

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int FarthestPointIndex(const vector<vector<int>>& arr, int x1, int y1) {
    int farthestIndex = -1;
    double maxDistance = -1;

    for (int i = 0; i < arr.size(); i++) {
        if (x1 == arr[i][0] && y1 == arr[i][1]) continue;
        //最大距離の計算
        double distance = sqrt(pow(arr[i][0] - x1, 2) + pow(arr[i][1] - y1, 2));
        if (distance > maxDistance) {
            maxDistance = distance;
            farthestIndex = i;
        }
    }

    return farthestIndex + 1;
}

int main() {
    int N;
    cin >> N;
    vector<vector<int>> coords(N, vector<int>(2));
  
    for (int i = 0; i < N; i++) {
        cin >> coords[i][0] >> coords[i][1];
    }
    for (int i = 0; i < N; i++) {
        cout << FarthestPointIndex(coords, coords[i][0], coords[i][1]) << endl;
    }
    
    return 0;
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?