HDU-6308

题目链接:

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值