公式

A - Four Points 解説 by en_translator


Given three points \((x_1, y_1), (x_2, y_2)\), and \((x_3, y_3)\), how can we find the other point \((x_{ans}, y_{ans})\) of the rectangle?

First, let us find \(x_{ans}\). Since \(x_1, x_2\), and \(x_3\) are three \(x\)-coordinates of different three vertices of the rectangle in the statement, two of \(x_1, x_2\), and \(x_3\) are equal. The other one, which has a different value than these two, is equal to \(x_{ans}\). Therefore, exactly one of \(x_1 = x_2\), \(x_2 = x_3\), and \(x_3 = x_1\) holds and

  • if \(x_1 = x_2\), then \(x_{ans} = x_3\);
  • if \(x_2 = x_3\), then \(x_{ans} = x_1\);
  • if \(x_3 = x_1\), then \(x_{ans} = x_2\).

The same applies for \(y_{ans}\). Exactly one of \(y_1 = y_2\), \(y_2 = y_3\), and \(y_3 = y_1\) holds and

  • if \(y_1 = y_2\), then \(y_{ans} = y_3\);
  • if \(y_2 = y_3\), then \(y_{ans} = y_1\);
  • if \(y_3 = y_1\), then \(y_{ans} = y_2\).

Therefore, you can get Accepted by computing \(x_{ans}, y_{ans}\) by the procedures above based on the given \((x_1, y_1), (x_2, y_2), (x_3, y_3)\) and print it. In order to check if “\(x_1 = x_2\)” and so on, we can use a standard feature of programming language, a conditional branch (like if statements).

Here is a sample code in C++.

#include <iostream>
using namespace std;

int main(void)
{
  int x_1, x_2, x_3, y_1, y_2, y_3, x_ans, y_ans;
  
  cin >> x_1 >> y_1;
  cin >> x_2 >> y_2;  
  cin >> x_3 >> y_3;
  
  if(x_1 == x_2) x_ans = x_3;
  if(x_2 == x_3) x_ans = x_1;  
  if(x_3 == x_1) x_ans = x_2;  
  
  if(y_1 == y_2) y_ans = y_3;
  if(y_2 == y_3) y_ans = y_1;  
  if(y_3 == y_1) y_ans = y_2;
  
  cout << x_ans << " " << y_ans << endl;
  
  return 0;
}

投稿日時:
最終更新: