7-6 6翻了 (15 分)(沒啥難的,主要是熟悉c++ string的方法)
阿新 • • 發佈:2021-02-13
7-6 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
我之所以為這題寫篇文章是因為我發現以自己現在的水平,難題很多時候寫不出。。。所以希望自己能在簡單題目上把握好一些零碎的知識點,慢慢提升吧
這題主要是熟悉一下substr()和replace()的用法
AC程式碼:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string s;
getline(cin,s);
int ptr_len = 0, slow = 0;
int fast = slow;
while (slow < s.length())
{
if (s[slow] == '6')
{
fast = slow;
while (s[fast] == '6' && fast < s.length())
{
fast++;
}
ptr_len = fast - slow;
if (ptr_len > 3)
{
if (ptr_len > 9)
{
s.replace(slow, ptr_len, "27");
}
else
{
s.replace(slow, ptr_len, "9");
}
}
}
slow++;
}
cout << s;
return 0;
}