1038 Recover the Smallest Number
程序员文章站
2024-03-17 14:26:28
...
解题代码
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(string a, string b) {
return a + b < b + a;
}
int main() {
int num;
cin >> num;
string figure[10000], ans;
for (int i = 0; i < num; i++) cin >> figure[i];
sort(figure, figure + num, cmp);
for (int i = 0; i < num; i++) ans += figure[i];
while(ans[0] == '0') ans.erase(ans.begin());
if (ans.size() == 0) cout << 0;
else cout << ans;
return 0;
}
测试结果
问题整理
1.简单贪心。
2.string 还是和iostream一起用比较好,省了cat和len等复杂处理,还可以+=,重载强大。
3.通过erase很方便去除前导零。
推荐阅读
-
1038 Recover the Smallest Number
-
PAT A1038 Recover the Smallest Number (30分)
-
[PAT-A 1038]Recover the Smallest Number
-
1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance
-
PAT A1038 Recover the Smallest Number
-
PAT_A 1038. Recover the Smallest Number (30)
-
A1038 Recover the Smallest Number [贪心]
-
1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance
-
PAT(A)1038 Recover the Smallest Number (30分)(神奇的sort用法)
-
PAT A1038 Recover the Smallest Number (30)