3183. 坐标排序

Fifnmar

这题的题面太鬼畜了。

#include <algorithm>
#include <cmath>
#include <cstdio>

struct Point {
    int x, y;
} a[100];

inline int get_int() {
    int ans = 0;
    signed char ch = getchar();
    bool fuvu = false;
    while (ch < '0' || '9' < ch) {
        if (ch == '-')
            fuvu = true;
        ch = getchar();
    }
    while ('0' <= ch and ch <= '9') {
        ans = ans * 10 + ch - '0';
        ch = getchar();
    }
    return fuvu ? -ans : ans;
}

int main() {
    int cas = get_int();
    for (int t = 0; t < cas; ++t) {
        int n = get_int();
        for (int i = 0; i < n; ++i) {
            a[i].x = get_int();
            a[i].y = get_int();
        }
        std::sort(a, a + n, [](Point a, Point b) {
            double dis_a = hypot(a.x, a.y);
            double dis_b = hypot(b.x, b.y);
            if (dis_a < dis_b)
                return true;
            if (dis_b < dis_a)
                return false;
            int a_quadrant = a.y < 0 ? 0 < a.x ? 4 : 3 : a.x < 0 ? 2 : 1;
            int b_quadrant = b.y < 0 ? 0 < b.x ? 4 : 3 : b.x < 0 ? 2 : 1;
            if (a_quadrant < b_quadrant)
                return true;
            if (b_quadrant < a_quadrant)
                return false;
            return abs(a.x) < abs(b.x);
        });
        printf("case #%i:\n", t);
        for (int i = 0; i < n; ++i)
            printf("(%i,%i)%c", a[i].x, a[i].y, i < n - 1 ? ' ' : '\n');
    }
}
你当前正在回复 博客/题目
存在问题!