【NOIP考前題目回顧】Luogu P1012

思路

對ASCII碼熟悉的很快就可以想出做法,無非就是字符串拼接然後排序,只要排序規則明瞭的話排序工作就直接給STL做就好了。(這個題卡了我旁邊一哥們一下午)

代碼

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>

int nextInt()
{
    int num = 0;
    char c;
    bool flag = false;
    while ((c = std::getchar()) == ' ' || c == '\r' || c == '\t' || c == '\n');
    if (c == '-')
        flag = true;
    else
        num = c - 48;
    while (std::isdigit(c = std::getchar()))
        num = num * 10 + c - 48;
    return (flag ? -1 : 1) * num;
}

std::string a[101];

bool cmp(const std::string s1, const std::string s2)
{
    std::string s3 = s1 + s2;
    std::string s4 = s2 + s1;
    return s3 < s4;
}

int n;

int main(int argc, char **argv)
{
    std::ios_base::sync_with_stdio(false);
    std::cin >> n;
    for (int i = 1; i <= n; i++)
        std::cin >> a[i];
    std::sort(a + 1, a + n + 1, cmp);
    for (int i = n; i >= 1; i--)
        std::cout << a[i];
    std::cout << std::endl;
#ifdef __EDWARD_EDIT
    std::cin.get();
    std::cin.get();
#endif
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章