pat 1038 Recover the Smallest Number

大一上玩熟练的linux现在又是一团浆糊,从头学命令行花了我一个小时QAQ

wen ti bu da

bu,hen da

anyway~
这个题一开始可能在想某种规律234 23 //534 53 的排序就不一样

但实际上这个题是想麻烦了...其实只需要一个排序就ok了,而排序的方法就也很明显,就a+b >b+a就好了

这个题有一个注意点就是前导零,如 00 000 000这种,直接输出0 (这块需要判断)


#include <algorithm>
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
bool cmp(string a,string b){
    return a+b<b+a;
}
int main(){
    int n;
    string str[10020];
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        cin>>str[i];
    }
    sort(str,str+n,cmp);
    string ans="";
    for(int i=0;i<n;i++){
        ans+=str[i];
    }
    while(ans[0]=='0'&&ans.size()!=0){
        ans.erase(ans.begin());
    }
    if(ans.size()==0)
        cout<<0;
    else{
        cout<<ans;
    }
    return 0;

}

posted @ 2021-07-23 09:43  安之若醇  阅读(24)  评论(0编辑  收藏  举报
Live2D服务支持