Time Zone

Time Zone

Problem Description

Chiaki often participates in international competitive programming contests. The time zone becomes a big problem.
Given a time in Beijing time (UTC +8), Chiaki would like to know the time in another time zone s.

Input

There are multiple test cases. The first line of input contains an integer T (1≤T≤106), indicating the number of test cases. For each test case:
The first line contains two integers a, b (0≤a≤23,0≤b≤59) and a string s in the format of “UTC+X”, “UTC-X”, “UTC+X.Y”, or “UTC-X.Y” (0≤X,X.Y≤14,0≤Y≤9).

Output

For each test, output the time in the format of hh:mm (24-hour clock).

Sample Input

3
11 11 UTC+8
11 12 UTC+9
11 23 UTC+0

Sample Output

11:11
12:12
03:23

题目概述

时区

问题描述

Chiaki经常参加国际竞争性编程竞赛。时区成了一个大问题。

给定一个北京时间(UTC +8), Chiaki想知道另一个时区的时间。

输入

有多个测试用例。输入的第一行包含一个整数T(1≤T≤106),显示测试用例的数量。为每个测试用例:

第一行包含两个整数a、b b(0≤≤23日0≤≤59)和一个字符串格式的“UTC + X”,“UTC-X”、“UTC + X..Y”,或“UTC-X.Y”(0≤X,X.Y≤14 0≤Y≤9)。

输出

每次测试输出的时间格式为hh:mm(24小时时钟)。

样例输入

3

11 11 UTC + 8
11 12 UTC + 9
11 23 UTC + 0

样例输出

11:11
12:12
03:23

解题思路

根据题目意思与测试样例我们可以看出
当输入的 是“+”,即 UTC + n 时,需要比较n与8的大小(因为北京是UTC + 8)
如果n<8则让8-n ,此时将输入的时间 -(8-n)
如果n>8则让n-8 ,此时将输入的时间 +(n-8)
(我们不妨直接用输入的时间+n-8
当输入的 是“-”,即 UTC - n 时,不需要比较n与8的大小(因为负数一定比8小)
此时我们将输入的时间 -8-n即可

最后判断变型后的小时和分钟是否符合规定格式
设小时数值为h 分钟数值为 s
当 h>24 时 h=h-24
当 h<0 时 h=24+h
当 s>60 时 s=s-60且h++
当 s<60 时 s=60+s且h- -

代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int n,h,m;
    double s;
    char fu[20];
    scanf("%d",&n); 
    while(n--)
    {
        scanf("%d %d ",&h,&m);
        scanf("%s",fu);       //输入UTC -/+ n
        sscanf(fu+4,"%lf",&s);//此处用到sscanf进行转化(字符转化为浮点数)
        int newh,newm;
        newh=s/1;             //得到n中的小时数
        if(fu[3]=='+')
        {
            h+=(newh-8);     //将小时数转化为北京时间
            newm=(s-newh)*60+0.01; //得到n中的分钟数
 //加一个0.01是因为存在精度问题 例如输入的n=10.2 但是转换为浮点数后为10.19999
            m+=newm;         //将分钟数转化为北京时间
        }
        else
        {
            h=h-newh-8;
            newm=(s-newh)*60+0.01; //同上
            m-=newm;
        }
         if(m>=60)  //判断得到的北京时间是否符合题意
            {
                m-=60;
                h++;
            }
        if(h>=24)
            {
                h-=24;
            }
        if(m<0)
            {
                m+=60;
                h--;
            }
        if(h<0)
            {
                h+=24;
            }
        printf("%02d:%02d\n",h,m); //保留两位不足两位自动向前补0
    }
    return 0;
}

各位亲不懂的地方欢迎留言 留个赞呗

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值