当前位置: 首页 > news >正文

编程日志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;

    }

};

相关文章:

  • Day21打卡—常见降维算法
  • 免安装 + 快速响应Photoshop CS6 精简版低配置电脑修图
  • Linux PCI 驱动开发指南
  • AI日报 · 2025年05月11日|传闻 OpenAI 考虑推出 ChatGPT “永久”订阅模式
  • 【React中useRef钩子详解】
  • 【数据结构入门训练DAY-31】组合的输出
  • 【音视频工具】MP4BOX使用
  • k8s 资源对比总结
  • 通过 Azure DevOps 探索 Helm 和 Azure AKS
  • istio in action之应用弹性与容错机制
  • 理解页内碎片与页外碎片:分页存储管理的关键问题
  • 长短期记忆网络(LSTM)深度解析:从理论到实践的全方位指南
  • 23、DeepSeek-V2论文笔记
  • LangGraph框架中针对MCP协议的变更-20250510
  • YOLOv2框架深度解析
  • Go语言实现优雅关机和重启的示例
  • 九、STM32入门学习之WIFI模块(ESP32C3)
  • TIME - MoE 模型代码 4——Time-MoE-main/run_eval.py
  • 图形化编程革命:iVX携手AI 原生开发范式
  • MNIST 数据并行 Data Parallel - DP
  • 白天气温超30℃的北京,晚间下起了冰雹
  • 北京航空航天大学首个海外创新研究院落户巴西
  • 沈阳卫健委通报“健康证”办理乱象:涉事医院已被立案查处
  • 支持企业增强战略敏捷更好发展,上海市领导密集走访外贸外资企业
  • 食用城市|食饭识人
  • 季后赛主场优势消失之谜,这事竟然要赖库里