ACM 第一期训练题第3题

题目:

There are n stones on the table in a row, each of them can be red, green or blue. Count the minimum number of stones to take from the table so that any two neighboring stones had different colors. Stones in a row are considered neighboring if there are no other stones between them.

Input

The first line contains integer n (1 ≤ n ≤ 50) — the number of stones on the table.

The next line contains string s, which represents the colors of the stones. We’ll consider the stones in the row numbered from 1 to n from left to right. Then the i-th character s equals “R”, if the i-th stone is red, “G”, if it’s green and “B”, if it’s blue.

Output

Print a single integer — the answer to the problem.
题解:首先定义int类型的n,j,把j初始化为0,定义char类型的s数组,n表示石头数,j表示要取出石头的最小数目,输入n的值,s从R,G,B中输入,利用for循环语句,再用if判断石头是否相邻,若是,则j自增,最后输出j的值。

#include<iostream>
using namespace std;
int main()
{
	int n, j = 0;
	char s[100];
	cin >> n;
	cin >> s;
	for (int i = 1; i < n; i++)
	{
		if (s[i] == s[i - 1])
		{
			j++;
		}
	}
	cout << j;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值