机试刷题记录 2023-7-11

首字母大写

题目描述

Time Limit: 1000 ms
Memory Limit: 256 mb

对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。 在字符串中,单词之间通过空白符分隔,空白符包括:空格(' ')、制表符('\t')、回车符('\r')、换行符('\n')。

输入输出格式
输入描述:
输入一行:待处理的字符串(长度小于100)。
输出描述:
可能有多组测试数据,对于每组数据,
输出一行:转换后的字符串。
输入输出样例
输入样例:
if so, you already have a google account. you can sign in on the right.
输出样例:
If So, You Already Have A Google Account. You Can Sign In On The Right.
题目来源
北京大学机考题
#include<bits/stdc++.h>

std::string capitalizeWords(const std::string& str) {
    std::string result; // 用于存储转换后的字符串
    std::string word;   // 用于存储提取的单词

    for (char c : str) {
        if (std::isspace(c)) {
            if (!word.empty()) {
                // 检查单词的首字母是否为小写字母,并进行转换
                if (std::islower(word[0])) {
                    word[0] = std::toupper(word[0]);
                }
                result += word + c; // 添加转换后的单词及空白符
                word.clear();       // 清空单词缓存
            } else {
                result += c; // 添加空白符
            }
        } else {
            word += c; // 构建单词
        }
    }

    // 处理最后一个单词(没有空白符结尾的情况)
    if (!word.empty()) {
        if (std::islower(word[0])) {
            word[0] = std::toupper(word[0]);
        }
        result += word;
    }

    return result;
}

int main() {
    std::string input;
    std::getline(std::cin, input);

    std::string output = capitalizeWords(input);
    std::cout << output << std::endl;

    return 0;
}

删除字符串2

题目描述

Time Limit: 1000 ms
Memory Limit: 256 mb

给你一个字符串S,要求你将字符串中出现的所有"gzu"(不区分大小写)子串删除,输出删除之后的S。

就是说出现“Gzu”、“GZU”、“GZu”、"gzU"都可以删除。

输入输出格式
输入描述:
输入一行字符串S,长度不超过100。
输出描述:
输出进行删除操作之后的S。
输入输出样例
输入样例:
GzzGzukkgzUuu
输出样例:
Gzzkkuu
题目来源
贵州大学2019机试
#include <iostream>
#include <string>

int main() {
    std::string s;
    std::getline(std::cin, s);

    while (s.find("GZU") != std::string::npos) {
        size_t a = s.find("GZU");
        s.erase(a, 3);
    }
    while (s.find("GZu") != std::string::npos) {
        size_t a = s.find("GZu");
        s.erase(a, 3);
    }
    while (s.find("GzU") != std::string::npos) {
        size_t a = s.find("GzU");
        s.erase(a, 3);
    }
    while (s.find("gZU") != std::string::npos) {
        size_t a = s.find("gZU");
        s.erase(a, 3);
    }
    while (s.find("Gzu") != std::string::npos) {
        size_t a = s.find("Gzu");
        s.erase(a, 3);
    }
    while (s.find("gZu") != std::string::npos) {
        size_t a = s.find("gZu");
        s.erase(a, 3);
    }
    while (s.find("gzU") != std::string::npos) {
        size_t a = s.find("gzU");
        s.erase(a, 3);
    }
    while (s.find("gzu") != std::string::npos) {
        size_t a = s.find("gzu");
        s.erase(a, 3);
    }

    std::cout << s << std::endl;

    return 0;
}

0和1的个数

题目描述

Time Limit: 1000 ms
Memory Limit: 256 mb

给定一个int型整数,输出这个整数的二进制的0和1的个数。

输入输出格式
输入描述:
输入一个整数n
输出描述:
输出这个整数的二进制的0和1的个数。
输入输出样例
输入样例:
15
输出样例:
count0=28 count1=4
题目来源
华南师范大学/贵州大学机试
#include <bits/stdc++.h>

using namespace std;

int main(){
    int n, count1 = 0;
    cin>>n;
    while(n!=0){
        n-=n&(-n);
        count1++;

    }
    cout<<"count0="<<(32-count1)<<" count1="<<count1;
    return 0;
}

利润提成

题目描述

Time Limit: 1000 ms
Memory Limit: 256 mb

企业发放的奖金根据利润提成。利润低于或等于100000元的,奖金可提10%;
利润高于100000元,低于200000元(100000<I≤200000)时,低于100000元的部分按10%提成,高于100000元的部分,可提成 7.5%;
200000<I≤400000时,低于200000元部分仍按上述办法提成,(下同),高于200000元的部分按5%提成;
400000<I≤600000元时,高于400000元的部分按3%提成;600000<I≤1000000时,高于600000元的部分按1.5%提成;
I>1000000时,超过1000000元的部分按1%提成。从键盘输入当月利润I,求应发奖金总数。

输入输出格式
输入描述:
一个整数,当月利润。
输出描述:
一个整数,奖金。
输入输出样例
输入样例:
900
输出样例:
90
题目来源
北京大学机试题
#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long n=0;
    cin>>n;

    if(n<=100000)
        cout<<0.1*n;
    if(100000<n&&n<=200000)
        cout<<0.1*(100000)+0.075*(n-100000);

    if(200000<n&&n<=400000)
        cout<<0.1*(100000)+0.075*(200000-100000)+0.05*(n-200000);

    if(400000<n&&n<=600000)
        cout<<0.1*(100000)+0.075*(200000-100000)+0.05*(400000-200000)+0.03*(n-400000);

    if(600000<n&&n<=1000000)
        cout<<0.1*(100000)+0.075*(200000-100000)+0.05*(400000-200000)+0.03*(600000-400000)+0.015*(n-600000);

    if(n>1000000)
        cout<<0.1*(100000)+0.075*(200000-100000)+0.05*(400000-200000)+0.03*(600000-400000)+0.015*(1000000-600000)+0.01*(n-1000000);


    return 0;
}

单链表

题目描述

Time Limit: 1000 ms
Memory Limit: 256 mb

设节点定义如下

struct Node {
    int Element; // 节点中的元素为整数类型
    struct Node * Next; // 指向下一个节点
};

从键盘输入5个整数,将这些整数插入到一个链表中,并按从小到大次序排列,最后输出这些整数。

输入输出格式
输入描述:
输入5个整数。
输出描述:
按题意输出。
输入输出样例
输入样例:
5 3 4 2 1
输出样例:
1 2 3 4 5
题目来源
贵州大学机试题
#include <iostream>

struct Node {
    int Element;
    struct Node* Next;
};

void insertNode(Node** head, int value) {
    Node* newNode = new Node();
    newNode->Element = value;
    newNode->Next = nullptr;

    if (*head == nullptr || value < (*head)->Element) {
        newNode->Next = *head;
        *head = newNode;
    } else {
        Node* curr = *head;
        while (curr->Next != nullptr && curr->Next->Element < value) {
            curr = curr->Next;
        }
        newNode->Next = curr->Next;
        curr->Next = newNode;
    }
}

void printList(Node* head) {
    Node* curr = head;
    while (curr != nullptr) {
        std::cout << curr->Element << " ";
        curr = curr->Next;
    }
}

void deleteList(Node* head) {
    Node* curr = head;
    while (curr != nullptr) {
        Node* temp = curr;
        curr = curr->Next;
        delete temp;
    }
}

int main() {
    Node* head = nullptr;

    int num;
    for (int i = 0; i < 5; ++i) {
        std::cin >> num;
        insertNode(&head, num);
    }

    printList(head);

    deleteList(head);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_53700832/article/details/131671155