• 【紫书】 Unix ls UVA


    题意:中文版https://vjudge.net/problem/UVA-400#author=Zsc1615925460

    题解:首先读取字符,维护一个最长字符串长度M,再排序。

       对于输出,写一个print(string s,int len,char c)  函数,用来输出s,不足len的用c补齐。

       关于竖着输出,用一个idx算出这个位置应该放第几个元素。

    坑:需要在输出前加一句 if(idx<n),否则会多输出很多空格???

    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <iostream>
    #include<string>
    #include<algorithm>
    using namespace std;
    const int maxcol = 60;
    const int maxn = 100 + 5;
    string nms[maxn];
    void print(string s, int len, char ex) {
        cout << s;
        for (int i = 0; i < len - s.length(); i++){
        cout << ex;
    }
    }
    int main()
    {
        int n; 
        while (cin >> n) {
            int M = 0;
            for (int i = 0; i < n; i++) {
                cin >> nms[i];
                M = max(M, (int)nms[i].length());
            }
            int col = (maxcol - M) / (M + 2) + 1, row = (n-1) / col+1;//只有一行
            print("", 60, '-'); cout << endl;
            sort(nms, nms + n);
            
            for (int r = 0; r < row; r++) {
                for (int c = 0; c < col; c++) {
                    int idx = c*row+ r;
                    if(idx<n)print(nms[idx],c==col-1? M:M+2, ' ');
                }
                cout << endl;
            }
        }
        return 0;
    }
    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    关于ajax入门案例
    关于idea maven工程创建struts2入门配置及案例
    hibernate关于多对多注解配置
    hibernate关于一对一注解配置
    hibernate批量处理数据
    HQL链接查询
    关于hibernate组件配置
    VS2010 项目属性的默认包含路径设置方法
    VC++的全局变量(转)
    调用文字在位编辑器
  • 原文地址:https://www.cnblogs.com/SuuT/p/8746430.html
Copyright © 2020-2023  润新知