第三题

第三题

问题链接:https://vjudge.net/problem/CodeForces-266A

Stones on the Table

Time limit 1000 ms

Memory limit 262144 kB

Problem Description

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.

Sample Input

9
RRRRRBRBG

Sample Output

4

问题简述:

有n块石头,石头有红、绿、蓝三种颜色,要求相邻石头不能撞色,如果没有其他石头,一排石头被认为是相邻的。

问题分析:

首先输入number确定石头数,定义一个字符串数组,利用循环写入颜色,用if语句判断相邻颜色是否撞色,记录数值输出即可

程序说明:

用cin>>number作为while循环的逻辑表达式来控制循环的终止,用for循环和if语句搜索相邻撞色的i,若撞色令sum自增,最后输出sum即可

AC通过的C语言程序如下:

#include <iostream>
using namespace std;

int main()
{
	int number,sum;
	char color[51];
	
	while (cin>>number)
	{
		sum = 0;
		cin >> color;
		for (int i = 0;i < (number - 1);i++)
		{
			if (color[i] == color[i + 1])
			{
				sum++;
			}
		}
		printf("%d\n", sum);
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值