题目是输入一个数组,例如1 1 1 2 3 4 4 4 3 5 5 5 3 6 7,要求,当存在连续相同3个或3个以上时,则把其消除,如第一次得到233367,
继续消除,直到不存在连续相同3个或3个以上,最后会得到267,输出要求,每个数字之间有空格,即2 6 7;输入数组最大为15个;
这道题挺简单的,但是做的时候由于紧张,思路不清晰,最终还差一丢丢没有搞定;
代码如下:
#include<iostream>
#include<string>
using namespace std;
int main(void){
string str;
while (getline(cin, str)){
char ch[16] = { 0 };
int m = 0;
for (int i = 0; i < str.size(); i++){
if (str[i] != ' '){
ch[m] = str[i];
m++;
}
}
int flag = 0;
while (strlen(ch)>2 && flag == 0){
unsigned int j = 0;
char ch1[16] = { 0 };
bool flag_ch[16] = { 0 };
while (j < strlen(ch)){
unsigned int temp1 = 0;
bool flag_cnt = 0;
unsigned int temp = j;
while (ch[temp] == ch[temp+1] && ch[temp] == ch[temp + 2]){
temp++;
temp1++;
flag_cnt = 1;
}
if (flag_cnt == 1){
for (int p = j; p <= j + temp1 + 1; p++){
flag_ch[p] = 1;
flag_cnt = 0;
}
j = j + temp1 + 2;
}
else{
j++;
}
}
for (int i = 0; i < 16; i++){
if (flag_ch[i] == 1){
ch[i] = 0;
}
}
int r = 0;
for (int p = 0; p < 16; p++){
if (ch[p] != 0){
ch1[r] = ch[p];
r++;
}
}
int d = 0;
for (int q = 0; q < 16; q++){
ch[q] = 0;
if (ch1[q] != 0){
ch[d] = ch1[q];
d++;
}
}
flag = 1;
for (int i = 0; i < strlen(ch); i++){
for (int j = i+1; j < strlen(ch); j++){
for (int k = i+2; k < strlen(ch); k++){
if (ch[i] == ch[j] && ch[i] == ch[k]){
flag = 0;
break;
}
}
}
}
}
int cnt[16] = { 0 };
int u = 0;
for (int i = 0; i < strlen(ch); i++){
if (ch[i] != '\0'){
cnt[u] = i;
u++;
}
}
if (u != 0){
for (int i = 0; i < u - 1; i++){
cout << ch[cnt[i]];
cout << " ";
}
cout << ch[cnt[u - 1]];
}
else{
cout << "none";
}
cout << endl;
}
return 0;
}