zoukankan      html  css  js  c++  java
  • 【紫书】 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;
    }
    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    第十二周总结
    第十一周总结
    第十周总结
    人月神话阅读笔记04
    第九周总结
    第八周总结
    人月神话阅读笔记03
    主成分分析(PCA)算法介绍及matlab实现案例
    Cross-entropy
    压缩算法--TP(1978)
  • 原文地址:https://www.cnblogs.com/SuuT/p/8746430.html
Copyright © 2011-2022 走看看