跳到正文

题解

题解 P1012 【拼数】

DarkMoon_Dragon约 1 分钟
原文
文章信息与版本

文章信息

发布
2017年10月14日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 1 分钟
获赞
347
题目
P1012

版本

  1. v1当前
  • upd:20191114.

  • 好不容易三百多个赞了,据说考前发题解 RP++RP++,那就来整理一下以前的萌新码风和描述。大家 CSP2019 RP++CSP2019\ RP++

  • sortsort 排序字符串是不用加cmpcmp的,已经按照字典序重载了大小运算符operatoroperator

  • stringstring类也重载了++操作,a+ba+b意思是aa串拼接在bb串后面

  • 注意sortsort对字符串排序只能是c++c++中stringstring类型

  • 想对charchar数组排序,当然你也可以写一个cmp


  • 对于此题,按长度排序加一个a.lengtha.length就可以了,cmpcmp里returnreturn的条件是返回哪个小
C++
#include <iostream>
#include <algorithm>
using namespace std;
string a[21];
bool cmp(string a, string b)
{
    return a + b > b + a; //a+b就是b接在a的后面
}
int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n; i++)
        cout << a[i];
}