题意分析:你现在手头上有一大堆文件名,你要做的就是把这些文件名按一定的格式输出。输出规则为:1.第一行为60个'-'。2.以文件名中的宽度最大的那个作为标准宽度(标准宽度 = 最长宽度 + 2)。文件名不能超过60个字符的长度(即:不能有字符出现在'-'所列的的范围之外,但是空格可以。)3.文件名需要按字母序。4.要求输出的行数最少,且按列顺序输出文件名
解题思路:让每行输出最多的文件名数就行了,注意文件名长度为60的情况
个人感受:输出格式一直没控制好,老觉得自己控制格式太慢了,硬是上网查了格式输出orz其实这样真心不好,不过也算是学习了
具体代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
using namespace std;
const int MAXN = 1e6 + 111;
string name[110];
int main()
{
//freopen("C:\\Users\\apple\\Desktop\\in.txt", "r", stdin);
int n;
while (cin >> n)
{
int mx = 0;
for (int i = 0; i < n; ++i)
{
cin >> name[i];
if (name[i].length() > mx) //记录最大名字长度
mx = name[i].length();
}
//cout << mx << endl;
sort(name, name + n);
mx += 2;
int r = 62 / mx; //每行放几个
//cout << r << endl;
int len = n / r + (n % r != 0); //放几行
//cout << len << '\n';
cout << "------------------------------------------------------------\n";
for (int j = 0; j < len; ++j)
{
for (int i = j; i < n; i += len)
{
cout.width(mx); //设置输出宽度
cout << setiosflags(ios::left) << name[i]; //左对齐
}
cout << '\n';
}
}
return 0;
}