[ACM]Equilateral triangle


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
1 2 3 
6 7 8  
7 9 18 
5 10 13
Sample Output
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值