Regionals 2014 >> Asia - Tehran >> 7017 - Working Hours【模拟】

7017 - Working Hours

题目:

Regionals 2014 >> Asia - Tehran >> 7017 - Working Hours【模拟】_第1张图片
Regionals 2014 >> Asia - Tehran >> 7017 - Working Hours【模拟】_第2张图片

题目思路:处理时间加减,模拟。

题目链接:7017 - Working Hours

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
int change(string t)
{
    int sum = 0;
    for (int i = t.size() - 1,j = 0; i >= 0; i--,j++)
    {
        sum = sum + (t[i] - '0') * pow(10,j);
    }
    return sum;
}
int main(){
    string s;
    int hour = 0,second = 0;
    while(cin >> s)
    {
        if (s == "###")
        {
            second += hour * 60;
            hour = 0;
            int temp = second / 60;
            hour += temp;
            second -= 60 * temp;
            printf("%d:%02d\n",hour,second);
            break;
        }
        if (s == "$$$")
        {
            second += hour * 60;
            hour = 0;
            int temp = second / 60;
            hour += temp;
            second -= 60 * temp;
            printf("%d:%02d\n",hour,second);
            hour = 0,second = 0;
        }
        else
        {
            int poi = s.find(":");
            if (poi == string :: npos) poi = s.find(".");
            string sub_h = s.substr(1,poi - 1);
            string sub_s = s.substr(poi + 1,s.size() - poi);
            if (s[0] == '-')
            {
                hour -= change(sub_h);
                second -= change(sub_s);    
            }
            else
            {
                hour += change(sub_h);
                second += change(sub_s);
            }
        }
    }
    return 0;
}

你可能感兴趣的:(Regionals,7017,Tehran)