Time Zone 【模拟时区转换】(HDU暑假2018多校第一场)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6308

Time Zone

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


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 (1T106), indicating the number of test cases. For each test case:
The first line contains two integers ab (0a23,0b59) and a string s in the format of "UTC+X'', "UTC-X'', "UTC+X.Y'', or "UTC-X.Y'' (0X,X.Y14,0Y9).
 
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

题意概括:

给出北京时间(北京时区为UTC+8),和需要转换成的目的时间的时区;

求出目的时区的时间;

解题思路:

模拟:

小时和分钟分开处理计算

+时区和 -时区要注意两个加减时间不同的问题,+时区比-时区时间早;

字符转换为整型 atoi(str)

字符转换为双精度浮点型 atof(str)

(当然也可以选择自己手敲一个转换表达式,位数不多)

AC code:

 1 #include <queue>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <cstring>
 5 #include <iostream>
 6 #include <algorithm>
 7 #define INF 0x3f3f3f3f
 8 #define LL long long
 9 using namespace std;
10 const int MOD1 = 24;
11 const int MOD2 = 60;
12 const int MAXN = 20;
13 char str[MAXN];
14 
15 int main()
16 {
17     int a, b;
18     int T_case;
19     scanf("%d", &T_case);
20     while(T_case--){
21         scanf("%d %d %s", &a, &b, &str);
22         //printf("%d %d %s
", a, b, str);
23         bool flag = false;
24         char aa[MAXN], top1=0, bb[MAXN], top2=2;
25             bb[0] = '0';
26             bb[1] = '.';
27         for(int i = 4; i < strlen(str); i++){
28 
29             if(str[i] == '.'){ flag = true; continue;}
30             if(!flag){
31                 aa[top1++] = str[i];                ///zhengshuu
32             }
33             else{
34                 bb[top2++] = str[i];                ///xiaoshu
35             }
36         }
37         aa[top1] = '';bb[top2]='';
38         double num_it = 0;
39         int num_b;
40         int num_a = atoi(aa);
41         if(flag){
42                 num_it = atof(bb);
43                 num_b = (int)(num_it*60);
44         }
45         //printf("num_a:%d num_b:%d
", num_a, num_b);
46         ///*
47         if(str[3] == '+'){
48             if(num_a >= 8) num_a-=8;
49             else if(num_a < 8) num_a = -8+num_a;
50             if(flag){
51                 num_a = num_a+(b+num_b)/60;
52                 b = (b+num_b)%MOD2;
53             }
54             a=((a+num_a)%MOD1 + MOD1)%MOD1;
55         }
56 
57         if(str[3] == '-'){
58 
59             if(flag){
60                 //num_a = num_a-(b+num_b)/60;
61                 b = (b-num_b)%MOD2;
62                 if(b < 0) num_a++;
63                 b = (b+MOD2)%MOD2;
64             }
65             a = ((a-num_a+16)%MOD1+MOD1)%MOD1;
66         }
67 
68         if(a<10) printf("0%d:", a);
69         else printf("%d:", a);
70 
71         if(b<10) printf("0%d
", b);
72         else printf("%d
", b);
73         //*/
74     }
75     return 0;
76 }
原文地址:https://www.cnblogs.com/ymzjj/p/10282469.html