HDU 6556 (2018CCPC吉林 B题)

### HDU 6556 题目链接 ###

题目大意:

给你四个国家的时区,告诉你 A 国家的时间,让你输出这时候在 B 国家的时间,还需要输出对于 A 国家来说这是 昨天、今天 还是 明天。


分析前提:

1、对于时区转换,通过样例我们可以了解到的是: B 国家的时间差减去 A 国家的时间差 (这个时间差是对于标准时间而言,即题面中的 英国 。这个时间差在题面上每个国家末尾,带正负。)这样就可以根据正负与值来判断 A 与 B 的时间差值了。

2、我们还需要知道的是,对于凌晨0 :00,应该为 12 :00 AM ,而对于中午,应该为 12 :00 PM 。

 

之后我们需要来看看这道题怎么做了,很显然是模拟题,需要分类讨论。

 B 国家先于 A 国家 (即 B 时间差减 A 时间差为负,记为 t ),那么说明我们需要将 A 国家的时间,往前挪 t 个时间长度。

  • 若当前时间往前减去 t 个长度时,不超过当前的时间段的 12 点(即 0 点)。 

  比如现在是 3:00 AM ,往前挪 2 小时, 则不会超过 12:00 AM ,那么直接用 3 减去 2 ,分钟、AM 都不变,即为答案, PM 同理。

  • 若当前时间往前减去 t 个长度时,超过当前的时间段的 12 点(即 0 点)。这里有两种可能。 

  假设当前为 12 :00 AM,要往前挪 13 小时 (我们要知道,这道题最大的时间差就是 8 + 3 = 13 小时),那么就变成了昨天的 11 : 00 AM。同理我们         可知,如果当前为 12:00 PM,最大往前只会挪 13 小时,到昨天的 11 : PM 。所以如果 0 - t + 12 (12点看成 0 )小于 0 了,那么首先无论现在是              AM 还是 PM ,都一定会到昨天,且 AM 与 PM 不会改变。

       如果 0 - t + 12 大于等于 0 ,那么说明不一定会到昨天,且与 当前是 AM 还是 PM 有关。笔画一下就知道,AM 往前如果超过了 12 : 00 AM ,那么必定到昨天。PM 则还是在今天,分类讨论即可。

那么对于 A 先于 B 国家的情况,大家自己去试试啦~

代码如下:

#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<map>
using namespace std;
map<string,int> m;
int t;
int A;
char B[5],C[5];
string a,b;
int main()
{
    //freopen("test.in","r",stdin);
    //freopen("test.out","w",stdout);
    m.clear();
    m.insert(make_pair("Beijing",8));
    m.insert(make_pair("Washington",-5));
    m.insert(make_pair("London",0));
    m.insert(make_pair("Moscow",3));
    scanf("%d",&t);
    int T=0;
    while(t--)
    {
        scanf("%d:%s %s",&A,&B,&C);
        cin>>a>>b;
        int temp=m[b]-m[a];
        if(temp==0){
            printf("Case %d: Today %d:%s %s
",++T,A,B,C );
        }
        else if(temp<0){
            temp=-temp;
            if(A==12) A=0;
            if(A>=temp){
                A-=temp;
                if(A==0) A=12;
                printf("Case %d: Today %d:%s %s
",++T,A,B,C );
            }
            else{
                A=A-temp+12;
                if(A<0){
                    A+=12;
                    printf("Case %d: Yesterday %d:%s %s
",++T,A,B,C );
                }
                else{
                    if(A==0) A=12;
                    if(C[0]=='A') printf("Case %d: Yesterday %d:%s PM
",++T,A,B );
                    else printf("Case %d: Today %d:%s AM
",++T,A,B );
                }
            }
        }
        else{
            if(A==12) A=0;
            if(12-A>=temp){
                A+=temp;
                if(A==12&&C[0]=='P') printf("Case %d: Tomorrow %d:%s AM
",++T,A,B );
                else if(A==12&&C[0]=='A') printf("Case %d: Today %d:%s PM
",++T,A,B );
                else{
                    printf("Case %d: Today %d:%s %s
",++T,A,B,C );
                }
            }
            else{
                A+=temp-12;
                if(A>=12){
                    A-=12;
                    if(A==0) A=12;
                    printf("Case %d: Tomorrow %d:%s %s
",++T,A,B,C );
                }
                else{
                    if(C[0]=='A') printf("Case %d: Today %d:%s PM
",++T,A,B );
                    else printf("Case %d: Tomorrow %d:%s AM
",++T,A,B );
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/Absofuckinglutely/p/11461711.html