2031. 绝对值排序

BoxunWang

为什么就是wrong answer 啊
好长[em:14]

10185101178

while(scanf(“%d”,&n)!=EOF)

Deuchie

注意检查 EOF。这种输入不定项的题都要注意这一点。

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

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

int main() {
    int n;
    while ((n = get_int()) != NO_MORE) {
        if (n == 0) {
            putchar('\n');
            continue;
        }
        int *const nums = new int[n], *const ed = nums + n;
        for (int *i = nums; i != ed; ++i)
            *i = get_int();
        std::sort(nums, ed, [](int a, int b) { return abs(a) < abs(b); });
        printf("%i", *nums);
        for (int *i = nums + 1; i != ed; ++i)
            printf(" %i", *i);
        putchar('\n');
        delete[] nums;
    }
}
你当前正在回复 博客/题目
存在问题!