1038 Recover the Smallest Number (30 point(s))

版权声明:听说这里让写版权声明~~~ https://blog.csdn.net/m0_37691414/article/details/86688336

题解

找一个最小的排列。此题我是蒙的,没有证明。没看过sort的源码,有兴趣的可以去读一下sort源码可能就会知道缘由。

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
vector<string> ans;
bool cmp(const string& a, const string& b) {
	return a + b < b + a;
}
int main() {
	int n;
	scanf("%d", &n);
	ans.resize(n);
	for(int i = 0; i < n; ++i) cin >> ans[i];
	sort(ans.begin(), ans.end(), cmp);
	string res = "";
	for(int i = 0; i < ans.size(); ++i) res += ans[i]; //别忘记可能全部是0串的情况. 
	while(res.size() != 0 && res[0] == '0') res.erase(res.begin());
	if(res.size()) cout << res;
	else printf("0");
	return 0; 
}

猜你喜欢

转载自blog.csdn.net/m0_37691414/article/details/86688336