欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > 编程日志5.3

编程日志5.3

2025/5/12 18:08:07 来源:https://blog.csdn.net/2301_80146401/article/details/147881456  浏览:    关键词:编程日志5.3

串的习题

1.Problem - 2030

#include<iostream>

using namespace std;

int main() {
    char s[500];
    int n;
    cin >> n;
    getchar();//去掉空格部分
    while (n--) {
        gets(s);//老式写法 vs显示错误题目解答正确
        int cnt = 0;
        int len = strlen(s);
        for (int i = 0; i < len; ++i) {
            if (s[i] < 0) {
                ++cnt;
            }
        }
        cout << cnt / 2 << endl;
    }
    return 0;
}

2.Problem - 2026

#include<iostream>
#include<cstring>

using namespace std;

int main() {
    char s[110];
    while (gets(s)) {
        int len = strlen(s);
        for (int i = 0; i < len; ++i) {
            if (i == 0 || s[i - 1] == ' ') {
                if (s[i] != ' ') {
                    if (s[i] >= 'a' && s[i] <= 'z') {
                        s[i] -= 'a';
                        s[i] += 'A';
                    }
                }
            }
        }
        printf("%s\n", s);
    }
    return 0;
}

3.Problem - 2025

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

int main() {
    string s;
    while(cin >> s) {
        string ret = "";
        char maxc = 'a';//定义最小字符元素
        for (int i = 0; i < s.size(); ++i) {//遍历
            if (s[i] > maxc) {
                maxc = s[i];//更新得到最大s[i]
            }
        }
        for (int i = 0; i < s.size(); ++i) {//遍历
            ret = ret + s[i];//字符串加到ret上
            if (s[i] == maxc) {
                ret = ret + "(max)";//加上最大标记
            }
        }
        cout << ret;
    }
    return 0;
}

4.1812. 判断国际象棋棋盘中一个格子的颜色 - 力扣(LeetCode)

class Solution {

public:

    bool squareIsWhite(string coordinates) {

       int x = coordinates[0]-'a';

       int y = coordinates[1]-'1';

       return (x + y) % 2 == 1;

    }

};

5.LCR 122. 路径加密 - 力扣(LeetCode)

class Solution {

public:

    string pathEncryption(string path) {

        int len = path.size();

        for(int  i = 0;i<len;++i){

            if(path[i] == '.'){

                path[i] = ' ';

            }

        }    

        return path;

    }

};

6.1876. 长度为三且各字符不同的子字符串 - 力扣(LeetCode)

class Solution {

public:

    int countGoodSubstrings(string s) {

        int len = s.size();

        int cnt = 0;

        for(int i =0;i<len-2;++i){//长度为3

            int a = s[i];

            int b = s[i+1];

            int c = s[i+2];

            if( a!= b&&b!=c&&a!=c){

                ++cnt;

            }

        }

        return cnt;

    }

};

7.LCP 17. 速算机器人 - 力扣(LeetCode)

class Solution {

public:

    int calculate(string s) {

        int len = s.size();

        int x = 1 , y = 0;

        for(int i = 0;i<len;++i){

            if(s[i]=='A'){

                x = 2 * x + y;

            }else if(s[i]=='B'){

                y = 2 * y + x;

            }

        }

        return x+y;

    }

};

8.2011. 执行操作后的变量值 - 力扣(LeetCode)

class Solution {

public:

    int finalValueAfterOperations(vector<string>& operations) {

        int X = 0;

        //共同点 中间为+ 或者 中间为-

        for(int  i = 0;i<operations.size();++i){

            string s  = operations[i];

            if(s[1]=='+'){

                X++;

            }else{

                X--;

            }

        }

        return X;

    }

};

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词