首页 > 其他分享 >实验一

实验一

时间:2024-10-15 17:26:47浏览次数:1  
标签:begin end cout int v0 实验 include

  1 //
  2 // 现代C++标准库、算法库体验
  3 // 本例用到以下内容:
  4 // 1. 字符串string, 动态数组容器类vector、迭代器
  5 // 2. 算法库:反转元素次序、旋转元素
  6 // 3. 函数模板、const引用作为形参
  7 
  8 #include <iostream>
  9 #include <string>
 10 #include <vector>
 11 #include <algorithm>
 12 
 13 using namespace std;
 14 
 15 // 声明
 16 // 模板函数声明
 17 template<typename T>
 18 void output(const T &c);
 19 
 20 // 普通函数声明
 21 void test1();
 22 void test2();
 23 void test3();
 24 
 25 int main() {
 26 cout << "测试1: \n";
 27 test1();
 28 
 29 cout << "\n测试2: \n";
 30 test2();
 31 
 32 cout << "\n测试3: \n";
 33 test3();
 34 }
 35 
 36 // 函数实现
 37 // 输出容器对象c中的元素
 38 template <typename T>
 39 void output(const T &c) {
 40 for(auto &i: c)
 41 cout << i << " ";
 42 cout << endl;
 43 }
 44 
 45 // 测试1
 46 // 组合使用算法库、迭代器、string反转字符串
 47 void test1() {
 48 string s0{"0123456789"};
 49 cout << "s0 = " << s0 << endl;
 50 
 51 string s1{s0};
 52 reverse(s1.begin(), s1.end()); // 反转指定迭代器区间的元素
 53 cout << "s1 = " << s1 << endl;
 54 
 55 string s2{s0};
 56 reverse_copy(s0.begin(), s0.end(), s2.begin()); // 将指定迭代区间的元素拷贝到指定迭代器开始的目标区间,并且在复制过程中反转次序
 57 cout << "s2 = " << s2 << endl;
 58 }
 59 
 60 // 测试2
 61 // 组合使用算法库、迭代器、vector反转动态数组对象vector内数据
 62 void test2() {
 63 vector<int> v0{2, 0, 4, 9};
 64 cout << "v0: ";
 65 output(v0);
 66 
 67 vector<int> v1{v0};
 68 reverse(v1.begin(), v1.end());
 69 cout << "v1: ";
 70 output(v1);
 71 
 72 vector<int> v2{v0};
 73 reverse_copy(v0.begin(), v0.end(), v2.begin());
 74 cout << "v2: ";
 75 output(v2);
 76 }
 77 
 78 // 测试3
 79 // 组合使用算法库、迭代器、vector实现元素旋转移位
 80 void test3() {
 81 vector<int> v0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 82 cout << "v0: ";
 83 output(v0);
 84 
 85 vector<int> v1{v0};
 86 rotate(v1.begin(), v1.begin()+1, v1.end()); // 旋转指定迭代器区间[v1.begin(), v1.end())之间的数据项,旋转后从迭代器v1.begin()+1位置的数据项开始
 87 cout << "v1: ";
 88 output(v1);
 89 
 90 vector<int> v2{v0};
 91 rotate(v2.begin(), v2.begin()+2, v2.end());
 92 cout << "v2: ";
 93 output(v2);
 94 
 95 vector<int> v3{v0};
 96 rotate(v3.begin(), v3.end()-1, v3.end());
 97 cout << "v3: ";
 98 output(v3);
 99 
100 vector<int> v4{v0};
101 rotate(v4.begin(), v4.end()-2, v4.end());
102 cout << "v4: ";
103 output(v4);
104 }
105 
106  

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 #include<algorithm>
 5 #include<numeric>
 6 #include<iomanip>
 7 using namespace std;
 8 template<typename T>
 9 void output(const T& c) {
10     for (auto& i : c)
11         cout << i << " ";
12     cout << endl;
13 }
14 int rand_int_100() {
15     return rand() % 101;
16 }
17 void test1() {
18     vector<int> v0(10);
19     generate(v0.begin(), v0.end(), rand_int_100);
20     cout << "v0:";
21     output(v0);
22     vector<int> v1{ v0 };
23     sort(v1.begin(), v1.end());
24     cout << "v1:";
25     output(v1);
26     vector<int> v2{ v0 };
27     sort(v2.begin() + 1, v2.end() - 1);
28     cout << "v2:";
29     output(v2);
30 }
31 void test2() {
32     vector<int> v0(10);
33     generate(v0.begin(), v0.end(), rand_int_100);
34     cout << "v0:";
35     output(v0);
36     auto iter1 = min_element(v0.begin(), v0.end());
37     cout << "最小值:" << *iter1 << endl;
38     auto iter2 = max_element(v0.begin(), v0.end());
39     cout << "最大值:" << *iter2 << endl;
40     auto ans = minmax_element(v0.begin(), v0.end());
41     cout << "最小值:" << *(ans.first) << endl;
42     cout << "最大值:" << *(ans.second) << endl;
43     double avg1 = accumulate(v0.begin(), v0.end(), 0) / v0.size();
44     cout << "均值:" << fixed << setprecision(2) << avg1 << endl;
45     cout << endl;
46     vector<int> v1{ v0 };
47     cout << "v0:";
48     output(v0);
49     sort(v1.begin(), v1.end());
50     double avg2 = accumulate(v1.begin() + 1, v1.end() - 1, 0) / (v1.size() - 2);
51     cout << "去掉最大值、最小值之后,均值:" << avg2 << endl;
52 
53 }
54 int main() {
55     cout << "测试1:\n";
56     test1();
57     cout << "测试2:\n";
58     test2();
59 }
View Code

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 using namespace std;
 5 bool is_palindrome(string s) {
 6     string s1{ s };
 7     reverse(s1.begin(), s1.end());
 8     if (s1 == s)
 9         return true;
10     return false;
11 }
12 int main() {
13     string s;
14     while (cin >> s)
15         cout << boolalpha << is_palindrome(s) << endl;
16 
17 }
View Code

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 
 5 bool is_palindrome(std::string s);
 6 
 7 int main() {
 8     using namespace std;
 9     string s;
10 
11     while(cin >> s)  // 多组输入,直到按下Ctrl+Z后结束测试
12         cout << boolalpha << is_palindrome(s) << endl;
13 
14 }
15 
16 // 函数is_palindrom定义
17 // 待补足
18 // ×××
19 bool is_palindrome(std::string s){
20     std::string b{s};
21     reverse(s.begin(),s.end());
22     if(s == b){
23         return true;
24     }else{
25         return false;
26     }
27 }
View Code

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main() {
 5     cout << "  ";
 6     vector<char> arr;
 7     for(int i = 0;i < 26;i++){
 8         cout << " ";
 9         cout << char('a' + i);
10         arr.push_back(char('A' + i));
11     }
12     cout << endl;
13     for(int i = 1;i <= 26;i++){
14         cout << setw(2) << setfill(' ') << i;
15         rotate(arr.begin(),arr.begin() + 1,arr.end());
16         for(auto ele:arr){
17             cout << " ";
18             cout << ele;
19         }
20         cout << endl;
21     }
22 
23 
24     return 0;
25 }
View Code

 1 #include <iostream>
 2 #include <vector>
 3 #include <iomanip>
 4 #include <numeric>
 5 #include <time.h>
 6 
 7 using namespace std;
 8 
 9 int calculation();
