UVa 400 - Unix ls
2010-02-27 21:02
387 查看
/* coder: ACboy date: 2010-2-27 result: 1A description: UVa 400 - Unix ls */ #include <iostream> #include <string> #include <vector> using namespace std; vector<string> ve(110); // 最长字符串的长度 int maxLen = 0; // 列数 int columnCount; // 行数 int rowCount; // 满列数 int fillColumnCount; // 满行数 int fillRowCount; // 由行列的值计算相应位置的文件名在ve中的位置 int findPos(int x, int y) { int result; if (y <= fillColumnCount) { result = x * rowCount + y; } else { result = rowCount * fillColumnCount + y; } return result; } int main() { int i, j; int n; #ifndef ONLINE_JUDGE freopen("400.txt", "r", stdin); #endif while (cin >> n) { string input; // 本题难点如何动态格式化输出(输出格式是动态变化的) char outputFormat1[10]; char outputFormat2[10]; char d[3]; getline(cin, input); maxLen = 0; for (i = 0; i < n; i++) { getline(cin, input); ve[i] = input; int len = input.size(); if (len > maxLen) maxLen = len; } sort(ve.begin(), ve.begin() + n); // 把maxLen转化成char数组 sprintf(d, "%d", maxLen); // 生成输出格式并保存在outputFormat1数组中(69 - 78行代码) outputFormat1[0] = '%'; outputFormat1[1] = '-'; int c = 2; int len = strlen(d); for (i = 0; i < len; i++) { outputFormat1[c++] = d[i]; } outputFormat1[c++] = 's'; outputFormat1[c] = '/0'; // 生成输出格式并保存在outputFormat2数组中(81 - 91行代码) sprintf(d, "%d", maxLen + 2); outputFormat2[0] = '%'; outputFormat2[1] = '-'; c = 2; len = strlen(d); for (i = 0; i < len; i++) { outputFormat2[c++] = d[i]; } outputFormat2[c++] = 's'; outputFormat2[c] = '/0'; // 计算输出文件名时的列数 columnCount = (60 - maxLen) / (2 + maxLen) + 1; // 计算输出文件名时的行数 for (i = 1;; i++) { if ((i - 1) * columnCount < n && i * columnCount >= n) {rowCount = i; break;} } for (i = 0; i < 60; i++) cout << "-"; cout << endl; // 输出所有的文件名 for (i = 0; i < rowCount; i++) { for (j = 0; j < columnCount; j++) { int temp = j * rowCount + i; if (temp < n) { if (j == columnCount - 1) printf(outputFormat1, ve[temp].c_str()); else printf(outputFormat2, ve[temp].c_str()); } } cout << endl; } } return 0; }
相关文章推荐
- UVA_400: Unix ls
- Unix ls UVA400
- UVa 400 - Unix ls
- UVA 400 Unix ls
- 例题5-8 Unix ls 命令 UVa400
- UVa——400Unix ls(字典序文本处理输出iomanip)
- Uva - 400 - Unix ls
- uva 400 - Unix ls
- UVa 400 Unix的ls命令
- uva 400 Unix ls
- UVa 400 - Unix ls
- UVA400 Unix ls
- [UVA400]Unix ls[格式输出]
- 【例题5-8 UVA - 400】Unix ls
- Uva400 Unix ls 【排序+技巧】【例5-8】
- UVA 400 Unix ls
- uva-400-Unix ls
- #UVA 400 Unix ls (string + sort( ) )
- Unix ls UVA - 400
- UVA - 400 Unix ls