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

2012年(8)

2011年(1)

2009年(32)

我的朋友
最近访客

分类:

2009-04-16 20:47:53

/*
 *******************************************************************************
 *
 * Filename: 10138.c
 *
 * Author: Ye Xiaofeng , yexfeng@gmail.com
 *
 *******************************************************************************
 */


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

typedef struct {
    char license_num[21];
    char capture_time[13];
    int flag; /* enter(0) or exit(1) */
    int length;
} capture;

int setup_capture(capture *cap, char *buffer)
{
    char *token;

    token = strtok(buffer, " \n");
    strcpy(cap->license_num, token);

    token = strtok(NULL, " \n");
    strcpy(cap->capture_time, token);

    token = strtok(NULL, " \n");
    if (0 == strcmp(token, "enter")) {
        cap->flag = 0;
    } else if (0 == strcmp(token, "exit")) {
        cap->flag = 1;
    } else {
        return -1;
    }

    token = strtok(NULL, " \n");
    cap->length = atoi(token);

    return 0;
}

int compare_license(const void *capture1, const void *capture2)
{
    capture *cap1 = (capture *)capture1;
    capture *cap2 = (capture *)capture2;
    char *p1;
    char *p2;

    p1 = cap1->license_num;
    p2 = cap2->license_num;
    while (*p1 != '\0' && *p2 != '\0') {
        if (*p1 != *p2) {
            break;
        }
        p1++;
        p2++;
    }
    /* XXX */
    if (*p1 != *p2) {
        return (*p1 - *p2);
    }
    p1 = cap1->capture_time;
    p2 = cap2->capture_time;
    while (*p1 != '\0' && *p2 != '\0') {
        if (*p1 != *p2) {
            break;
        }
        p1++;
        p2++;
    }
    return (*p1 - *p2);
}

int main(int argc, char **argv)
{
    int case_num;
    int cur_case;
    int toll[24];
    capture all_car[1000];
    char flag_field[10];
    char line_buffer[256];
    char *p_line;
    int cur_car;
    int i;
    int tmp;
    char *cur_license = "";
    int cur_charge = 0;
    int trip_num = 0;
    int hour = 0;
    int tmp_charge = 0;
    int case_start_flag = 1;

    /* read the number of case */
    scanf("%d\n", &case_num);

    for (cur_case = 0; cur_case < case_num; cur_case++) {
        /* read the toll */
        case_start_flag = 1;
        for (i = 0; i < 24; i++) {
            scanf("%d", &toll[i]);
        }
        cur_car = 0;
        fgets(line_buffer, 256, stdin);
        /* DEBUG */
        p_line = fgets(line_buffer, 256, stdin);
        /* DEBUG */
        while (NULL != p_line && line_buffer[0] != '\n') {
            if (-1 == setup_capture(&all_car[cur_car], line_buffer)) {
                continue;
            }
            cur_car++;
            p_line = fgets(line_buffer, 256, stdin);
            /* DEBUG */
        }
        qsort(all_car, cur_car, sizeof(capture), compare_license);
#if 0
        for (tmp = 0; tmp < cur_car; tmp++) {
            printf("%s %s %d %d\n", all_car[tmp].license_num,
                   all_car[tmp].capture_time,
                   all_car[tmp].flag,
                   all_car[tmp].length);
        }
#endif
        trip_num = 0;
        for (tmp = 0; tmp < cur_car; tmp++) {
            if (0 != strcmp(all_car[tmp].license_num, cur_license)) {
                /* New car */
                if (trip_num != 0) {
                    if (case_start_flag && cur_case != 0) {
                        printf("\n");
                        case_start_flag = 0;
                    }
                    cur_charge += 200;
                    cur_charge += trip_num * 100;
                    /* printf("\n"); */
                    printf("%s $%.2f\n", all_car[tmp-1].license_num,
                            (double)cur_charge/100);
                }
                cur_charge = 0;
                trip_num = 0;
                cur_license = all_car[tmp].license_num;
                continue;
            } else {
                if ((all_car[tmp-1].flag == 0) && (all_car[tmp].flag == 1)) {
                    /* a start and an exit matched */
                    hour = (all_car[tmp-1].capture_time[6]-'0') * 10 +
                           (all_car[tmp-1].capture_time[7]-'0');
                    tmp_charge = abs(all_car[tmp-1].length - all_car[tmp].length);
                    tmp_charge *= toll[hour];
                    trip_num++;
                    cur_charge += tmp_charge;
                }
            }
        }
        if (trip_num != 0) {
            cur_charge += 200;
            cur_charge += trip_num * 100;
            printf("%s $%.2f\n", all_car[tmp-1].license_num,
                   (double)cur_charge/100);
        }
    }

    return 0;
}

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

上一篇:ACM UVA (10182)

下一篇:ACM UVA (10026)

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