【題解】ZeroJudge d385: 10905 – Children’s Game

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d385

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(int a, int b) {
  return (stoll(to_string(a)+to_string(b)) > stoll(to_string(b)+to_string(a))); 
}

int main() {
  int N, buf;
  
  while (cin >> N) {
    if (N == 0) break;
    
    vector<int> v;
    for (int i=0; i<N; i++) {
      cin >> buf;
      v.push_back(buf);
    }
    sort(v.begin(), v.end(), cmp);
    for (auto i:v) {
      cout << i;
    }
    cout << endl;
  }
  return 0;
}

Python code (credit: Amy Chou)

def my_sort(a):
    n = len(a)
    for i in range(n-1):
        for j in range(i+1, n):
            if a[i]+a[j] < a[j]+a[i]:
                a[i], a[j] = a[j], a[i]
    return a
    
    
while True:
    N = int(input().strip())
    if N == 0:
        break
    
    nums = input().strip().split()
    
    nums = my_sort(nums)
    print("".join(nums))
分享本文 Share with friends