[6] turned PTA

Title repeat

"666" is a network language, probably is a very powerful person, we admire mean. Recently spawned another number "9", which means "turned 6," is too much meaning. If you think this is the highest level of powerful, you're wrong - currently the highest level are the number "27", because it was 3 "9"!

This question will ask you to write a program, those obsolete, only with a series of "6666 ...... 6" expressed admiration sentence, translated into the latest high-level expression.

Input formats:

Given input sentence in a row, i.e., a non-empty string by not more than 1,000 letters, numbers and spaces, ends with a carriage return.

Output formats:

Scan input sentence from left to right: if the sentence has more than three consecutive 6, this string will be replaced by a continuous 6 9; however, if there are more than 9 consecutive 6, this string will be replaced by a continuous 6 27. Other content will not be affected, as it is output.

Sample input:

it is so 666 really 6666 what else can I say 6666666666

Sample output:

it is so 666 really 9 what else can I say 27

answer

Scan from left to right, is met if 6 will start counting when the next character is not 6, the processing starts counting earlier, if it is greater than 3, is converted to 9, 9 is converted to greater than 27, it is noted that each successful transformation 6 string, we must start again from zero array indices.

Is not difficult, but he is still in the fifth test cards for a moment, probably the fifth test point measurement 666,666,666,676,666 this.

100 ++ AND

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str="";
    getline(cin,str);

    int i=0;
    int cnt=0;
    while(i<str.length())
    {

        if(str[i]=='6')
        {
            cnt++;
        }
        else
        {
            cnt=0;

        }
        if(str[i+1]!='6')
        {
            if(cnt>9)
            {
                str.replace(i-cnt+1,cnt,"27");
                i=0;
            }
            else if(cnt>3&&cnt<=9)
            {
                str.replace(i-cnt+1,cnt,"9");
                i=0;//i=0很关键,当转化一串6后,下标可能已经超了,就不再执行。
            }
        }
        i++;
    }
    cout<<str;
    return 0;
}
Published 200 original articles · won praise 99 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_43889841/article/details/104025509