题目:
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;
}