C++与STL部分竞赛题目举例(1):Unix is 命令
C++与STL部分常用函数已经悉数介绍
接下来将列举5道例题。
例题1:Unix Is 命令
using namespace std;const int maxcol = 60;const int maxn = 100 + 5;string filenames[maxn];void print(const string &s, int len){cout << s;for (int i = 0; i < len - s.length(); i++)cout << ' ';}int main(){int n;cin >> n;int M = 0;for (int i = 0; i < n; i++){cin >> filenames[i];M = max(M, (int)filenames[i].length()); //STL max}//计算列数和行数int cols = (maxcol - M) / (M + 2) + 1, rows = (n - 1) / cols + 1;sort(filenames, filenames + n); //STL sort;for (int r = 0; r < rows; r++){for (int c = 0; c < cols; c++){int idx = c * rows + r; //表示出对应位置if (idx < n)print(filenames[idx], c == cols - 1 ? M : M + 2); //巧妙运用问号表达式}cout << endl;}return 0;}
