yhjXW

导航

 

任务1:

源代码task1.cpp

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 //模板函数声明 
 5 template<typename T>
 6 void output(const T &c);
 7 
 8 //普通函数声明 
 9 void test1();
10 void test2();
11 void test3();
12 
13 int main(){
14     cout<<"测试1:\n";
15     test1();
16     
17     cout<<"测试2:\n";
18     test2();
19     
20     cout<<"测试3:\n";
21     test3();
22     
23 }
24 
25 //输出容器对象C中的元素 
26 template <typename T>
27 void output(const T &c)
28 {
29     for(auto &i: c)
30         cout<<i<<" ";
31     cout<<endl;
32 }
33 
34 void test1(){
35     string s0{"0123456789"};
36     cout<<"s0 = "<<s0<<endl;
37     
38     string s1={s0};
39     reverse(s1.begin(),s1.end()); //从头至尾反转元素 
40     cout<<"s1 = "<<s1<<endl;
41     
42     string s2={s0};
43     reverse_copy(s0.begin(),s0.end(),s2.begin()); //从头至尾复制,从第0位开始反转 
44     cout<<"s2 = "<<s2<<endl; 
45 }
46 
47 void test2(){
48     vector<int> v0{2,0,4,9};
49     cout<<"v0: ";
50     output(v0);
51     
52     vector<int> v1{v0};
53     reverse(v1.begin(),v1.end());
54     cout<<"v1: ";
55     output(v1);
56     
57     vector<int> v2{v0};
58     reverse_copy(v0.begin(),v0.end(),v2.begin());
59     cout<<"v2: ";
60     output(v2);
61 }
62 
63 void test3(){
64     vector <int> v0{0,1,2,3,4,5,6,7,8,9};
65     cout<<"v0: ";
66     output(v0);
67     
68     vector<int> v1{v0};
69     rotate(v1.begin(),v1.begin()+1,v1.end());
70     //旋转指定迭代器区间[v1.begin(),v1.end()]之间的数据项 ,旋转后从迭代器v1.begin()+1位置的数据项开始 
71     cout<<"v1: ";
72     output(v1);
73     
74     vector<int> v2{v0};
75     rotate(v2.begin(),v2.begin()+2,v2.end());
76     cout<<"v2: ";
77     output(v2);
78     
79     vector<int> v3{v0};
80     rotate(v3.begin(),v3.end()-1,v3.end());
81     cout<<"v3: ";
82     output(v3);
83     
84     vector<int> v4{v0};
85     rotate(v4.begin(),v4.end()-2,v4.end());
86     cout<<"v4: ";
87     output(v4);
88     
89 }

运行结果截图:

 

任务2:

源代码task2.cpp

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

运行结果截图:

 

任务3:

源代码task3.cpp

 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 // 函数is_palindrom定义
16 bool is_palindrome(std::string s){
17     int len=s.size();
18     int t=0;
19     for(int i=0;i<len;i++)
20     {
21         if(s[i]!=s[len-1-i])
22             t=1;
23     }
24     if(t==0)
25         return true;
26     else
27         return false;
28 }

运行结果截图:

 

任务4:

源代码task4.cpp

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 
 5 std::string dec2n(int x, int n = 2);
 6 
 7 int main() {
 8     using namespace std;
 9 
10     int x;
11     while(cin >> x) {
12         cout << "十进制: " << x << endl;
13         cout << "二进制: " << dec2n(x) << endl;
14         cout << "八进制: " << dec2n(x, 8) << endl;
15         cout << "十六进制: " << dec2n(x, 16) << endl << endl;
16     }
17 }
18 
19 // 函数dec2n定义
20 std::string dec2n(int x,int n){
21     int t=0;
22     std::string ans; 
23     do
24     {
25         t=x%n;
26         if(t>=0&&t<=9)
27           ans+=(t+'0');
28         else
29           ans+=(t+'A'-10);
30         x/=n;
31     }while(x>0);
32     reverse(ans.begin(),ans.end());
33     return ans;
34 }

运行结果截图:

 

任务5:

源代码task5.cpp

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     string s1="abcdefghijklmnopqrstuvwxyz";
 6     string s2="ABCDEFGHIJKLNMOPQRSTUVWXYZ";
 7     cout<<"  ";
 8     for(int i=0;i<=26;i++)
 9         cout<<setw(2)<<s1[i];
10     cout<<endl;
11     for(int i=1;i<=26;i++)
12     {
13         cout<<setw(2)<<i;
14         for(int j=0;j<26;j++)
15         {
16             cout<<setw(2)<<s2[(j+i)%26];
17         }
18         cout<<endl;
19     }
20     cout<<endl;
21     return 0;
22 }

运行结果截图:

 

 

任务6:

源代码tssk6.cpp

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4 srand((unsigned)time(NULL));
 5     int ans;
 6     int a,b,c;
 7     double sum=0;
 8     char op;
 9     for(int i=1;i<=10;i++){
10         ans=0;
11         op="-+*/"[rand()%4];
12         if(op=='*')
13         {
14           a=rand()%10+1;
15           b=rand()%10+1;
16           ans=a*b;
17         }
18         else if(op=='+')
19         {
20           a=rand()%10+1;
21           b=rand()%10+1;
22           ans=a+b;
23         }
24           else if(op=='-')
25           {
26               a=rand()%10+1;
27                b=rand()%10+1;
28                while(b>a)
29                  b=rand()%10+1;
30                 ans=a-b;
31           }
32           else
33           {
34               a=rand()%10+1;
35               b=rand()%10+1;
36               while(a%b!=0)
37                 b=rand()%10+1;
38               ans=a/b;
39           }
40         cout<<a<<" "<<op<<" "<<b<<" = ";
41         cin>>c;
42         if(c==ans)
43           sum++;
44     }
45         printf("%.2lf%\n",sum*10);
46     return 0;
47 }

运行结果截图:

 

实验总结:

(1)reverse(s1.begin(),s1.end());   //从头至尾反转元素 

         reverse_copy(s0.begin(),s0.end(),s2.begin());    //从头至尾复制,从第0位开始反转 

         rotate(v1.begin(),v1.begin()+1,v1.end());   //旋转指定迭代器区间[v1.begin(),v1.end()]之间的数据项 ,旋转后从迭代器v1.begin()+1位置的数据项开始 

(2)srand((unsigned)time(NULL));

          int x=rand()%10+1;      //随机生成[1,10]的整数x

posted on 2024-10-14 19:53  於泓瑾  阅读(61)  评论(0)    收藏  举报