多校訓練賽:K - Time Zone (擴大再縮小)

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 ss. 

Input

There are multiple test cases. The first line of input contains an integer TT (1≤T≤1061≤T≤106), indicating the number of test cases. For each test case: 
The first line contains two integers aa, bb (0≤a≤23,0≤b≤590≤a≤23,0≤b≤59) and a string ss in the format of "UTC+X'', "UTC-X'', "UTC+X.Y'', or "UTC-X.Y'' (0≤X,X.Y≤14,0≤Y≤90≤X,X.Y≤14,0≤Y≤9).

Output

For each test, output the time in the format of hh:mmhh: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

 

【解析】

這題煩就煩在X.Y上。。。。

所以就只能擴大十倍去掉小數,然後全都轉化成分鐘來處理。

另外就是昨天剛學的sscanf,太有用了。

#include <bits/stdc++.h>
using namespace std;
int main()
{
	long long t;
	scanf("%lld", &t);
	while (t--)
	{
		int h, m;
		char s[10];
		scanf("%d%d", &h, &m);
		int sum = h * 60 + m;
		scanf("%s", s);
		double d;
		int op = s[3] == '+' ? 1 : -1;
		sscanf(s + 4, "%lf", &d);
		sum += int(10 * d)*op * 6 - 8 * 60;//先將d擴大十倍,然後小時化分鐘
		int ansh = sum % (60 * 24);
		if (ansh < 0)ansh += 24 * 60;
		printf("%02d:%02d\n", ansh / 60, ansh % 60);
	}
	return 0;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章