CCF 201812-2 小明放学

CCF 201812-2 小明放学

#include <iostream>
#define _for(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int main()
{
    long long r, y, g; //r=red,y=yellow,g=green
    int k, t;
    long long timeclock = 0; //放学回家所用时长
    cin >> r >> y >> g;
    long long sum = r + y + g; //所有的等待时长总和
    long long n;
    cin >> n;
    _for(i, 0, n)
    {
        cin >> k >> t;
        //如果是道路,所有的时间都要加进去
        if (k == 0)
            timeclock += t;
        //如果是红灯,则考虑是哪种情况
        if (k == 1)
        {
            long long temp = timeclock - t; //temp的目的是为了计算timeclock此刻处在红黄绿哪个灯上
            long long temp1 = temp / sum;   //temp1的目的是为了计算有几个完整的sum周期
            //当前已用时间小于等于红灯时长
            if (timeclock <= t)
                timeclock = t;
            //当前已用时间大于红灯时长
            if (timeclock > t)
            {
                //处于绿灯,时间不变
                if (temp % sum <= g)
                    timeclock = timeclock;
                //处于黄灯红灯
                if (temp % sum > g && temp % sum <= sum)
                    timeclock = (temp1 + 1) * sum + t;
            }
        }
        if (k == 2)
        {
            long long temp = timeclock - t;
            long long temp1 = temp / sum;
            //当前已用时间小于等于黄灯时长
            if (timeclock <= t)
                timeclock = t + r;
            //当前已用时间大于黄灯时长
            if (timeclock > t)
            {
                //处于红灯
                if (temp % sum <= r)
                    timeclock = t + r + temp1 * sum;
                //处于绿灯
                if (temp % sum <= (g + r) && (temp % sum > r))
                    timeclock = timeclock;
                //处于黄灯
                if (temp % sum > (g + r) && temp % sum <= sum)
                    timeclock = (temp1 + 1) * sum + t + r;
            }
        }
        if (k == 3)
        {
            long long temp = timeclock - t;
            long long temp1 = temp / sum;
            //当前已用时间小于等于绿灯时长
            if (timeclock <= t)
                timeclock = timeclock;
            //当前已用时间大于绿灯时长
            if (timeclock > t)
            {
                //处于绿灯
                if (temp % sum > (y + r) && temp % sum <= sum)
                    timeclock = timeclock;
                //处于红灯和黄灯
                if (temp % sum <= (y + r))
                    timeclock = t + r + y + temp1 * sum;
            }
        }
    }
    cout << timeclock << endl;
    //system("pause");
    return 0;
}

由于在VSCODE里编写得加system(“pause”);
但是在CCF模拟测试器中不能加入!否则会有错误!
这个题分类挺多的,只要仔细想想都可以做出来的!

哦,对了,记得要用long long 这样才能得满分。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

365JHWZGo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值