• Uva




    先计算出最长文件的长度M,然后计算列数和行数,最后输出即可。

    AC代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cctype>
    #include <cstring>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <stack>
    #include <queue>
    
    using namespace std;
    
    const int maxcol = 60;
    const int maxn = 105;
    string filenames[maxn];
    
    // 输出函数,s长度不足len时补字符extra
    void print(const string s, int len, char extra)
    {
    	cout << s;
    	for (int i = 0; i < len - s.length(); i++) {
    		cout << extra;
    	}
    }
    
    int main()
    {
    	ios::sync_with_stdio(false);
    	int n;
    	while (cin >> n) {
    		int M = 0; // 输出的列数
    		for (int i = 0; i < n; i++) {
    			cin >> filenames[i];
    			M = max(M, (int)filenames[i].length());
    		}
    		//计算列数cols和行数rows
    		int cols = (maxcol - M) / (M + 2) + 1;
    		int rows = (n - 1) / cols + 1;
    		print("", 60, '-');
    		cout << endl;
    		sort(filenames, filenames + n);
    		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;
    }




  • 相关阅读:
    centos 编程环境
    git 安装 使用
    nodejs 笔记
    微信开发
    composer 使用笔记
    一:安装centos 7最小编程环境 xfce桌面
    二: 安装centos服务环境软件mysql httpd php
    我的通用程序规范及说明
    常用js代码集
    三 , lnmp 一键包安装使用
  • 原文地址:https://www.cnblogs.com/zhangyaoqi/p/4591592.html
Copyright © 2020-2023  润新知