1. 程式人生 > >Stones on the Table

Stones on the Table

Text Reverse
在這裡插入圖片描述
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.

問題連結:https://vjudge.net/problem/CodeForces-266A

問題簡述:該題就是判斷相同的字母有多少個,然後再減一。

程式簡述:先建立一個字元陣列,再輸入字串,然後用迴圈比較每一個字元陣列是否與下一個相同,最後記錄相同字母的個數。

#include <iostream>
using namespace std;
int main()
{
 int a,i;
 int n = 0;
 cin >> a;
 char*s = new char[a];
 cin >> s;
 for (i = 0; s[i] != '\0'; i++)
 {
  if (s[i] == s[i + 1])
   n++;
 }
 cout << n;
}