天梯赛--6翻了(字符串处理)

天梯—6翻了

“666”是一种网络用语,大概是表示某人很厉害、我们很佩服的意思。最近又衍生出另一个数字“9”,意思是“6翻了”,实在太厉害的意思。如果你以为这就是厉害的最高境界,那就错啦 —— 目前的最高境界是数字“27”,因为这是 3 个 “9”!

本题就请你编写程序,将那些过时的、只会用一连串“6666……6”表达仰慕的句子,翻译成最新的高级表达。
输入格式:

输入在一行中给出一句话,即一个非空字符串,由不超过 1000 个英文字母、数字和空格组成,以回车结束。
输出格式:

从左到右扫描输入的句子:如果句子中有超过 3 个连续的 6,则将这串连续的 6 替换成 9;但如果有超过 9 个连续的 6,则将这串连续的 6 替换成 27。其他内容不受影响,原样输出。
输入样例:

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

输出样例:

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

主要看的是如何处理读入含’\n’的和空格的

第一种处理方式用char str[1000]来存储判断

	#include<iostream>
	#include<string>
	#include<iomanip>
	#include<map>
	#include<sstream>
	#include<algorithm>
	#define pii pair<long long,long long>
	using namespace std;
	typedef long long ll;
	char str[10000];
	map<ll, pii> ma;

	int main()
	{
		char c = getchar();
		int n = 0;
		while (c!='\n')
		{
			str[++n] = c;
			c = getchar();
		}
		int num = 0;
		for (int i = 1;i <= n;i++)
		{
			if (str[i] == '6')
			{
				num++;
			}
			else
			{
				if (num > 9)cout << 27;
				else if (num > 3)cout << 9;
				else
				{
					while (num)
					{
						cout << 6;
						num--;
					}
				}
				num = 0;
				cout << str[i];
			}
		}
		if (num > 9)cout << 27;
		else if (num > 3)cout << 9;
		else
		{
			while (num)
			{
				cout << 6;
				num--;
			}
		}
	}

第二种方式,用string a getline(cin,a)来读入一行

#include<iostream>
#include<string>
using namespace std;

int main()
{
	
	int num = 0;
	string str;
	getline(cin, str);
	str += '\0';
	for (int i = 0;i <= str.size();i++)
	{
		if (str[i] == '6')
		{
			num++;
		}
		else
		{
			if (num > 9)cout << 27;
			else if (num > 3)cout << 9;
			else
			{
				while (num)
				{
					cout << 6;
					num--;
				}
			}
			num = 0;
			cout << str[i];
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Rikka_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值