HDU6308 - Time Zone(模拟)

博客给出ACM题目链接,介绍输入处理思路,可使用sscanf从字符串读取格式化输入,也可用scanf直接格式化输入,之后将时间全部转化为分钟计算,还提及double精度问题。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6308

思路:输入的时候可以用sscanf(sscanf(s+4, "%lf", &x))从字符串读取格式化输入,也可以直接用scanf(scanf("%d %d UTC%c%lf",&h,&m,&ch,&x))直接格式化输入,然后全部转化为分钟计算。关于double精度问题,点我,点我

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
    int t, h, m; char s[10];
    cin >> t;
    while(t--)
    {
        scanf("%d%d%s", &h, &m, s);
        h = h * 60 + m;
        int op = s[3]=='+'?1:-1;
        double x;
        sscanf(s+4, "%lf", &x);
        x = (int)(x*10 + 0.005); //防止精度损失
        int cha = x*6*op - 8*60;
        h += cha;
        h = (h + 24*60) % (24*60);
        printf("%02d:%02d\n", h/60, h%60);
    }
    return 0;
}

 

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
    int t, h, m;
    char s[10], ch;
    double x; cin >> t;
    while(t--)
    {
        scanf("%d %d UTC%c%lf", &h,&m,&ch,&x);
        h = h * 60 + m;
        int op = ch == '+' ? 1 : -1;
        x = (int)(x*10 + 0.005); //防止精度损失
        int cha = x*6*op - 8*60;
        h += cha;
        h = (h + 24*60) % (24*60);
        printf("%02d:%02d\n", h/60, h%60);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值