华为机试之简易消除

题目是输入一个数组,例如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;
}

转载于:https://my.oschina.net/java7cpp/blog/737915

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值