题目大意:
有四种灯,最后灯的排列中同种灯之间必有其他3中灯不然就会灭掉,问最后灭掉的灯四种颜色各占多少。
重点:答案必有借,一定会给你没有灭的灯。
题目解法:
很明显这题一定都是循环节,循环节由题目给出的方式给你,那就只要申明的4位的数组,每一位存循环节的颜色(最后一定四种都有),再遍历一边字符串就好了。
代码:
#include "iostream"
#include "cstdio"
#include "math.h"
#include "algorithm"
#include "string"
#include "string.h"
#include "vector"
#include "map"
#include "queue"
using namespace std;
char str[101];
int main() {
while (scanf(" %s", str) != EOF) {
int color[4];
memset(color, 0, sizeof(color));
char a[4];
map<char, int>mp;
int len = strlen(str);
mp['R'] = 0;mp['B'] = 1;mp['Y'] = 2;mp['G'] = 3;
for (int i = 0;i < len;i++) {
if (str[i] != '!') {
a[i % 4] = str[i];
}
}
for (int i = 0;i < len;i++) {
if (str[i] == '!') {
color[mp[a[i % 4]]]++;
}
}
printf("%d %d %d %d\n", color[0], color[1], color[2], color[3]);
}
return 0;
}