• UVa 400


    每行最多60个字符
    在尽量少的行中将字符串按字典序向下输出

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    
    using namespace std;
    
    const int maxn = 100;
    
    struct ceil{
        char s[maxn];
        int len;
    };
    struct ceil p[maxn+50], temp;
    
    bool cmp( struct ceil x, struct ceil y )
    {
        if( strcmp( x.s, y.s ) > 0 )    return x.s > y.s;
    }
    
    int main()
    {
        int n, i, j;
        int line, row;
        while( ~scanf("%d",&n) ){
            int M = -1;
            for( i = 0; i < n; i++ ){
                scanf("%s",p[i].s);
                p[i].len = strlen(p[i].s);
                M = p[i].len > M ? p[i].len : M;
            }
            line = 0;
            int M2 = M + 2;
            while( M + M2 <= 60 ){
                line++;
                M += M2;
            }
            line++;
            row = n / line;
            if( row*line < n )
                row++;
            int last = row*line - n;
            sort(p,p+n,cmp);
            puts("------------------------------------------------------------");
            for( i = 0; i < row; i++ ){
                int num = 0;
                for( j = 0; j < line; j++ ){
                    if( n == 0 )    break;
                    if( last > 0 && i > row - last - 1 && j == line-1 )     continue;
                    printf("%s",p[i+num*row].s);
                    for(int k = 0; k < M2 - 2 - p[i+num*row].len; k++ )
                            printf(" ");
                    if(j != line-1 && n != 1 ){
                        if( last > 0  ){
                            if( i > row - last - 1){
                                if(j != line-2 )
                                    printf("  ");}
                            else    printf("  ");
                        }
                        else printf("  ");
                    }
    
                    num++;
                    n--;
                }
                puts("");
            }
    
        }
        return 0;
    }
    
  • 相关阅读:
    小学二年级四则运算软件需求规格说明书
    周活动总结
    构建之法阅读笔记01
    学习进度条01
    四则运算
    软件工程概论
    课后作业1
    继承与多态-课后作业
    python文件处理-将图像根据坐标画矩形标记
    python文件处理-将图像根据坐标切割成若干小图
  • 原文地址:https://www.cnblogs.com/JinxiSui/p/9740638.html
Copyright © 2020-2023  润新知