201812-2 小明放学

本文详细解析了一种交通灯模拟算法,通过硬模拟的方式处理灯的转换,特别关注了取模操作和大数处理,确保在数据范围超过int类型限制的情况下仍能正确运行。文章提供了完整的C++代码实现,包括如何根据红、黄、绿灯的不同状态和时间调整总时间的计算逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解题思路:

灯的转换按如下方向,硬模拟,注意取模,注意数据范围10^(11)>2^(15)-1(int)(https://www.cnblogs.com/liuweimingcprogram/p/5840665.html

 

解题代码:
 

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long LL;
int r,y,g,n;
LL time(int p,int t,LL sum){
    int mod=r+g+y,temp;
    if(p==0) sum+=t;
    else if(p==1){
        temp=(sum+(r-t))%mod;
        if(temp>0&&temp<=r){
            sum+=(LL)(r-temp);
        }
        else if(temp>r+g&&temp<=r+g+y){
            sum+=(LL)((mod-temp)+r);
        }
    }
    else if(p==3){
        temp=(sum+(g-t))%mod;
        if(temp>g&&temp<=g+y){
            sum+=(LL)((g+y-temp)+r);
        }
        else if(temp>g+y&&temp<=g+y+r){
            sum+=(LL)(mod-temp);
        }
    }
    else if(p==2){
        temp=(sum+(y-t))%mod;
        if((temp>0&&temp<=y)||(temp>y&&temp<=(y+r))){
            sum+=(LL)(r+y-temp);
        }
    }
    return sum;
}
int main(){
    cin>>r>>y>>g;
    cin>>n;
    int p,t;
    LL sum=0;

    while(n--){
        cin>>p>>t;
        sum=time(p,t,sum);
    }
    cout<<sum<<endl;

return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值