多組測試數據,每組給出 n 個整數,升序排序後輸出。要求:每行數字間單空格分隔,行末無多餘空格,每行結尾換行。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T; cin >> T;
while (T--) {
int n; cin >> n;
vector<int> a(n);
for (int &x : a) cin >> x;
sort(a.begin(), a.end());
for (int i = 0; i < n; ++i) {
if (i) cout << ' ';
cout << a[i];
}
cout << '\n';
}
return 0;
}
if (i) cout << ' '; 避免行首空格。vector 動態分配。純排序模板題,重點在於輸出格式的精確控制,避免 Presentation Error。