#include <iostream>
#include <string>
using namespace std;

int main() {
    int n;
    cin >> n;
    string gears;
    cin >> gears;
    
    int count = 0;
    for(int i = 0; i < n; i++) {
        if(gears[i] == 'L' && gears[(i+1)%n] == 'R') {
            count++;
        }
        if(gears[i] == 'R' && gears[(i+1)%n] == 'L') {
            count++;
        }
    }
    
    cout << count << endl;
    
    return 0;
}

问题描述

Goose Tattarrattat 有一个包含 n 个齿轮(编号 0 到 n-1)的机器,n 个齿轮排成一个环,两个啮合的齿轮必须同时反向转动。

给出每个齿轮要求的转动方向

输入格式

  • 第一行,一个正整数 n,表示齿轮数量。
  • 第二行,一个长度为 n 的字符串,表示每个齿轮的方向,'L' 表示逆时针,'R' 表示顺时针。

输出格式

一个整数,表示最小需要删除的齿轮个数。当一个齿轮删除了之后,其他齿轮还在原来的位置,因此与它相邻的两个齿轮不再由它连接。

输入输出样例

GearsDiv2.in	GearsDiv2.out
4	1
LRRR
3	2
RRR
4	0
LRLR
16	6
LRLLRRLLLRRRLLLL
50	14
RRRRRRRLRRRRRRRLRLRLLRLRLRLRRLRLRLLLRLRLLRLLRRLRRR

数据规模和约定

3 <= n <= 50

C++ 齿轮问题:静态数组解法 - GearsDiv2

原文地址: https://www.cveoy.top/t/topic/qxeP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录