第三题
问题链接: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);
}
}