6翻了 &&敲笨钟 (2019天梯赛中的字符串)

不得不说。。。在这场比赛中我彻底被字符串搞自闭了,后来做了做就开始更自闭了(完全布吉岛为啥当时做不出来enmmmm),然后就自闭了好几天(感觉自己拖后腿了哭唧唧T^T),不过还好下来并没有影响到拿奖(没错我就是这么着水了一个高校三等和团体二等orz)
好了接下来是正题了
1, L1 - 2 6翻了
题目链接如下:https://pintia.cn/problem-sets/994805046380707840/problems/1111914599408664577
6翻了 &&敲笨钟 (2019天梯赛中的字符串)
6翻了 &&敲笨钟 (2019天梯赛中的字符串)
解析: 题意非常明显,就是标记计算连续的6的个数,然后进行替换,然后输出。
代码如下 :
1,自己的

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string arr;
    int i = 0, x = 0;  //x是计算器
    getline(cin,arr);
    for(i = 0; i < arr.size(); i++)
    {
        if(arr[i] == '6')
        {
            x = 1;
            for(int j = i + 1; j < arr.size(); j++)
            {
                if(arr[j] == '6')
                    x ++;
                else
                    break;
            }
            if(x > 9)
                cout << 27, i += x - 1;   //下标 
            else if(x > 3)
                cout << 9, i += x - 1;
            else
                cout << arr[i];
            x = 0;   //计数器归零
        }
        else
            cout << arr[i];
    }
    return 0;
}

2,(c语言指针版)&& (群里题解)

#include<stdio.h>
#include <string.h>
int main(void)
{    
    char str[1001], * p1=str, * p2;    
    int i, j;    gets(str);    
    while(*p1) 
    {        
        if(*p1=='6') 
        {            
            p2 = p1;            
            while(*p2=='6') ++p2;            
            if(p2-p1>9) 
            {                
                *p1++ = '2';                
                *p1++ = '7';                
                strcpy(p1, p2);            
            } 
            else if(p2-p1>3)
            {                
                *p1++ = '9';                
                strcpy(p1, p2);            
            } 
            else p1 = p2;        
        } 
        else ++p1;    
    }    
    puts(str);
    return 0; 
 }

3,(c++构造string类)&& (群里题解)

#include<iostream> 
#include<string> 
using namespace std; 
int main(void) 
{    
    string str;    
    int s, count = 0;    
    getline(cin, str);    
    for (int i = 0; i < str.size(); i++) 
    {        
        count = 0;        
        while (str[i] == '6'&&++i&&++count);        
            cout << (count > 9 ? "27" : (count > 3 ? "9" : string(count, '6')));        
        cout << str[i];    
    }    
    return 0; 
}

4,(正则表达式)&&(群里题解)

#include<iostream> 
#include<string> 
#include<regex> 
using namespace std; 
int main(void) 
{    
    string str;    
    getline(cin, str);    
    cout << regex_replace(regex_replace(str, regex("6{10,}"), "27"), regex("6{4,}"), "9");    
    return 0; 
}

2,L1 - 3 敲笨钟
题目链接如下:https://pintia.cn/problem-sets/994805046380707840/problems/1111914599412858880
6翻了 &&敲笨钟 (2019天梯赛中的字符串)
6翻了 &&敲笨钟 (2019天梯赛中的字符串)
解析: 这道题也不难理解,如果上下两句的最后都是ong,就把最后三个字的拼音改成“qiao ben zhong ”
代码如下:
1,我自己的

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin >> n;
	getchar();
	for (int i = 0; i < n; i++)
	{
		string s;
		int k,flag = 0,num = 0;
		getline(cin, s);
		for (int j = 0; j < s.size(); j++)
		{
			if (s[j] == ',' || s[j] == '.')
			{
				if (s[j - 1] != 'g')
					flag = 1;
				if (s[j - 2] != 'n')
					flag = 1;
				if (s[j - 3] != 'o'  )
					flag = 1;
			}
		}
			if (flag == 0)
			{
				for (k = s.size() - 1; k >= 0; k--)
				{
					if (s[k] == ' ')
						num ++;
					if (num == 3)
						break;
				}
			}
			if (flag == 1)
				cout << "Skipped" << endl;
			if (flag == 0)
			{
				for (int kk = 0; kk < k; kk++)
				{
					cout << s[kk];
				}
				cout << " qiao ben zhong." << endl;
			}
	}
	return 0;
}

2,(群里题解)

#include <stdio.h> 
#include <string.h>
int main(void) 
{   
    int n, i, space;    
    char str[150];    
    scanf("%d", &n); 
    getchar();    
    while(n--) 
    {        
        gets(str);        
        if(strstr(str, "ong,") && strstr(str, "ong.")) 
        {            
            for(space=0, i=strlen(str); space<3; --i)                
                if(str[i]==' ') ++space;            
            strcpy(str+i+1, " qiao ben zhong.");            
            puts(str);        
        } 
    else 
        puts("Skipped");    
    }
    return 0; 
 }

3,正则表达式(转自:https://blog.csdn.net/YoungBoron/article/details/88932184)

#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin >> n;
  cin.get();
  while(n--) {
    string s;
    getline(cin, s);
    if (regex_match(s, regex(".*ong,.*ong\\."))) {
      cout << regex_replace(s, regex(" [a-z]* [a-z]* [a-z]*\\."), " qiao ben zhong.\n");
    } else {
      cout << "Skipped\n";
    }
  }
  return 0;
}