1038. Recover the Smallest Number (30)

来源:互联网 发布:数据库第六版中文答案7 编辑:程序博客网 时间:2024/06/07 07:39

比较有趣的题,重点是写出(如果用<,9001,900110组合的9001900110明显比9001109001大)

bool compare(string a, string b){    return a + b < b + a;}

之后就string叠加,去掉开头的0,如果边空了输出0。

#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;bool compare(string a, string b){    return a + b < b + a;}int main(){    vector<string> st;    int N;    cin >> N;    for (int t = 0;t < N;t++)    {        string temp;        cin >> temp;        st.push_back(temp);    }    sort(st.begin(), st.end(), compare);    int i;    string resault = "";    for (auto x : st) resault += x;    auto it = find_if_not(resault.begin(), resault.end(), [](char a) {return a == '0';});    resault.assign(it, resault.end());    if (resault == "") cout << "0" << endl;    else cout << resault << endl;}
0 0
原创粉丝点击