POJ 1589 Unix ls

来源:互联网 发布:阿里云分布式文件系统 编辑:程序博客网 时间:2024/05/01 04:45

看到题目很有熟悉感

刚好最近在玩Linux&Unix

模拟,注意细节


//============================================================================

// Name        : hello.cpp
// Author      : key
// Version     : 8
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================



#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
#include <map>
using namespace std;

#define NUM_INF 0x7FFFFFFF

int cmp(const void* a,const void* b)
{
    char* aa = (char*) a;
    char* bb = (char*) b;
    return strcmp(aa,bb);
}

// void swap_str(char* a,char* b)
// {
//     char temp[110];
//     strcpy(temp,a);
//     strcpy(a,b);
//     strcpy(b,temp);
// }

char str[110][110];
int ls[110][110];

int main()
{
    int n;
    int i,j,k;
    int most_long_word;
    int num_every_line;
    while(scanf("%d",&n)!=EOF&&n)
    {
        most_long_word = 0;
        num_every_line = 1;
        for(i=1;i<=n;i++)
        {
            scanf("%s",&str[i]);
            if(strlen(str[i])>most_long_word)
                most_long_word = strlen(str[i]);
        }
        
        qsort(str[1],n,sizeof(str[0]),cmp);

        int row_num,column_num;
        for(row_num=1;row_num<=n;row_num++)
        {
            column_num=n/row_num;
            if(n%row_num)
                column_num++;
            if((most_long_word+2)*(column_num-1)+most_long_word<=60)
                break;
        }
        int x,y;
        for(i=1,x=1,y=1;i<=n;i++)
        {
            ls[x][y]=i;
            x++;
            if(x>row_num)
            {
                x=1;
                y++;
            }
        }
//         for(i=1;i<=row_num;i++)
//         {
//             for(j=1;j<=column_num;j++)
//             {
//                 printf("%s  ",str[ls[i][j]]);
//             }
//             printf("\n");
//         }
        
        printf("------------------------------------------------------------\n");

        for(i=1;i<=row_num;i++)
        {
            for(j=1;j<=column_num;j++)
            {
                if((j-1)*row_num+i<=n)
                {
                    printf("%s",str[ls[i][j]]);
                    for(k=strlen(str[ls[i][j]]);k< ( j==column_num ?most_long_word:(most_long_word+2)) ;k++)
                        putchar(' ');
                }
            }
            printf("\n");
        }


//         for(i=0;i<n/num_every_line;i++)
//         {
//             for(j=i+1;i*num_every_line+j<n;j++)
//             {
//                 swap_str(str[i*num_every_line+j],str[j*num_every_line+i]);
//             }
//         }
//         for(i=0;i<n;i++)
//         {
//             printf("%s\n",str[i]);
//         }

    }
    return 0;
}
原创粉丝点击