题目描述
贝茜把N (1 <= N <= 80)粒蓝色和橙色的珠子连成了一串,问有多少对珠子(相邻的)是不同颜色的。
输入
第一行,一个整数N;
第二行,N个数字(0或1),其中0表示橙色,1表示蓝色。
输出
输出相邻两粒珠是不同颜色的对数。
样例输入
6
1 0 0 1 1 1
样例输出
2
提示
注意边界问题(入只有一颗珠子)
不废话,代码如下:
#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
#define N 1005
int a[N];
int main() {
int n;
int cnt = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int j = 1; j <= n; j++) {
if (a[j] != a[j + 1]) {
cnt++;
}
}
cout << cnt << endl;
return 0;
}
1443

被折叠的 条评论
为什么被折叠?



