Stones on the table
Time limit 2000ms;
Memory 262144KB
Problem Description:(Stones on the Table)
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.
Examples:
Input:
3
RRG
Output:
1
Input:
5
RRRRR
Output:
4
Input:
4
BRBG
Output:
0
click here to实战演练
问题简述:
第一行输入颜色的个数,第二行符合第一行的颜色字母,并且是输入大写字母,程序运行,把颜色左右相同的颜色字母去掉一个,输出需要去掉颜色字母的个数
问题简单分析:
如果后面一个颜色字母等于前面的一个颜色字母则要被拿掉一个字母。因为输入的颜色字母个数不确定,可以考虑定义一个足够大的数组去存放输入的颜色字母
Virtual Judge通过的C++程序以及具体的分析如下:
#include <stdio.h>
#include <iostream>
#define N 51
using namespace std;
int main()
{
char a[N];//定义一个足够大的数组空间
int n, i;
int output = 0;//output作为输出的个数要先赋初值
scanf_s("%d", &n);//输入n,n为第二行要输入的颜色字母的个数
scanf_s("%c", &a);//输入a即往a[]里输入数,作为第二行输入的颜色字母
for (i = 0;i < n - 1;i++)
{
if (a[i] = a[i + 1])//如果前面的颜色字母等于后面一个颜色字母
output++;//就得拿掉一个,即output要加一
}
printf("%d\n", output);
}