HDU 1861 游艇

来源:互联网 发布:用数据讲故事 编辑:程序博客网 时间:2024/04/25 18:04

为了练手,用了链表,写的麻烦了。用数组方便些


#include "stdio.h"#include "string.h"#include "stdlib.h"typedef struct msg{int num;char type;char time[6];struct msg* next;}Msg, *pMsg;Msg theMsg;pMsg end;long sumTm, sumCt;int getTime(char* a, char* b){int secA, secB, minA, minB;secA = a[4]-48 + (a[3]-48)*10;secB = b[4]-48 + (b[3]-48)*10;minA = a[1]-48 + (a[0]-48)*10;minB = b[1]-48 + (b[0]-48)*10;if(secA<secB){secA += 60;minA --;}return (minA-minB)*60 + secA - secB;}int Cal(pMsg node, long* tm, int time){pMsg pre = &theMsg, start = theMsg.next;for(; start; pre = start, start = start->next)if(start->num == node->num){if(time)*tm = getTime(node->time, start->time);pre->next = start->next;if(end == start)end = pre;free(start);return 1;}return 0;}int Input(){int n;long tm;char t; /*占位*/theMsg.next = NULL;end = &theMsg;while(scanf("%d", &n), n>=0){pMsg node = (pMsg)malloc(sizeof(Msg));node->num = n;scanf("%c", &t); /*占位*/scanf("%c", &(node->type));scanf("%c", &t); /*占位*/scanf("%s", node->time);if(!n)return 1;if(node->type == 'E' && Cal(node, &tm, 1)){sumCt++;sumTm += tm;}else if(node->type == 'S'){Cal(node, &tm, 0);node->next = end->next;end->next = node;end = node;continue;}free(node);}return 0;}void delMsg(){pMsg temp;end = theMsg.next;while(end){temp = end;end = end->next;free(temp);}}void main(){freopen("in.txt", "r", stdin);while(Input()){printf("%d ", sumCt);if(sumCt)printf("%.0f", sumTm*1.0/sumCt);elseprintf("0");printf("\n");sumCt = sumTm = 0;delMsg();}}