輸入 4 邊,並小到大排序

若 4 邊等長,則為正方形

若小的 2 邊等長,且大的 2 邊等長,則為長方形(矩形)

若 小的 2 邊和  < 大的 2 邊差, 則可構成 4 邊形

 

為什麼呢?

首先,我們將四邊形分成 2 個三角形,一個包含小的 2 邊,另一個包含大的 2 邊

假設小的 2 邊為 a b ,大的 2 邊為 c d , 且 a <= b <= c <= d

另外共同邊為 x   , 則 2 三角形的邊分別為 abx 和 cdx

 

以小三角形的觀點來看, x < a+b

以大三角形的觀點來看, x > d - c

因此 ,  d - c < x < a + b   => d - c < a + b  

 

#include<iostream>
#include<algorithm>

using namespace std;


int main(){

    int border[4];
    int n;

    while(cin >> n)
        while(n--){

        for(int i = 0; i<4; i++)
            cin >> border[i];

        sort(border,border + 4);

        if(border[0] == border[1] && border[1] == border[2] && border[2] == border[3])
            cout << "square\n";
        else if( border[0] == border[1] && border[2] == border[3] )
            cout << "rectangle\n";
        else if( border[0] + border[1] > border[3] - border[2])
            cout << "quadrangle\n";
        else
            cout << "banana\n";

    }

    return 0;
}
 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大神(偽) 的頭像
    大神(偽)

    大神的世界

    大神(偽) 發表在 痞客邦 留言(0) 人氣()