1. 程式人生 > >UVa 10905 - Children's Game 排序,題目沒有說輸入是int 難度: 0

UVa 10905 - Children's Game 排序,題目沒有說輸入是int 難度: 0

user oca output div code out freopen algorithm option

題目

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1846


題意

n個數字按照字符串方式直接組合成大數字,問最大組合成多少

思路

排序

感想

1. 因為腦洞的原因以為只要把所有數的數位填到一樣長就好了,比如27,273,72,723把27填到272,72填到727,就能把它們順利排出來。結果這樣無法區別345, 3453這種情況和543, 5435這種情況。總之為了這種腦洞花費了過多時間。

2. 後來對每個元素x,按照x / (basex - 1)排序

3. 最後發現每個元素可能非常長。。。

代碼

技術分享圖片
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#define
LOCAL_DEBUG using namespace std; typedef pair<double, long long> MyPair; const int MAXN = 51; long long base[19]; string a[MAXN]; bool cmp(string s1, string s2) { return s1 + s2 < s2 + s1; } int main() { #ifdef LOCAL_DEBUG freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt
", "r", stdin); freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout); #endif // LOCAL_DEBUG int T; base[0] = 1; for (int i = 1; i < 19; i++)base[i] = base[i - 1] * 10; int n; for (int ti = 1;cin>>n && n; ti++) { for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n, cmp); for (int i = n - 1; i >= 0;i--) { cout << a[i]; } cout << endl; } return 0; }
View Code

UVa 10905 - Children's Game 排序,題目沒有說輸入是int 難度: 0