首页 > 编程语言 >SYNU PTA C++ 第三章实验题

SYNU PTA C++ 第三章实验题

时间:2023-03-18 20:35:50浏览次数:34  
标签:SYNU cout int cin PTA repeat C++ str include

题目详细内容见PTA,仅提供答案参考。

7-7 冒泡

 1 #include <iostream>
 2 using namespace std;
 3  
 4 void merge(int *arr, int left, int mid, int right)
 5 { 
 6     int n1 = mid - left + 1;
 7     int n2 = right - mid;
 8     int L[n1], R[n2];
 9     for (int i = 0; i < n1; i++) 
10     {
11         *(L + i) = *(arr + left + i);
12     }
13     for (int j = 0; j < n2; j++) 
14     {
15         *(R + j) = *(arr + mid + 1 + j);
16     }
17     int i = 0, j = 0, k = left;
18     while (i < n1 && j < n2) 
19     {
20         if (*(L + i) <= *(R + j)) 
21         {
22             *(arr + k) = *(L + i);
23             i++;
24         } 
25         else 
26         {
27             *(arr + k) = *(R + j);
28             j++;
29         }
30         k++;
31     }
32     while (i < n1) 
33     {
34         *(arr + k) = *(L + i);
35         i++;
36         k++;
37     }
38     while (j < n2) 
39     {
40         *(arr + k) = *(R + j);
41         j++;
42         k++;
43     }
44 }
45 
46 void mergeSort(int *arr, int left, int right)
47 { 
48     if (left < right)
49     {
50         int mid = (left + right) / 2;
51 
52         mergeSort(arr, left, mid); 
53         mergeSort(arr, mid + 1, right); 
54 
55         merge(arr, left, mid, right); 
56     }
57 }
58 
59 int main() 
60 {
61     int n;
62     cin >> n;
63     int a[n];
64     for (int i = 0; i < n; i++)
65     {
66       cin >> *(a + i);
67     }
68     mergeSort(a, 0, n - 1); 
69     for (int i = 0; i < n; i++)
70     {
71         cout << *(a + i) << " ";
72     }
73     cout << endl;
74     return 0;
75 }

 

 

 

7-8 在数组中查找指定元素

#include <iostream>
using namespace std;

class NotFoundException : public exception
{
public:
    virtual const char* what() const throw() 
    {
        return "Not found";
    }
};

class InvalidInputException : public exception 
{
public:
    virtual const char* what() const throw() 
    {
        return "Invalid input";
    }
};

class Search
{
public:
    Search() {}
    ~Search() {}

    int search(int* list, int n, int x) 
    {
        if (list == NULL || n <= 0)
            throw InvalidInputException(); 
        for (int i = 0; i < n; i++)
        {
            if (list[i] == x)
                return i;
        }
        throw NotFoundException(); 
    }

    void inputArray(int* a, int n) 
    {
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
    }

    void run() 
    {
        int repeat;
        cin >> repeat;
        while (repeat--) 
        {
            int n, x;
            cin >> n;
            try {
                if (n <= 1 || n > 10)
                    throw InvalidInputException();
                int a[n];
                inputArray(a, n);
                cin >> x;
                int res = search(a, n, x);
                printf("index = %d\n", res);
            }
            catch (NotFoundException& e)
            {
                cout << e.what() << endl;
            }
            catch (InvalidInputException& e)
            { 
                cout << e.what() << endl;
            }
            catch (exception& e)
            { 
                cout << "Unknown exception." << endl;
            }
        }
    }
};

int main() 
{
    Search s;
    s.run();
    return 0;
}

 

 

 

 

7-9 使用函数删除字符串中的字符

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

void delchar(char* str, char c)
{
    int i = 0, j = 0;
    int len = strlen(str);
    while(i < len)
    {
        if(str[i] != c)
        {
            str[j++] = str[i];
        } 
        i++;
    }
    str[j] = '\0';
}

int main()
{
    int repeat;
    cin >> repeat;  
    while(repeat<=0 || repeat>=10)
    {
        cin >> repeat;
    }
    for(int i=0; i<repeat; i++)
    {
        char str[100];  
        cin >> str;
        int len = strlen(str);
        while(len>100)
        {
            cin >> str;
            len = strlen(str);
        }
        char c;          
        cin >> c;
        delchar(str, c); 
        cout << "result: " << str << endl;
    }
    return 0;
}

 

 

 

 

7-10 使用函数实现字符串复制

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

void strmcpy(string &s, const string &t, int m) 
{
    s.resize(0);
    if (m <= t.size())
    {  
        copy(t.begin() + m - 1, t.end(), back_inserter(s)); 
    }
    else 
    {
        s = "error input";
    }
}

