蓝桥杯省赛练习第02周题解

文章包含了多个C++代码示例,涉及水下探测器检测、字符计数、希蒙算法应用、字符串比较、加密和解密等,展示了字符串处理和逻辑判断的编程技巧。
摘要由CSDN通过智能技术生成

P1070  水下探测器

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	int h,s;
	char str[105];
	cin>>h>>s; 
	cin>>str;
	int len=strlen(str);
	for(int i=0;i<len;i++){
		if(str[i]=='u' && s>0)
		{
			s--;
		}
		else if(str[i]=='d' && s<h)
		{
			s++;
		}
	}
	cout<<s;
	return 0;
}

P285  希蒙的心得统计

#include <bits/stdc++.h>
using namespace std;
int main(){
    char a[105];
    gets(a);
    int b=0,c=0;
    for(int i=0; i<strlen(a); ++i){
        if(a[i]>='0' && a[i]<='9') b++;
        if(a[i]>='A' && a[i]<='Z' || a[i]>='a' && a[i]<='z') c++;
    }
    if(c+b/2>=20){
        cout<<"Congratulation";
    }else{
        cout<<20-c-b/2;
    }
    return 0;
}

P651  你的飞碟在这儿

#include<iostream>
#include<cstring>
using namespace std;
int main(){
	char a[10],b[10];
	cin>>a>>b;
	int x=1,y=1;
	for(int i=0;i<strlen(a);i++){
		x*=(a[i]-64);
		x%=47;
	}
	for(int i=0;i<strlen(b);i++){
		y*=(b[i]-64);
		y%=47;
	}
	if(x==y)cout<<"GO";
	else cout<<"STAY";
	return 0;
}

P836  希蒙的AC贴纸数

#include<iostream>
using namespace std;

int main(){
    string s;
    getline(cin, s);
    int sum = 0;
    char c = s[0];
    for(int i = 1; i < s.size(); i ++ ){
        if(s[i] == ' ') continue;
        if(s[i] == c) sum ++ ;
        else if(s[i] != 'A') sum ++ ;
        c = s[i];
    }
    cout << sum;
    return 0;
}

P608  【字符串】石头剪子布

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main() {
	int n;
	cin>>n;
	while(n--){
		string a,b;
		cin>>a>>b;
		if(a=="Rock" and b=="Scissors"){
			cout<<"Player1"<<endl;
		}
		else if(a==b){
			cout<<"Tie"<<endl;
		}
		else if(b=="Rock" and a=="Scissors"){
			cout<<"Player2"<<endl;
		}
		else if(a=="Rock" and b=="Paper"){
			cout<<"Player2"<<endl;
		}
		else if(a=="Paper" and b=="Rock"){
			cout<<"Player1"<<endl;
		}
		else if(a=="Scissors" and b=="Paper"){
			cout<<"Player1"<<endl;
		}
		else if(a=="Paper" and b=="Scissors"){
			cout<<"Player2"<<endl;
		}
	}
	return 0;
}

P288  数单词

#include <iostream>
using namespace std;

int main() {
	string s, word = "lanqiao";
	getline(cin,s);
	int res = 0;
	int len = s.size();
	for(int i=0;i<len;i++){
		if(s[i]>='A'&&s[i]<='Z') s[i] += 32;
	}
	for(int i=0;i<len-7+1;i++){
		if(s.substr(i, 7) == word) res++;
	}
	cout<<res;
	return 0;
}

 P2075  希蒙的字符串

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	string a;
	string b;
	cin >> a >> b;
	int n;
	cin >> n;
	for(int i=1;i<=n;i++)
	{
		int l1,l2,r1,r2;
		cin >> l1 >> r1 >> l2>>r2;
		string s1,s2;
		for(int j=l1;j<=r1;j++)
		{
			s1+=a[j-1];
		}
		for(int j=l2;j<=r2;j++)
		{
			s2+=b[j-1];
		}
		if(s1==s2)
		{
			cout << "playgame!"<<endl;
		}
		else if(s1<s2)
		{
			cout << "workhard"<<endl;
		}
		else
		{
			cout << "touchfish"<<endl;
		}
	}
	
	return 0;
}

P612  【字符串】加密的病历单

#include <iostream>

using namespace std;

int main() {
    string s;
    cin >> s;
    // 右移三位
    for (int i = 0; i < s.length(); i++) {
        // 右移超过字母范围,减去26
        if (s[i] <= 'z' && s[i] >= 'x') {
            s[i] -= 26;
        } else if (s[i] <= 'Z' && s[i] >= 'X') {
            s[i] -= 26;
        }
        // 右移三位 +3
        s[i] += 3;
        // 大小写转换
        // 大写 + 32 --> 小写
        if (s[i] >= 'A' && s[i] <= 'Z') {
            s[i] += 32;
        }
        // 小写 - 32 --> 大写
        else if (s[i] >= 'a' && s[i] <= 'z') {
            s[i] -= 32;
        }
    }
    // 倒序输出
    for (int i = s.length() - 1; i >= 0; i--) {
        cout << s[i];
    }
    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值