HDU - 6308 Time Zone

Time Zone

@ J


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


输入一个时间,然后把这个时间换算成那个时区的时间,我们这个时区规定为+8区。

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
    std::ios::sync_with_stdio(false);
    int T;
    while(cin>>T){
        while (T--) {
            int a,b,n = 0,m = 0 ;
            string s;
            cin>>a>>b>>s;
            //cout<<s.size()<<endl;
            if(s.size()==5)
            {
                n=s[4]-'0';
            }
            else if(s.size()==6)
            {
                n=s[4]-'0';
                n=n*10+(s[5]-'0');
            }
            else if(s.size()==7)
            {
                n=s[4]-'0';
                m=s[6]-'0';
                m=m*6;
            }
            else if(s.size()==8)
            {
                n=s[4]-'0';
                n=n*10+(s[5]-'0');
                m=s[7]-'0';
                m=m*6;
            }
            //cout<<n<<"  "<<m<<endl;
            if(s[3]=='+')
            {
                n=n-8;
                if(n<0&&m!=0)
                {
                    n++;
                    b=b-(60-m);
                    if(b<0) {
                        b+=60;
                        n--;
                    }
                }
                else if(m!=0)
                {
                    b=b+m;
                    if(b>=60)
                    {
                        b%=60;
                        n++;
                    }
                }
                //cout<<n<<endl;
                a=a+n;
                if(a<0)a+=24;
                else a%=24;
            }
            else
            {
                n = n + 8;
                if(m!=0)
                {
                    b-=m;
                    if(b<0)
                    {
                        a--;
                        b+=60;
                    }
                }

                a = a-n;
                while (a<0) {
                    a+=24;
                }
            }
            if(a<10)
            {
                if(b<10) cout<<"0"<<a<<":0"<<b<<endl;
                else cout<<"0"<<a<<":"<<b<<endl;
            }
            else
            {
                if(b<10) cout<<a<<":0"<<b<<endl;
                else cout<<a<<":"<<b<<endl;
            }

        }

    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/J1nAB1n9/article/details/81209554