# UVa 400 - Unix ls --- # 題目大意 有多筆輸入,每次給n個字串,按照字典序排序後照格式從上到下、從左到右輸出。每列不能超過60個字元。每個字串要佔最長字串長度加2的空間。每筆輸出前用60個'-'隔開。 --- # 題解 輸出的部分可以用printf("%-*s") 滿足要求,所以只要紀錄最長長度計算每列長就好。然後我現在才知道printf不能輸出string。因為printf是C的,用來輸出Char數組,而string是一個容器,所以要用c_str()轉換後才能用printf。Orz --- ```=C++ #include <bits/stdc++.h> #define ll long long #define pb push_back #define pf push_front #define ft first #define sec second #define pr pair<int,int> #define ISCC ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std; int t ,n ,m ,pos ,mx ,r ,c; string s; vector<string> a; int main() { while(cin >> n) { mx = 0; a.clear(); for(int i=0 ;i<n ;i++) { cin >> s; a.pb(s); mx = max(mx ,(int)s.size()); } sort(a.begin() ,a.end()); c = max(60/(mx+2) ,1); r = n/c + (n%c!=0); for(int i=0 ;i<60 ;i++) cout << "-"; cout << '\n'; for(int i=0 ;i<r ;i++ ,cout << '\n') for(int j=0 ;j<c ;j++) { if(i+j*r>=n) break; printf("%-*s" ,mx+2 ,a[i+j*r].c_str()); } } return 0; } ```