ACM UVA - 400 Unix ls

#include <iostream>
#include <string>
#include <algorithm>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

using namespace std;

void print_string(string& s, int len, char extra)
{
	cout << s;
	for(int i = 0; i < len-s.length(); i++)
		cout << extra;
}

const int maxn = 101;
const int maxchar = 60;

int main(int argc, char** argv) {
	int n;
	int max_length=0;
	int col, row;
	string s[maxn];
	while(cin >> n)
	{
		for(int i=0; i<n; i++)
		{
			cin >> s[i];
			if(max_length<s[i].length())
				max_length = s[i].length();
		}
		sort(s, s+n);
		col = maxchar/(max_length+2);
		cout << col << "\n";
		row = (n-1)/col+1;
		cout << "------------------------------------------------------------" << "\n"; 
		for(int i=0; i<row; i++)
		{
			for(int j=0; j<col; j++)
			{
				print_string(s[j*row+i], j==col-1?max_length:max_length+2, ' ');
//				cout << s[j*row+i] << "\n";
			}
			cout << "\n";
		}
	}
	return 0;
}

Runtime error 主要是熟悉string的用法

第一次:8.17

猜你喜欢

转载自blog.csdn.net/yestin_L/article/details/81751233