时间格式转换

时间格式转换

24 小时制的时间格式为 “HH:mm”,如 “05:20”,而 12 小时制的时间格式为 “h:mm AM/PM”,如 “5:20 AM”。
24 小时制到 12 小时制的对应关系如下:
0 时:12 时 (AM)
1~11 时:1~11 时 (AM)
12 时:12 时 (PM)
13~23 时:1~11 时 (PM)
例如:“00:00” 对应 “12:00 AM”,“01:20” 对应 “1:20 AM”,“12:35” 对应 “12:35 PM”,“13:17” 对应 “1:17 PM”,“23:59” 对应 “11:59 PM”。

现在给你一个 24 小时制的时间,请你编写程序将其转换为 12 小时制的时间。
Input
输入只有一行,包含一个 24 小时制的时间。
Output
输出一行,表示转换为 12 小时制以后的时间。其中分钟部分若不足两位需要加 0 补足两位。
Sample Input
00:05
Sample Output
12:05 AM
Hint
输入部分可以使用 scanf("%d:%d") 读入;输出的数字部分可以使用 printf("%d:%02d") 输出。
Source

方法一:

#include<stdio.h>
int main()
{
    int h,m,x,y;
    scanf("%d:%d",&h,&m);
    if(h==0)
    {
        x=12;
        y=m;
        printf("%02d:%02d AM\n",x,y);
    }
    else if(h>=1&&h<=11)
    {
        x=h;
        y=m;
        printf("%d:%02d AM\n",x,y);
    }
    else if(h==12)
    {
        x=12;
        y=m;
        printf("%02d:%02d PM\n",x,y);
    }
    if(h>=13&&h<=23)
    {
        x=h-12;
        y=m;
        printf("%d:%02d PM\n",x,y);
    }
    return 0;
}

方法二

#include <stdio.h>
int main()
{
    int a,b;
    scanf ("%d:%d",&a,&b);
    if(a==0||a==12)printf("12:%02d ",b);
    else printf("%d:%02d ",a%12,b);
    if(a<12)printf("AM\n");
    else printf("PM");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值