PTA ladder game practice set L1-058 6 turned over

6 turned over

insert image description here

"666" is an Internet term, which probably means that someone is very powerful and we admire him very much. Recently, another number "9" was derived, which means "6 turned over", which is really too powerful. If you think this is the highest level of power, you are wrong - the current highest level is the number "27", because it is three "9"!

For this question, you are asked to write a program to translate those outdated sentences that can only express admiration with a series of "6666...6" into the latest advanced expressions.

Input format:
Input a sentence in one line, that is, a non-empty string consisting of no more than 1000 English letters, numbers and spaces, and terminated by 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 string of 6s with 9; but if there are more than 9 consecutive 6s, replace the string of 6s with 27. Other content is not affected and is output as it is.

Input sample:

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

The problem of string replacement, find out the string that meets the conditions and then replace it. For details, please refer to the code
AC code (C++)

#include <iostream>    //数据流输入/输出
#include <string>

using namespace std;

//输出有几个6,然后返回相应字符串
string is666(string str)
{
    
    
	int count = 0;
	for (string::iterator it = str.begin(); it != str.end(); it++)
		if (*it == '6')
			count++;
	if (count > 9)			//大于9个六返回27
		return "27";
	else if (count > 3)		//小于9大于3个六返回9
		return "9";
	else					//其他情况返回原本字符串
		return str;
}

int main()
{
    
    
	string str;			//读取输入的字符串
	string tmp = "";	//保存字串
	string putStr = ""; //保存输出时候的串
	getline(cin, str);	//读取一整行

	for (string::iterator it = str.begin(); it != str.end(); it++)
	{
    
    
		if (*it == '6')		//发现6把他保存进临时子串
			tmp += *it;
		else
		{
    
    
			putStr += is666(tmp);		//不是6的话检查子串情况
			putStr += *it;				//不是六的话直接接入输出串
			tmp = "";					//每次检查完字串都需要把子串清空
		}
	}
	cout << putStr + is666(tmp) << endl;		//最后检查是否有遗漏未处理的子串

	return 0;
}


Guess you like

Origin blog.csdn.net/m0_52072919/article/details/124569704