10 int rand_int(int range) 
11 {
12     srand((unsigned)time(NULL));
13     return rand() % (range + 1);
14 }
15 char rand_flag() {
16     vector<char> v = { '+', '-', '*',  '/' };
17     return v[rand_int(3)];
18 }
19 void output(int a, int b, char flag) {
20     cout << a << " " << flag << " " << b << " = ";
21 }
22 
23 int main() 
24 {
25     int num = 0;
26     double ave = 0;
27     while (++num <= 10) 
28      {
29         int ans, given_ans;
30         ans = calculation();
31         cin >> given_ans;
32         if (ans == given_ans)
33             ave++;
34     }
35     cout << "正确率:" << fixed << setprecision(2) << ave * 10 << "%" << endl;
36 }
37 
38 int calculation() 
39 {
40     char flag = rand_flag();
41     if (flag == '+') 
42     {
43         int a = rand_int(10), b = rand_int(10);
44         output(a, b, flag);
45         return a + b;
46     }
47     else if (flag == '*') 
48     {
49         int a = rand_int(10), b = rand_int(10);
50         output(a, b, flag);
51         return a * b;
52     }
53     else if (flag == '-')
54     {
55         int a = rand_int(10), b = rand_int(10);
56         while (a < b) 
57        {
58             int t = a;
59             a = b;
60             b = t;
61         }
62         output(a, b, flag);
63         return a - b;
64     }
65     else
66     {
67         int a = rand_int(10);
68         vector<int> v;
69         int t = 0;
70         while (1) 
71        {
72             for (int i = 1; i <= a; i++) 
73            {
74                 if (a % i == 0)
75                    {
76                     v.push_back(i);
77                     t++;
78                 }
79             }
80             if (t == 0)
81                 a = rand_int(10);
82             else
83                 break;
84         }
85         int b = v[rand_int(t - 1)];
86         output(a, b, flag);
87         return a / b;
88     }
89 }
View Code

 总结:比起巩固课上知识,我可能通过别人的代码学到的函数更多……可能我确实太菜了……多敲代码多学点吧……

