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

2018-05-12  本文已影响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;
}
上一篇 下一篇

猜你喜欢

热点阅读