想法:

中點座標 = 兩點座標和的一半

故,要判別對稱點是否存在

就判別各中點座標是否相同

 

迴圈

輸入 座標數 及 座標

將座標做排序(先看x再看y)

 

因為最左下角的點其對稱點必為最右上角的點

(左右優先)

 

因此,由外至內找尋中點座標

若皆相同則存在對稱點

若有任一個不同則不存在對稱點

 

 

 

 

#include <iostream>
#include <cstdlib>

using namespace std;

int cmp(const void *addr1, const void *addr2)
{
    const int *a = *(const int **)addr1;
    const int *b = *(const int **)addr2;

    if(a[0] - b[0] == 0)
        return a[1] - b[1];
    else
        return a[0] - b[0];

}

int main()
{
    int n;
    int **coord, *tmp;
    int i, j, x, y;
    bool exist;

    while(cin >> n && n)
    {
        coord = new int* [n*sizeof(int *) + 2*n*sizeof(int)];

        for(i = 0, tmp = (int *)(coord + n); i<n; i++, tmp+=2)
        {
            coord[i] = tmp;
            cin >> coord[i][0] >> coord[i][1];
        }

        qsort(coord, n, sizeof(int *), cmp);

        exist = true;

        x = coord[0][0] + coord[n-1][0], y = coord[0][1] + coord[n-1][1];
        for(i = 0, j = n-1; i<j; i++, j--)
        {
            if(coord[i][0] + coord[j][0] != x || coord[i][1] + coord[j][1] != y)
            {
                exist = false;
                break;
            }
        }

        if(exist)
            cout << "yes\n";
        else
            cout << "no\n";
    }
    
    return 0;
}

arrow
arrow
    全站熱搜

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