Time Zone(多校联赛)多种方法详解

题目链接

题目描述:

Time Zone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2720    Accepted Submission(s): 859


 

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

 

 

Source

2018 Multi-University Training Contest 1

 

思路分析:

题目目的很明确,给出一个本地时间(以东八区为本地时间)和一个时区,要求计算该时区的当地时间,根据时区的计算方法很容易得到结果,需要注意的是给出的时区可能是X.Y的形式,关于时区的输入方式,可以用字符串类型,也可以用double类型,下面给出实现过程:

参考代码:

方法一:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    int a,b,c,d,e,s,w;
    char m[20];
    while(~scanf("%d",&a))
    {
        while(a--)
        {
            s=0;w=0;
            scanf("%d %d %s",&b,&c,&m);
            d=strlen(m);
            
            //根据字符串的长度来判断输入的形式是+还是-,是X.Y形还是X形
            if(d==5)
            {
                s=m[4]-'0';
            }
            else if(d==6)
            {
                s=(m[4]-'0')*10+m[5]-'0';
            }
            else if(d==7)
            {
                s=m[4]-'0';
                w=m[6]-'0';
            }
            else if(d==8)
            {
                s=(m[4]-'0')*10+m[5]-'0';
                w=m[7]-'0';
            }
            if(m[3]=='-')
            {
                b=b-8-s;
                e=w*6;
                if(e<=c)
                    c=c-e;
                else
                {
                    b--;
                    c=60+c-e;
                }
            }
            else
            {
                b=b-(8-s);
                e=w*6;
                if(e+c<60)
                    c=c+e;
                else
                {
                    b++;
                    c=c+e-60;
                }
            }
            
            //进位处理
            if(b>=24)    
                b=b-24;
            else if(b<0)
                b=b+24;
            printf("%02d:%02d\n",b,c);
        }
    }
    return 0;
}

方法二:方法二涉及到eps误差处理问题,如有疑问请详看另一篇文章:浮点数数据误差eps处理(详细解析)

#include<iostream>
#include<cstring>
#include<cstdio>
#define eps 1e-10
using namespace std;
int main()
{
    int t;
    double c;
    scanf("%d",&t);
    while(t--)
    {
        int h, m, sum,flag;
        char str[20];
        scanf("%d %d %s", &h, &m, str);
        h = h * 60 + m;
        if(str[3]=='+')
            flag=1;
        else
            flag=-1;
        sscanf(str + 4, "%lf", &c);//利用sscanf特性进行读取
        sum = int(c * 60+eps);//浮点数误差处理
        h += sum * flag - 8 * 60;
        if (h < 0) 
            h += 24 * 60;
        printf("%02d:%02d\n", h / 60%24, h % 60);//进位处理并输出
    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值