2023短学期0912题解

使用find()寻找ABBA

【C系列6.21】字符串训练之AB串

Description

无聊的wxa又开始玩字符串了,现在他有一个字符串s,他想知道这个字符串是否含有非重叠AB和BA。(如ABA里AB和BA是重叠的,而ABBA里AB和BA是不重叠的)

Input

输入数据有多组。

每组测试数据有一个字符串s。

Output

如果可以找到AB和BA,输出”yu ye sa wang dai xing”,否则输出”zhen shi ou ba”

Samples

input

ABA

output

zhen shi ou ba

Solution

#include<bits/stdc++.h>
using namespace std;
int main() {
    string s;
    while (cin >> s) {
        int pos1 = s.find("AB");
        int pos2 = s.find("BA", pos1 + 2);
        int pos3 = s.find("BA");
        int pos4 = s.find("AB", pos3 + 2);
        if ((pos1 != -1 && pos2 != -1) || (pos3 != -1 && pos4 != -1)) {
            cout << "yu ye sa wang dai xing" << endl;
        }
        else {
            cout << "zhen shi ou ba" << endl;
        }
    }
    return 0;
}

结构体简单练习

期末排名

Description

自古以来,期末成绩排名一直困扰着人类社会。现在给出N个人的姓名、理论考成绩以及实践考成绩。总分等于理论考和实践考成绩之和,请按照总分排序并输出。题目保证分数均为正整数,两两之间总分互不相同。

Input

第一行一个整数T,表示T组数据。(0<T<100)

每组测试数据第一行为一个整数N代表接下来有N条数据(0<N<100)。
接下来N行,每行给出一个英文名字(长度小于30),以及这个名字对应的理论成绩与实践成绩(均为正整数)。

Output

请输出排序后的姓名列表。

Samples

input

1
3
YYY 10 10
AAA 1 1
SSS 3 3

output

YYY
SSS
AAA

Solution

#include<bits/stdc++.h>
using namespace std;
struct stu {
    string name;
    int a;
    int b;
    int total;//保存总分
};
bool cmp(const struct stu& x, const struct stu& y) {
    return x.total > y.total;//按照总分降序排序
}
int main(){
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        struct stu grade[100];
        for (int i = 0; i < n; i++) {
            cin >> grade[i].name >> grade[i].a >> grade[i].b;
            grade[i].total = grade[i].a + grade[i].b;
        }
        sort(grade, grade + n, cmp);
        for (int i = 0; i < n; i++) {
            cout << grade[i].name << endl;
        }
    }
    return 0;
}

字符和数字的转换

凯撒密码

Description

李先森最近迷上了密码学,决定洗心革面好好学习。今天,他学习了最简单的加密方法——凯撒加密,突发奇想想要来考考你们。他自己制定了这样一个加密规则:大写字母偏移量为3,小写字母偏移量为13。他会给你一串大小写混杂的字符串,你能为他加密吗。

Input

有多组输入,每行为一组,当输入一行为“!"时输入结束。字符串长度不超过50,且均为字母。

Output

输出加密结果。

Samples

input

abc
!

output

nop

Hint

凯撒密码作为一种最为古老的对称加密体制,在古罗马的时候都已经很流行,他的基本思想是:通过把字母移动一定的位数来实现加密和解密。明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推X将变成A,Y变成B,Z变成C。由此可见,位数就是凯撒密码加密和解密的密钥。

博客题解:https://blog.csdn.net/qq_49006646/article/details/107031160

“%26”可以确保大小写字母不会超出各自范围

#include<bits/stdc++.h>
using namespace std;
string caesarCipher(string str) {
    for (char& c : str) {
        if (isupper(c)) {
            c = 'A' + (c - 'A' + 3) % 26; // 大写字母偏移量为3
        }
        else if (islower(c)) {
            c = 'a' + (c - 'a' + 13) % 26; // 小写字母偏移量为13
        }
    }
    return str;
}

int main() {
    string input;
    while (true) {
        cin >> input;
        if (input == "!") {
            break;
        }
        string encrypted = caesarCipher(input);
        cout << encrypted << endl;
    }
    return 0;
}

难以捉摸的detail

统计疫情人数

Description

疫情爆发之后,全球各地都广受其影响。歪比巴卜小镇也被疫情所波及,小镇需要统计当前时刻现有病例的人数。现在有一份现有病例的人数的数据(数据可能是伪造的),需要你设计程序来统计现有病例的人数。

Input

数据的第一行为n,m(n<=1e6, 1<=m<=1e6)。n表示开始统计的初始时刻现有病例的人数,m表示接下来有m次操作。
接下来m行会有3种可能存在的操作类型:
1、Add x 表示现有病例的人数增加了x人。( 0<=x<=1e9 )

2、Sub x 表示现有病例的人数减少了x人。( 0<=x<=1e9 )

3、Query 表示一次查询现有病例的人数的操作,你将需要输出此时的现有病例的人数。

Output

在每次查询操作后,你需要输出此时的现有病例的人数。
注意:现有病例的人数不可能小于零,当你发现这种情况时,说明这是一份伪造的数据,你需要在之后所有的查询之后都输出“fake news!”。

Samples

input

100 2
Add 100
Query

output

200

input

100 4
Add 50
Query
Sub 30
Query

output

150
120

input

100 6
Sub 1
Query
Sub 101
Query
Add 101
Query

output

99
fake news!
fake news!

Solution

哎呀这个细节真的难以捉摸,真折磨人

#include<bits/stdc++.h>
using namespace std;
int main() {
    long long n, m;
    cin >> n >> m;

    long long current_cases = n;
    bool is_fake_news = false;

    for (long long i = 0; i < m; i++) {
        if(n<0){
            is_fake_news = true;
        }
        string operation;
        cin >> operation;

        if (operation == "Add") {
            int x;
            cin >> x;
            if (!is_fake_news) {
                current_cases += x;
            }
        }
        else if (operation == "Sub") {
            int x;
            cin >> x;
            if (!is_fake_news) {
                current_cases -= x;
                if (current_cases < 0) {
                    is_fake_news = true;
                }
            }
        }
        else if (operation == "Query") {
            if (is_fake_news) {
                cout << "fake news!" << endl;
            }
            else {
                cout << current_cases << endl;
            }
        }
    }

    return 0;
}
posted @ 2023-09-12 14:27  yoongii  阅读(128)  评论(0)    收藏  举报