Stones on the Table CodeForces - 266A

Text Reverse
Time limit 2000 ms
Memory limit 262144 kB
Source Codeforces Round #163 (Div. 2)
Tags implementation *800 EditorialAnnouncement Tutorial

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
3
RRG
Output
1
Input
5
RRRRR
Output
4
Input
4
BRBG
Output
0

问题链接CodeForce-266 A

问题简述:
桌子上有n块石头,每一块都可以是红的、绿的或蓝的。数一下从桌子上拿走的最小石头数,这样相邻的两颗石头都有不同的颜色。如果一排石头之间没有其他石头,就被认为是相邻的。
问题分析:
循环的使用,对字符串的运用

程序说明:
走一个循环,判断当前字符与前一个字符是否相同,相同则计数加一。

#include<iostream>
using namespace std;

int main()
{
	int n,b=0,j=0;
		char s[51];
		cin >> n;
			cin >> s;
		for (int i = 1; s[i] != '\0'; i++)
		{
			if (s[i] == s[i-1])
				b++;
		}
		cout <<b<< endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44012551/article/details/84887957