ACM篇:POJ 1589 -- Unix Is

挺有趣的模拟。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>

#define isokey(t) (isdigit(t) || isalpha(t) || ((t)=='.') || ((t)=='_') || ((t)=='-'))
#define max(a,b) (((a) > (b)) ? (a) : (b)) 
using namespace std;
const int MAXN = 100;
const int MAXL = 60;
struct FileName
{
    int id;
    int len;
    char *s;
    FileName(int id=0, int len=0, char *s=NULL)
    {
        this->id = id;
        this->len = len;
        this->s = s;
    }
};

int readchar()
{
    int t;
    while ((t=getchar()) != EOF)
    {
        if (isokey(t))
        {
            return t;
        }
        if (t == '\n')
        {
            return -1;
        }
    }
}
int n;
FileName filename[MAXN+2];
char tempname[MAXN+2][MAXL+2];

void read_filename()
{
    memset(filename, 0, sizeof(filename));
    memset(tempname, 0, sizeof(tempname));

    for (int i = 1; i <= n; i++)
    {
        int l = 0;
        int ch;
        while (ch = readchar())
        {
            if (ch == -1)
            {
                break;
            }
            tempname[i][l++] = ch; 
        }
        tempname[i][l] = '\0';
        filename[i] = FileName(i, l, tempname[i]);
    }
}

bool _cmp(FileName a, FileName b)
{
    return (strcmp(a.s, b.s) == -1);
}

int find_max()
{
    int max_len = 0;
    for (int i = 1; i <= n; i++)
    {
        max_len = max(max_len, filename[i].len);
    }
    return max_len;
}

void _print()
{   
    for (int i = 1; i <= MAXL; i++)
    {
        putchar('-');
    }
    putchar('\n');

    int max_len = find_max();
    int col = (MAXL+2) / (max_len+2);
    int row = (n%col == 0) ? (n/col) : (n/col + 1);

    for (int i = 1; i <= row; i++)
    {
        for (int j = 1; j <= col; j++)
        {
            int id = (j-1)*row + i;
            if (id <= n)
            {
                printf("%-*s", max_len + ((j==col)? 0 : 2), filename[id].s);
            }
        }
        putchar('\n');
    }

}
int main()
{
    while (scanf("%d", &n) != EOF)
    {
        getchar();
        read_filename();
        sort(filename+1, filename+n+1, _cmp);
        _print();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值