There is an infinite equilateral triangle gridding , numbering vertices(crossing points) as shown in figure below.Some three vertices of them form an equilateral triangle,for example,{1,2,3},{7,9,18},etc.These equilateral triangles must satisfy that each edge of the equilateral triangle is a part of a line of the equilateral triangle gridding.Thus vertices{5 10 13} are not vertices of an equilateral triangle. Your task is to point out whether three given vertices form an equilateral triangle.
Input Input is a sequence of lines, each line containing three integers:i,j,k , indicating numbers of 3 vertices. You may assume that 1>i,j,k<2 31 .
Output For each line of input, output one line “i j k are vertices of an equilateral triangle.” if vertices {i,j,k} form an equilateral triangle,otherwise, output one line “i j k are not vertices of an equilateral triangle.”
Sample Input
Sample Output1 2 3 6 7 8 7 9 18 5 10 13
1 2 3 are vertices of an equilateral triangle. 6 7 8 are not vertices of an equilateral triangle. 7 9 18 are vertices of an equilateral triangle. 5 10 13 are not vertices of an equilateral triangle.
#include <iostream>
#include <cmath>
bool IsEquTri(int i, int j, int k);
int row(int n);
int main()
{
using namespace std;
int i, j, k;
while(cin >> i >> j >> k)
{
if(IsEquTri(i, j ,k))
cout << i << j << k << " are vertices of an equilateral triangle.\n";
else
cout << i << j << k << " are not vertices of an equilateral triangle.\n";
}
return 0;
}
bool IsEquTri(int i, int j, int k)
{
int row_i = row(i), row_j = row(j), row_k = row(k);
int col_i, col_j, col_k;
col_i = i-(row_i-1)*row_i/2;
col_j = j-(row_j-1)*row_j/2;
col_k = k-(row_k-1)*row_k/2;
if( (col_i==col_j && row_j==row_k && row_j-row_i==col_k-col_j) || //正三角
(row_i==row_j && col_j==col_k && col_j-col_i==row_k-row_i) ) //倒三角
return true;
else
return false;
}
int row(int n)
{
int r;
r = sqrt(2*n);
while(2*n > r*(r+1))
r++;
return r;
}