Chinaunix首页 | 论坛 | 博客
  • 博客访问: 71886
  • 博文数量: 41
  • 博客积分: 1475
  • 博客等级: 上尉
  • 技术积分: 440
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 22:49
文章分类
文章存档

2012年(8)

2011年(1)

2009年(32)

我的朋友
最近访客

分类:

2009-04-16 20:28:51

/*
 *******************************************************************************
 *
 * Filename: 400.c
 *
 * Description:
 *
 * Version: 0.1
 * Created: 4/7/2009 8:30:02 AM
 *
 * Author: Ye Xiaofeng , yexf # gmail.com
 *
 *******************************************************************************
 */


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void ls_func(int line_num);
int compare(const void *a, const void *b);
void find_row_col(int count, int max_col, int *row, int *col);


int main(int argc, char **argv)
{
    int line_num = 0;
    int rc = 0;


    rc = scanf("%d", &line_num);
    while (rc != -1) {
        ls_func(line_num);
        rc = scanf("%d", &line_num);
    }
}

void ls_func(int line_num)
{
    char line_buffer[100][61];
    int i = 0, j = 0, k = 0;
    int max_len = 0;
    int cur_len = 0;
    int cul_num = 0;
    int row_num = 0;
    char cel_buffer[61];
    int count = 0;
    int flag = 0;
    int last_col_len = 0;


    memset(cel_buffer, 0, 61);

    for (i = 0; i < line_num; i++) {
        scanf("%s", &line_buffer[count][0]);
        for (j = 0; j < count; j++) {
            if (0 == strcmp(line_buffer[j], line_buffer[count])) {
                flag = 1;
                break;
            }
        }
        if (!flag) {
            cur_len = strlen(line_buffer[count]);
            if (max_len < cur_len) {
                max_len = cur_len;
            }
            count++;
        }
    }

    qsort(line_buffer, count, 61, compare);

    find_row_col(count, (60+2)/(max_len+2), &row_num, &cul_num);
    last_col_len = 60 - (cul_num-1)*(max_len+2);
    printf("------------------------------------------------------------\n");
    for (j = 0; j < row_num; j++) {
        k = 0;
        for (i = j; i < count; i+=row_num) {
            if (k == cul_num-1) {
                /* Last column */
                memset(cel_buffer, ' ', max_len);
                memcpy(cel_buffer, line_buffer[i], strlen(line_buffer[i]));
            } else {
                memset(cel_buffer, ' ', max_len+2);
                memcpy(cel_buffer, line_buffer[i], strlen(line_buffer[i]));
            }
            printf("%s", cel_buffer);
            k++;
        }
        printf("\n");
    }
}

int compare(const void *a, const void *b)
{
    const char *v1 = a;
    const char *v2 = b;

    return strcmp(v1, v2);
}

void find_row_col(int count, int max_col, int *row, int *col)
{
    *row = 1;
    *col = count;

    while (*col > max_col) {
        (*row)++;
        if (count % (*row) == 0) {
            *col = count / (*row);
        } else {
            *col = count / *row + 1;
        }
    }
}

阅读(315) | 评论(0) | 转发(0) |
0

上一篇:ACM UVA (401)

下一篇:ACM UVA (701)

给主人留下些什么吧!~~