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
思路
给了一个北京时间(UTC+8),然后给了一个目标时区,让求出对应时区的时间。
题目中时区可能出现小数,所以为了计算方便起见,在做题的时候我们把所有的时间都化成分钟,这样就不存在小数了。然后直接按照需要加或减的时间模拟就好。
对于具体的时区怎么算,请移步百度百科:时区
代码
#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int N = 5e5 + 10;
const int inf = 0x3f3f3f3f;
void calc(int h, int m, int fen, int flag) //flag=1为+
{
if (flag)
{
while (fen)
{
m++;
if (m == 60)
{
m = 0;
h++;
}
if (h == 24)
{
h = 0;
}
fen--;
}
printf("%02d:%02d\n", h, m);
}
else
{
while (fen)
{
m--;
if (m == -1)
{
m = 59;
h--;
}
if (h == -1)
{
h = 23;
}
fen--;
}
printf("%02d:%02d\n", h, m);
}
}
int main()
{
//freopen("in.txt", "r", stdin);
int t;
char str[100];
scanf("%d", &t);
while (t--)
{
int h, m;
scanf("%d %d %s", &h, &m, str);
int len = strlen(str);
float x;
char ch;
sscanf(str, "UTC%c%f", &ch, &x);
if (ch == '+')
{
if (x == 8.0)
{
printf("%02d:%02d\n", h, m);
}
else if (x > 8.0) //向上加
{
int fen = (int)((x - 8.0) * 60 + 0.01);
calc(h, m, fen, 1);
}
else if (x < 8.0) //向下减
{
int fen = (int)((8.0 - x) * 60 + 0.01);
calc(h, m, fen, 0);
}
}
else if (ch == '-')
{
x = -x;
int fen = (int)(((8.0) - x) * 60 + 0.01);
calc(h, m, fen, 0);
}
}
return 0;
}