美文网首页
设有n个正整数,将他们连接成一排,组成一个最大的多位整数。 如:

设有n个正整数,将他们连接成一排,组成一个最大的多位整数。 如:

作者: 蒜苗爱妞妞 | 来源:发表于2018-05-12 17:01 被阅读0次
#include<iostream>
#include<vector>
#include<string>

using namespace std;

class Solution {
public:
    void sort(vector<string> &str_array) {
        int len = str_array.size();
        for (int i = 0; i < len - 1; i++) {
            for (int j = 0; j < len - 1 - i; j++) {
                if ((str_array[j] + str_array[j + 1]) < (str_array[j+1] + str_array[j]))
                    swap(str_array[j], str_array[j + 1]);
            }
        }
    }
};

int main() {
    int tmp;
    while (cin >> tmp) {
        vector<string> str_array;
        string str;
        for (int i = 0; i < tmp; i++) {
            cin >> str;
            str_array.push_back(str);
        }
        Solution s;
        s.sort(str_array);
        for (int i = 0; i < str_array.size(); i++) {
            cout << str_array[i];
        }
        cout << endl;
    }
    return 0;
}

相关文章

网友评论

      本文标题:设有n个正整数,将他们连接成一排,组成一个最大的多位整数。 如:

      本文链接:https://www.haomeiwen.com/subject/umrhdftx.html