-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnix ls.cpp
More file actions
49 lines (46 loc) · 970 Bytes
/
Unix ls.cpp
File metadata and controls
49 lines (46 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
题目:
输入正整数n,以及n个文件名,排序后按列有限的方式左对齐输出。假设最长文件名有M字符,则最右列有M字符,其他列都是M+2字符。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
const int maxcols = 60;
const int maxn = 100 + 5;
string filenames[maxn];
void print(const string & s, int len, char extra)
{
cout << s;
for(int i = 0; i < len-s.length(); i++)
cout << extra;
}
int main()
{
int n;
FILE* fin = freopen("data.in", "r", stdin);
while(cin >> n)
{
int M = 0;
for(int i = 0; i < n; i++)
{
cin >> filenames[i];
M = max(M, (int)filenames[i].length());
}
int cols = (maxcols - M ) / (M + 2) + 1, rows = (n-1) / cols + 1;
sort(filenames, filenames+n);
print("", 60, '-');
cout << "\n";
for(int r = 0; r < rows; r++)
{
for(int c = 0; c < cols; c++)
{
int idx = c * rows + r;
print(filenames[idx], c == cols-1? M:M+2, ' ');
}
cout << "\n";
}
}
return 0;
}