UVA 400 Unix ls

字符串,需要注意格式,和样例输出用fc命令比较,一致时就行了。

# include <cstdio>

# include <cstring>

# include <cstdlib>

# include <cmath>



# define N 100 + 5

# define M 60 + 5



int n;

char fname[N][M];



int cmp(const void *x, const void *y)

{

    return strcmp((char*)x, (char*)y);

}

int max(int x, int y)

{

    return x>y ? x:y;

}



int main()

{    

    int T = 0;

    while (~scanf("%d", &n))

    {

        ++T;

        int coll = 0;

        for (int i = 0; i < n; ++i)

        {

            scanf("%s", fname[i]);

            coll = max(coll, strlen(fname[i]));

        }

        qsort(fname, n, sizeof(fname[0]), cmp);

        int cols = 62 / (coll+2);

        if (T != 1) putchar('\n');

        for (int i = 0; i < 60; ++i) putchar('-'); putchar('\n');

        int line = (int)ceil(1.0*n/cols);

        for (int j = 0; j < line; ++j)

        {

            for (int i = 0; i < cols; ++i)

            {

                int t = j+line*i;

                if (t >= n) continue;

                int len = strlen(fname[t]);

                printf(fname[t]);

                if (i==cols-1|| (i<cols-1 && t+line<n)) for (int i = len+1; i <=coll+2 && i <= 62; ++i)

                    putchar(' ');

            }

            if (j<line-1) putchar('\n');

        }

    }

    

    return 0;

}

/**/

你可能感兴趣的:(unix)