标签:begin,end,cout,int,v0,实验,include
From: https://www.cnblogs.com/WZX1521105313/p/18453526

相关文章

  • 实验1 现代C++编程初体验
    一、实验目的体验现代C++标准库、算法库用法灵活组合使用现代C++基础语言特性(数据表示、分支、循环、函数)和标准库,编程解决简单、基础问题编程代码过程中,注意编码素养。关注代码表达,提升代码的可读性、易于维护性二、实验准备系统浏览教材以下章节,对现代c++基础语言特性和......
  • 20222301 2024-2025-1 《网络与系统攻防技术》实验二实验报告
    1.实验内容本次实验主要围绕渗透测试与远程执行控制展开,通过不同工具和技术手段实现了对目标主机的深入渗透与监控。实验内容可以概括为以下几个方面:1.远程Shell获取:实验首先通过`netcat`和`cron`定时任务,以及`socat`与Windows任务计划相结合的方式,实现了对目标主机的远程Shell......
  • 实验2
    实验任务1include<stdio.h>include<stdlib.h>include<time.h>defineN5defineN1397defineN2476defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;while(cnt<N){random_major=rand()......
  • 数据采集实验一
    题目一(1)要求:用requests和BeautifulSoup库方法定向爬取给定网址(http://www.shanghairanking.cn/rankings/bcur/2020)的数据,屏幕打印爬取的大学排名信息。pytho......
  • 程序实际实验1实验报告
    实验任务11#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78template<typenameT>9voidoutput(constT&c);1011voidtest1();12voidtest2();13......
  • 实验一
    任务1#include#include#include#includeusingnamespacestd;//声明//模板函数声明templatevoidoutput(constT&c);//普通函数声明voidtest1();voidtest2();voidtest3();intmain(){ cout<<"测试1:\n"; test1(); cout<......
  • jsp高校空闲实验室资源预约管理系统77gmb
    jsp高校空闲实验室资源预约管理系统77gmb本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表项目功能学生,实验室信息,学生预约实验室,教师,设备信息,教师预约实验室,学生取消预约,教师取消预约技术要求:  ......
  • 实验1 现代C++编程初体验
    实验任务1代码:#include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;//声明//模板函数声明template<typenameT>voidoutput(constT&c);//普通函数声明voidtest1();voidtest2();voidtest......
  • OpenAI发布了一项实验性框架“Swarm”
      每周跟踪AI热点新闻动向和震撼发展想要探索生成式人工智能的前沿进展吗?订阅我们的简报,深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同,从行业内部的深度分析和实用指南中受益。不要错过这个机会,成为AI领域的领跑者。点击订阅,与未来同行!订阅:https://......
  • 实验1 现代C++编程初体验
    实验任务1:task1.cpp点击查看代码//现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>......