题意:中文版https://vjudge.net/problem/UVA-400#author=Zsc1615925460
题解:首先读取字符,维护一个最长字符串长度M,再排序。
对于输出,写一个print(string s,int len,char c) 函数,用来输出s,不足len的用c补齐。
关于竖着输出,用一个idx算出这个位置应该放第几个元素。
坑:需要在输出前加一句 if(idx<n),否则会多输出很多空格???
#include <cstdio> #include <cstring> #include <queue> #include <iostream> #include<string> #include<algorithm> using namespace std; const int maxcol = 60; const int maxn = 100 + 5; string nms[maxn]; void print(string s, int len, char ex) { cout << s; for (int i = 0; i < len - s.length(); i++){ cout << ex; } } int main() { int n; while (cin >> n) { int M = 0; for (int i = 0; i < n; i++) { cin >> nms[i]; M = max(M, (int)nms[i].length()); } int col = (maxcol - M) / (M + 2) + 1, row = (n-1) / col+1;//只有一行 print("", 60, '-'); cout << endl; sort(nms, nms + n); for (int r = 0; r < row; r++) { for (int c = 0; c < col; c++) { int idx = c*row+ r; if(idx<n)print(nms[idx],c==col-1? M:M+2, ' '); } cout << endl; } } return 0; }