团体程序设计天梯赛-练习集 L1-058 6翻了 (15 分)

版权声明:小牛牛的奋斗 https://blog.csdn.net/qq_43590403/article/details/89297761

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

题解:本题其实并不难,不要将其想的难了。直接在for循环中判断四种情况就好,判断时一定要加上下一个元素不为6的限定,否则会多输出东西。还要将每次输出后将就、count计时器清零。以便于下一次计算。
复杂做法:直接在6大于三的时候,用计数器count来操作需要打印的代码。但是经常出现问题。不如题解来的直接明了。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

char ch[1005],c;
int main()
{
    int i=0,j=0;
    int count=0;
    while((c=getchar())!='\n')
    {
        ch[i++]=c;
    }
    for(i=0;ch[i]!='\0';i++)
    {
        if(ch[i]=='6')
            count++;
        if(count>9&&ch[i+1]!='6')
        {
            cout<<27;
            count=0;
        }
        if(count>3&&count<=9&&ch[i+1]!='6')
        {
            cout<<9;
            count=0;
        }
        if(count<=3&&ch[i+1]!='6')
        {
            for(j=0;j<count;j++)
                cout<<6;
            count=0;
        }

        if(ch[i]!='6')
            cout<<ch[i];
    }
    return 0;
}


以下为我写的复杂解法,其中会有一些错误。目前没有找到改法。。。。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

char ch[1005],c;
int main()
{
    int i=0,j=0,k,n,num;
    int count=0;
    while((c=getchar())!='\n')
    {
        ch[i++]=c;
    }
    for(i=0;ch[i]!='\0';i++)
    {
        count=0;num=0;
        while(ch[j]!='\0')
        {
            if(ch[j]=='6')
                count++;
            j++;
            if(count<=3&&ch[j]!='6')
            {
                num=1;
                break;
            }
            if(count>3&&ch[j]!='6')
            {
                break;
            }
        }

        if(num)
        {
            for(k=i;k<=j;k++)
            {
                cout<<ch[k];
            }
        }
        else
        {
            //cout<<"hello";
            for(k=i;k<j-count;k++)
            {
                if(k==i)
                    cout<<" ";
                cout<<ch[k];
            }
        }

        if(count>9)
            cout<<27<<" ";
        if(count<=9&&count>3)
            cout<<9<<" ";

        i=j;
    }
    //cout<<"\b"<<endl;
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_43590403/article/details/89297761