pat甲级1038

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

原地址

思路:

  1. 写一个排序,排序去头部零,输出。

我踩的坑点:

  1. 当最小数是零时会被删除完,导致没有输出

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

using namespace std;
const int maxn = 1e5+5;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
typedef long long ll;
#define pb push_back

vector<string> v;
bool (const string &a,const string &b) {
int maxx = max(a.size(),b.size());
int i = 0;
while (i < maxx) {
if (a[i%a.size()] < b[i%b.size()])
return true;
else if (a[i%a.size()] > b[i%b.size()])
return false;
i++;
}
return true;
}

看到大佬有简便的写法
bool cmp (const string &a,const string &b) {
return a + b < b + a;
}
*/
int main() {
#ifdef ONLIGE_JUDGE
#else
//freopen("H:\in.txt","r",stdin);
#endif // ONLIGE_JUDGE
string s;
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> s;
v.pb(s);
}
sort(v.begin(),v.end(),cmp);
s = "";
for (int i = 0; i < v.size(); ++i) {
s += v[i];
}
while (s[0] == '0' && s.size() - 1 > 0) {
s.erase(s.begin());
}
cout << s << endl;
return 0;
}