Stones on the Table c++

题目

出处
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.

分析

桌子上有一排3种色的石头,问至少去掉多少个石头,才能使这一排的石头相邻颜色不同。
题意是输入石头的数量,再输入石头颜色顺序(字符串实现)。
思路:两两比较是否相同,如果是,则记数加1;否则不处理。这道题可以不用处理字符串,因为去掉石头后,后来的颜色顺序是不会改变的,所以保留原样就好了。
注意:要用getchar()去吸收 cin结束后的回车。

代码

#include<iostream>
using namespace std;
int main()
{
	int num, i,j;
	char stones[50];
	cin >> num;
	getchar();
	gets_s(stones, num+1);
	for (i = 1,j=0; i < num; i++)
	{
		if (stones[i - 1] == stones[i])j++;
	}
	cout << j<<endl;
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值