算法习题---5-6对称轴(UVa1595)

一:题目

判断平面上的一组点,是否关于一条竖线对称。即找到一条垂直对称轴

(一)样例输入

3
5
-2 5
0 0
6 5
4 0
2 3
4
2 3
0 4
4 0
0 0
4
5 14
6 10
5 10
6 14

(二)样例输出

YES
NO
YES

二:代码实现

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>

using namespace std;

#define MAX_LEN 1000

typedef struct Pot
{
    double x;
    double y;
    bool operator<(struct Pot& other)const
    {
        return x < other.x;
    }
}Pots;

int main()
{
    freopen("data5_6_h.in", "r", stdin);
    freopen("data5_6_h.out", "w", stdout);

    Pots pos[MAX_LEN];
    double check_pos;

    int total_num, num,i;
    cin >> total_num;
    while (total_num--)
    {
        cin >> num;        //信息获取
        for (i = 0; i < num; i++)
            cin >> pos[i].x >> pos[i].y;

        sort(pos, pos + num);  //排序后我们只需要找到中心点(最靠近中间的那两个,或者那一个点)即可check_pos
        if (num % 2 == 0)
            check_pos = (pos[num / 2 - 1].x + pos[num / 2].x) / 2;
        else
            check_pos = pos[num / 2].x;
        
        bool flag=true;
        for (int i = 0; i < num / 2; i++)        //进行信息校验
            if (pos[i].y != pos[num-i-1].y || (pos[i].x + pos[num-i-1].x) / 2 != check_pos)
            {
                flag = false;
                break;
            }
        if (flag)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }

    freopen("CON", "r", stdin);
    freopen("CON", "w", stdout);
    return 0;
}

 

转载于:https://www.cnblogs.com/ssyfj/p/11547773.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值