此题不能简单对其进行排序,因为32不一定排321前面,后来发现规律:对于最小的序列中的任意两个数 a,b 如果 a在b前面 当且仅当 a在b前面 < b在a前面。 原本是将所有串合并一串,再检测前导0,这样最后一点会超时。干脆直接检测。
AC代码:
//1038 19:12 -19:43
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
char str[10005][10];
// bool cmp(const char[10] &a, const char[10] &b)
// {
//
// return strcmp(a,b)<0;
// }
char res[100005];
int cmp(const void *a,const void *b)
{
char *x=(char *)a;
char *y=(char *)b;
char tmpa[20],tmpb[20];
strcpy(tmpa,x);
strcpy(tmpb,y);
strcat(tmpa,y);//tmpa=xy
strcat(tmpb,x);//tmpb=yx
return strcmp(tmpa,tmpb)>0;//当tmpa<tmpb时,a在b前;当tmpa>tmpb时,b在a前
}
int main()
{
int i,n;
freopen("C:\\Documents and Settings\\Administrator\\桌面\\input.txt","r",stdin);
scanf("%d",&n);
for(i=0;i<n;i++){
getchar();
scanf("%s",str[i]);
}
qsort(str,n,sizeof(str[0]),cmp);
// strcpy(res,str[0]);//把所有串合并成一串,也可以,但会超时
// for(i=1;i<n;i++){
// strcat(res,str[i]);
// }
// int fg=0;
// for(i=0;i<strlen(res);i++){
// if(fg==0&&res[i]!='0'){
// fg=1;
//
// }
// if(fg==1)
// printf("%c",res[i]);
//
// }
// if(fg==0)
// printf("0");
int j,fg=0;
for(i=0;i<n;i++){
for(j=0;str[i][j]!='\0';j++){
if(fg==0&&str[i][j]=='0');
else{
fg=1;
printf("%c",str[i][j]);
}
}
}
if(fg==0)
printf("0");
return 0;
}