本文共 986 字,大约阅读时间需要 3 分钟。
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. 问题简述:就是看输入的char中相邻的是否相同 程序说明:检查输入的char数组,第一个开始,检查有多少个第n个于第n+1个相同, 最后输出相同的次数 AC代码 #include using namespace std; int main() { int a; char b[60]; cin >> a >> b; b[a] = 0; int i=0,take=0; while (b[i]) { char c; c = b[i]; if (c == b[i + 1]) take++; i++; } cout << take << endl; }转载地址:http://ogevfk.baihongyu.com/