PTA_L1-058 6翻了 (15分)

L1-058 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

完整代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    getline(cin,s);
    //不要用cin输入,cin遇到空格会终止,读入的时候用getline(cin,s);
    int len=s.size();
    int ans=0;              //计数器
    for(int i=0;i<len;i++)
    {
        if(s[i]=='6')
        {
            ans++;
        }
        else
        {
            if(ans<=3)
            {
                for(int j=0;j<ans;j++)
                {
                    cout<<"6";
                }
                cout<<s[i];
                ans=0;
            }
            else if(ans>3&&ans<=9)
            {
                cout<<"9"<<s[i];
                ans=0;
            }
            else
            {
                cout<<"27"<<s[i];
                ans=0;
            }
        }
    }
    if(ans)
    {
        if(ans<=3)
        {
            for(int i=0; i<ans; i++)
            {
                cout<<"6"<<endl;
            }
        }
        else if(ans>3&&ans<=9)
        {
            cout<<"9"<<endl;
        }
        else
        {
            cout<<"27"<<endl;
        }
    }
    return 0;
}

原题链接:
https://pintia.cn/problem-sets/994805046380707840/problems/1111914599408664577

发布了87 篇原创文章 · 获赞 98 · 访问量 4952

猜你喜欢

转载自blog.csdn.net/qq_45856289/article/details/104875087