Daily practice 2023.12.13 - 6 overturned [PTA]

Question link:L1-058 6 turned over 

Topic requirements:

"666" is an Internet slang, which probably means that someone is very powerful and we admire them very much. Recently, another number "9" has been derived, which means "6 turned over", which is really awesome. If you think this is the highest state of awesomeness, you are wrong - the current highest state is the number "27", because it is 3 "9"!

This question asks you to write a program to translate those outdated sentences that only use a series of "6666...6" to express admiration into the latest advanced expressions.

Input format:

Enter a sentence in one line, that is, a non-empty string consisting of no more than 1000 English letters, numbers and spaces, ending with a carriage return.

Output format:

Scan the input sentence from left to right: if there are more than 3 consecutive 6s in the sentence, replace the consecutive 6s with 9s; but if there are more than 9 consecutive 6s, replace the consecutive 6s with 27. Other content will not be affected and will be output as is.

Input example:

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

Output sample:

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

Idea:

1. Use getline() to enter a string

2. Iterate through each character in the string

3. The first is to determine whether the character is 6, and the second is to determine how many characters are 6.

4. If there are more than 3 6s, 9 will be output, and if there are more than 9 6s, 27 will be output. Otherwise, each character will be output normally.

Code:

#include<bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    int c = 0;
    getline(cin,s);
    for(int i = 0; i < s.size();i++)
    {
        c = 0;
        while(s[i] == '6' && ++ i && ++ c);
        cout << (c > 9 ? "27" : (c > 3 ? "9" : string(c, '6')));
        cout << s[i];
    }
    return 0;
}

Test Results:

 

おすすめ

転載: blog.csdn.net/m0_63168877/article/details/134974497