题解:P16897 [GKS 2022 #H] Running in Circles

· · 题解

题目传送门

1.

这道题 也是相当的水 思路很简单 ,如果方向为 C ( 顺时针 ) 就加 D ,如果方向为 A ( 逆时针 ) 就减 D 。

代码如下:

if (c=='C')
    now += d;
else
    now -= d;

这样的话如果先顺时针跑 3 ,再逆时针跑 2 ,最终的结果便是 1 ,顺时针和逆时针相互抵消 。

2.

然后就要考虑如何增加圈数了 。实现起来也很简单 ,先判断是否超过了 L(不超过就不加),如果超过 ,那就判断跑了几圈 。

代码如下 :

if (abs(now)>=l){//别忘了加 abs(毕竟可能是负数)
    ans += abs(now)/l;
    now %= l;
}

AC CODE

#include<bits/stdc++.h>

using namespace std;

const int N=1e4+5;
long long t,l,n,d,ans,now;//范围1e9,得开long long
char c;
int main(){
    cin>>t;
    for (int i=1;i<=t;i++){
        cin>>l>>n;
        for (int j=1;j<=n;j++){
            cin>>d>>c;
            if (c=='C')
                now += d;
            else
                now -= d;
            if (abs(now)>=l){
                ans += abs(now)/l;
                now %= l;
            }
        }
        cout<<"Case #"<<i<<": " <<ans<<'\n';
        ans = 0;//记得归零
        now = 0;
    }
    return 0;
}