先想到 sort,但是cmp不对;又想到深搜,但是想不到判断退出的条件,把所有结果保存下来,再怎么比较找出最小的也不行;最后 ref:
http://tech-wonderland.net/blog/pat-1038-recover-the-smallest-number.html
1: sort 的用法
2:string的用法,特别是用substr直接去除前导0
#include <stdio.h>
#include <string>
#include <iostream>
#include <algorithm>
#define max 10000+5
using namespace std;
string seg[max];
int n;
bool cmp(const string a, const string b){ //cmp是返回bool,而且参数是const type,或者可用 const int &a,函数体重直接用a
string ab = a+b; //拼接字符串, ab<ba,则a就应该排在b前面
string ba = b+a;
return (ab<ba);
}
int main(){
//freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i = 0; i < n; i++){
cin>>seg[i];
}
sort(seg,seg+n,cmp);//指针seg, seg+n
//第一个串如果以0开头,需去除前导0,直至第一个非0出现;对于全0的,要输出一个0
//使用substring很方便
string res = "";
for(int i = 0; i < n; i++){
res += seg[i];
}
int nonZeroPos = 0;
while(res[nonZeroPos] == '0'){
nonZeroPos++;
}
if(nonZeroPos < res.length()){
cout<<res.substr(nonZeroPos);
}else{//全是0
printf("0");
}
return 0;
}