16初出茅庐E题

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值