16初出茅庐E题

来源:互联网 发布:icmp协议的ip端口号 编辑:程序博客网 时间:2024/04/30 00:07

E 时间排序

Time Limit:1000MS Memory Limit:65535K
题型: 编程题 语言: 无限制
描述

在开发项目的过程当中,经常会遇到这样一个问题,根据时间进行排序。并且当输入的时间格式不规范时,
这个问题变得有些棘手。现在,你需要编写一个程序,实现将给定的时间按时间先后排序后输出。
输入的时间主要有如下几种情况:

(1)09:09

(2)05:2 AM

(3)3:30 PM

第1种为24小时格式,第2、3种为12小时格式,AM和PM都是大写,时间与字母间有一个空格。

输入格式

一行一个时间,不超过10行
输出格式

排序好的时间
输入样例

09:09
05:2 AM
3:30 PM
输出样例

05:2 AM
09:09
3:30 PM
Hint

没什么难点,但要读到所有数据,也不能读到无效数据喔

建议可以使用:
while(gets(buf)!=NULL && strlen(buf)>0)
{
}
Provider

admin
用结构体保存好几个数据,排序输出,题目没说 08:00和08:00 am怎么办。但是数据也没这种情况。有的话再加个变量判断也可以

#include<stdio.h>#include<string.h>#include<stdlib.h>char time[11][20];struct times{    int h;    int m;    int num;    char s[20];}a[11];int cmp(const void*a,const void*b){    struct times*d =(struct times*)a;    struct times*c =(struct times*)b;    if(c->h!=d->h)        return d->h-c->h;    else        return d->m-c->m;}int main(){    int count=0;    int hn,mn;    while(gets(time[count++])>0);    count--;    for(int i=0;i<count;i++)        {        hn=0;        mn=0;        int p=0;        int len=strlen(time[i]);        for(int j=0;j<len;j++)        {            if(time[i][j]>='0'&&time[i][j]<='9'&&!p)            {                hn+=hn*10+time[i][j]-48;            }            if(time[i][j]==':')            {                p=1;            }            if(time[i][j]>='0'&&time[i][j]<='9'&&p)            {                mn+=mn*10+time[i][j]-48;            }            if(time[i][j]=='P')                hn+=12;        }        a[i].h=hn;        a[i].m=mn;        a[i].num=i;        strcpy(a[i].s,time[i]);        }    qsort(a,count,sizeof(a[0]),cmp);    for(int i=0;i<count;i++)        puts(a[i].s);}
0 0