L1-058 6翻了 (15 分)

L1-058 6翻了 (15 分)

在这里插入图片描述
“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

第四次天体赛第二题
创建一个变量(cnt)用来存连续的6的个数,遍历字符串,碰到6的时候则cnt++,没有碰到6那就根据cnt的值输出,同时将cnt置0。
注意遍历完之后一定要在看下cnt是否为0

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <climits>
using namespace std;
typedef long long LL;
#define debug(x, y) cout<<x<<" "<<y<<endl
#define nl cout<<endl
#define fy cout<<"------"<<endl
const int maxn = 1010;
int main() {
    int cnt=0;///连续的6的个数
    int k=0;///字符串的长度
    char s[maxn], ch;
    while(scanf("%c", &ch), ch!='\n') s[k++]=ch;
    s[k]='\0';
    /// PTA不支持gets,当然读取一行的字符串的时候,还可用C++的getline等,记得加头文件 #include<string>哦。
    /// 遍历字符串的时候,遍历到字符串的结束符,保证cnt在遍历完之后必定为0
    /// 当然若是常规的遍历的话,后面就要再看一次cnt的值
    for(int i=0; i<=k; i++) {
        if(s[i]=='6') cnt++;
        else {
            if(cnt>9) printf("27");
            else if(cnt>3) printf("9");
            else while(cnt--) printf("6");
            cnt=0;
            if(i==k) continue;
            printf("%c", s[i]);
        }
    }
    ///常规遍历
    /*
     for(int i=0; i<k; i++) {
        if(s[i]=='6') cnt++;
        else {
            if(cnt>9) printf("27");
            else if(cnt>3) printf("9");
            else while(cnt--) printf("6");
            cnt=0;
            if(i==k) continue;
            printf("%c", s[i]);
        }
    }
    if(cnt>9) printf("27");
    else if(cnt>3) printf("9");
    else while(cnt--) printf("6");*/
    nl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44379458/article/details/88930570