题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=6308
题目分析:
给出一个北京时间,然后给出一个时区,要求求出该时区的时间。如果给出的时区为X.Y,北京时间为h:m,那么该时区的小时为h+(t-8)。若时区为+,则该时区的分钟数为 m+6*Y,若时区为-,则该时区的分钟数为m-6*Y。
ps:
m = b + 60*mm;
m为int,mm为double,上面这句话导致我wa了一下午,对着数据才找出来错误。。。
因为式子中有double类型,所以在运算的过程中可能会有精度损失,改成下面这样就不会有错误了
m = b + (int)(mm*60);
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int t;
scanf("%d", &t);
while(t--)
{
int a, b, zone;
double mm = 0;
char str[10];
int h, m;
scanf("%d %d", &a, &b);
scanf("%s", str);
if(str[5] >= '0' && str[5] <= '9')
{
zone = (str[4] - '0')*10 + (str[5] - '0');
if(str[6] == '.')
{
mm = (str[7] - '0')*0.1;
}
}
else
{
zone = str[4] - '0';
if(str[5] == '.')
{
mm = (str[6] - '0')*0.1;
}
}
if(str[3] == '-')
{
zone = zone*(-1);
mm = (-1.0)*mm;
}
h = a + zone - 8;
m = b + (int)(mm*60);
if(m >= 60)
{
h++;
m = (int)m%60;
}
else if(m < 0)
{
m+=60;
h--;
}
h = (int)(h+24)%24;
if(h < 10)
printf("0");
printf("%d:", h);
//cout<<h<<":";
if(m < 10)
printf("0");
int x = mm*60;
printf("%d\n", m);
}
// fclose(stdin);
//fclose(stdout);
return 0;
}