1038. Recover the Smallest Number (30)

来源:互联网 发布:手机淘宝网的评价管理 编辑:程序博客网 时间:2024/05/07 11:00

字符串采用排序的方式来决定哪个排在前面。比较大小的方式为:a+b<b+a。

代码参考别人。

    #include<iostream>    #include<vector>    #include<string>    #include<algorithm>    using namespace std;    bool cmp(string a, string b)    {        return a+b < b+a;    }    int main()    {        int n;        while(scanf("%d",&n)!=EOF)        {            std::vector<string> number(n);            for(int i = 0; i < n; ++i)                cin>>number[i];            //sort            sort(number.begin(), number.end(), cmp);            //get the first non-zero            string ans;            for(int i = 0; i < n; ++i)                ans += number[i];            int non_zero = -1;            for(int i = 0; i < ans.size(); ++i)                if(ans[i] != '0')                {                    non_zero = i;                    break;                }            if(non_zero == -1)                printf("0\n");            else            {                ans = ans.substr(non_zero);                cout<<ans<<endl;            }        }        return 0;    }


0 0
原创粉丝点击