int main() 
{
    int repeat;
    cin >> repeat;
    while (repeat--) 
    {  
        string t, s;
        int m;
        cin >> t >> m;
        strmcpy(s, t, m);
        cout << s << endl;
    }
    return 0;
}

 

 

 

 

7-11 找最大的字符串

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

int main()
{
    vector<string> str(5);
    for (auto& s : str) 
    {
        cin >> s;
    }
    auto max_str = *max_element(str.begin(), str.end());
    cout << "Max is: " << max_str << endl;
    return 0;
}

 

 

 

 

7-6 组织星期信息

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

int main() 
{
    int repeat;
    cin >> repeat;
    unordered_map<string, int> weeks = {
        {"Sunday", 1}, {"Monday", 2}, {"Tuesday", 3},
        {"Wednesday", 4}, {"Thursday", 5}, {"Friday", 6}, {"Saturday", 7}
    };
    while (repeat--) 
    {
        string str;
        cin >> str;
        int index = weeks.find(str) == weeks.end() ? -1 : weeks[str];
        cout << index << endl;
    }
    return 0;
}

 

 

 

 

7-12 查找奥运五环色的位置

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

int main()
{
    unordered_map<string, int> colors = {
        {"red", 1},
        {"blue", 2},
        {"yellow", 3},
        {"green", 4},
        {"black", 5}
    };
    string input_color;
    getline(cin, input_color);
    if (auto it = colors.find(input_color); it != colors.end()) 
    {
        cout << it->second << endl;
    } 
    else
    {
        cout << "Not Found" << endl;
    }
    return 0;
}

 

 

 

 

7-5 实数排序

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<double> nums(n);
    for (auto &num : nums)
    {
        cin >> num;
    }
    sort(nums.begin(), nums.end(), greater<double>());
    for (int i = 0; i < n - 1; i++)
    {
        cout << fixed << setprecision(2) << nums[i] << " ";
    }
    cout << fixed << setprecision(2) << nums[n - 1] << endl;

    return 0;
}

 

 

 

 

7-13 字符串的连接

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

int main() 
{
    string s1, s2;
    getline(cin, s1);
    getline(cin, s2);
    for (char c : s2)
        s1 += c;
    cout << s1 << endl;
    return 0;
}

 

标签:SYNU,cout,int,cin,PTA,repeat,C++,str,include
From: https://www.cnblogs.com/Elysia1111/p/17231664.html

相关文章

  • C++ mutex,lock,unlock,lockguard
    //model/util.h#pragmaonce#include<algorithm>#include<chrono>#include<ctime>#include<fstream>#include<iomanip>#include<iostream>#include<map>......
  • c++钟楼
    #include<iostream>#include<string>#include"minecraft.h"usingnamespacestd;TxMinecraftmc;intx,y,z,id,data;voida(){ //正方形 mc.fillBlocks(x,y-1......
  • c++常用STL库及常用函数
    临近各种算法比赛,相信很多人想笔者一样还总是记不住很多函数的用法,特此总结一下常用的STL标准库以及标准函数,希望能够有所帮助。1.输入输出输入输出一般用两个标准库:#i......
  • c++ 影响多线程速度的因素记录
    目录0.序言1.缓存行同步问题/共享数据竞争1.1测试代码1.2测试逻辑1.3测试结果1.4小结2.任务颗粒度过小问题2.1测试代码2.1测试逻辑2.2测试结果2.3小结3.缓存未......
  • C++ class struct
    classandstruct目录前文问题对象与引用引用的传递对象copyshallowcopydepthcopymemcpy(data,a.data,sizeof(T)*n);简单类型复杂类型指针类型的......
  • C++ const的理解
    const​const修饰的变量不能再作为左值,初始化后值不能被修改C和C++const的区别​C语言中const修饰的值是常变量,不是常量,只是不能作为左值被修改voidmain(){......
  • 周六900C++模拟测试2023.3.18
     2023江南万达校区能力测试说明:1、在桌面以自己名字命名(中文名)建立文件夹;2、文件夹中存储每个题目对应的英文题目名.cpp文件; 中文题目名称小L的能量检测......
  • 第十三届蓝桥杯国赛 C++ B 组 J 题——搬砖(AC)
    目录​​1.搬砖​​​​1.题目描述​​​​2.输入格式​​​​3.输出格式​​​​4.样例输入​​​​5.样例输出​​​​6.数据范围​​​​7.原题链接​​​​2.解题思路​......
  • C++ STL 容器的size_type
    在C++STL容器中,size_type是一个无符号整数类型,用于表示容器中元素的数量或大小。由于不同平台和编译器有不同的实现,因此使用size_type可以确保代码的可移植性和兼容......
  • C++ mutex lock,unlock
    #model/util.h#pragmaonce#include<chrono>#include<ctime>#include<fstream>#include<functional>#include<iomanip>#include<iostream>#include<list>......