<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      任務(wù)1:

      源代碼task1.cpp

      // 現(xiàn)代C++標(biāo)準(zhǔn)庫、算法庫體驗(yàn)
      // 本例用到以下內(nèi)容:
      // 1. 字符串string, 動(dòng)態(tài)數(shù)組容器類vector、迭代器
      // 2. 算法庫:反轉(zhuǎn)元素次序、旋轉(zhuǎn)元素
      // 3. 函數(shù)模板、const引用作為形參
      #include <iostream>
      #include <string>
      #include <vector>
      #include <algorithm>
      // 模板函數(shù)聲明
      template<typename T>
      void output(const T &c);
      void test1();
      void test2();
      void test3();
      int main() {
      std::cout << "測試1: \n";
      test1();
      std::cout << "\n測試2: \n";
      test2();
      std::cout << "\n測試3: \n";
      test3();
      }
      // 輸出容器對象c中的元素
      template <typename T>
      void output(const T &c) {
      for(auto &i : c)
      std::cout << i << ' ';
      std::cout << '\n';
      }
      // 測試1:組合使用算法庫、迭代器、string反轉(zhuǎn)字符串
      void test1() {
      using namespace std;
      string s0{"0123456789"};
      cout << "s0 = " << s0 << endl;
      string s1(s0);
      // 反轉(zhuǎn)s1自身
      reverse(s1.begin(), s1.end());
      cout << "s1 = " << s1 << endl;
      string s2(s0.size(), ' ');
      // 將s0反轉(zhuǎn)后結(jié)果拷貝到s2, s0自身不變
      reverse_copy(s0.begin(), s0.end(), s2.begin());
      cout << "s2 = " << s2 << endl;
      }
      // 測試2:組合使用算法庫、迭代器、vector反轉(zhuǎn)動(dòng)態(tài)數(shù)組對象vector內(nèi)數(shù)據(jù)
      void test2() {
      using namespace std;
      vector<int> v0{2, 0, 4, 9};
      cout << "v0: "; output(v0);
      vector<int> v1{v0};
      reverse(v1.begin(), v1.end());
      cout << "v1: "; output(v1);
      vector<int> v2{v0};
      reverse_copy(v0.begin(), v0.end(), v2.begin());
      cout << "v2: "; output(v2);
      }
      // 測試3:組合使用算法庫、迭代器、vector實(shí)現(xiàn)元素旋轉(zhuǎn)移位
      void test3() {
      using namespace std;
      vector<int> v0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      cout << "v0: "; output(v0);
      vector<int> v1{v0};
      // 將[v1.begin(), v1.end())區(qū)間內(nèi)元素循環(huán)左移1位
      rotate(v1.begin(), v1.begin()+1, v1.end());
      cout << "v1: "; output(v1);
      vector<int> v2{v0};
      // 將[v1.begin(), v1.end())區(qū)間內(nèi)元素循環(huán)左移2位
      rotate(v2.begin(), v2.begin()+2, v2.end());
      cout << "v2: "; output(v2);
      vector<int> v3{v0};
      // 將[v1.begin(), v1.end())區(qū)間內(nèi)元素循環(huán)右移1位
      rotate(v3.begin(), v3.end()-1, v3.end());
      cout << "v3: "; output(v3);
      vector<int> v4{v0};
      // 將[v1.begin(), v1.end())區(qū)間內(nèi)元素循環(huán)右移2位
      rotate(v4.begin(), v4.end()-2, v4.end());
      cout << "v4: "; output(v4);
      }

      運(yùn)行結(jié)果截圖:

      屏幕截圖 2025-10-16 155248

      問題 1:reverse 原地反轉(zhuǎn)元素,reverse_copy 反轉(zhuǎn)后復(fù)制到新范圍且不修改原容器。

      問題 2:rotate 將 [first, middle) 與 [middle, last) 交換順序,參數(shù)分別是范圍起始、分割點(diǎn)、范圍結(jié)束迭代器。

      任務(wù)2:

      源代碼task2.cpp

       

      #include <iostream>
      #include <vector>
      #include <algorithm>
      #include <numeric>
      #include <iomanip>
      #include <cstdlib>
      #include <ctime>
      // 模板函數(shù)聲明
      template<typename T>
      void output(const T &c);
      int generate_random_number();
      void test1();
      void test2();
      int main() {
      std::srand(std::time(0)); // 添加隨機(jī)種子
      std::cout << "測試1: \n";
      test1();
      std::cout << "\n測試2: \n";
      test2();
      }
      // 輸出容器對象c中的元素
      template <typename T>
      void output(const T &c) {
      for(auto &i: c)
      std::cout << i << ' ';
      std::cout << '\n';
      }
      // 返回[0, 100]區(qū)間內(nèi)的一個(gè)隨機(jī)整數(shù)
      int generate_random_number() {
      return std::rand() % 101;
      }
      // 測試1:對容器類對象指定迭代器區(qū)間賦值、排序
      void test1() {
      using namespace std;
      vector<int> v0(10); // 創(chuàng)建一個(gè)動(dòng)態(tài)數(shù)組對象v0, 對象大小為10
      generate(v0.begin(), v0.end(), generate_random_number); // 生成隨機(jī)數(shù)填充v0
      cout << "v0: "; output(v0);
      vector<int> v1{v0};
      sort(v1.begin(), v1.end()); // 對整個(gè)vector排序
      cout << "v1: "; output(v1);
      vector<int> v2{v0};
      sort(v2.begin()+1, v2.end()-1); // 只對中間部分排序,不包含首尾元素
      cout << "v2: "; output(v2);
      }
      // 測試2:對容器類對象指定迭代器區(qū)間賦值、計(jì)算最大值/最小值/均值
      void test2() {
      using namespace std;
      vector<int> v0(10);
      generate(v0.begin(), v0.end(), generate_random_number);
      cout << "v0: "; output(v0);
      // 求最大值和最小值
      auto min_iter = min_element(v0.begin(), v0.end());
      auto max_iter = max_element(v0.begin(), v0.end());
      cout << "最小值: " << *min_iter << endl;
      cout << "最大值: " << *max_iter << endl;
      // 同時(shí)求最大值和最小值
      auto ans = minmax_element(v0.begin(), v0.end());
      cout << "最小值: " << *(ans.first) << endl;
      cout << "最大值: " << *(ans.second) << endl;
      // 求平均值
      double avg1 = accumulate(v0.begin(), v0.end(), 0.0) / v0.size();
      cout << "均值: " << fixed << setprecision(2) << avg1 << endl;
      sort(v0.begin(), v0.end());
      double avg2 = accumulate(v0.begin()+1, v0.end()-1, 0.0) / (v0.size()-2);
      cout << "去掉最大值、最小值之后,均值: " << avg2 << endl;
      }

      運(yùn)行結(jié)果截圖:

      屏幕截圖 2025-10-16 170536

      1. generate 算法的作用:通過調(diào)用指定函數(shù)生成元素,填充給定范圍內(nèi)的容器(覆蓋原有元素)。
      2. minmax_element 的優(yōu)勢:一次遍歷即可同時(shí)獲取范圍內(nèi)的最小和最大元素,比分別調(diào)用 min_element 和 max_element 減少一次遍歷,效率更高,且直接返回包含兩個(gè)結(jié)果的 pair,使用更簡潔。
      3. 等同

      任務(wù)3:

      源代碼task3.cpp

      #include <iostream>
      #include <string>
      #include <algorithm>
      #include <cctype>
      unsigned char func(unsigned char c);
      void test1();
      void test2();
      int main() {
      std::cout << "測試1: 字符串大小寫轉(zhuǎn)換\n";
      test1();
      std::cout << "\n測試2: 字符變換\n";
      test2();
      }
      unsigned char func(unsigned char c) {
      if(c == 'z')
      return 'a';
      if(c == 'Z')
      return 'A';
      if(std::isalpha(c))
      return static_cast<unsigned char>(c+1);
      return c;
      }
      void test1() {
      std::string s1{"Hello World 2049!"};
      std::cout << "s1 = " << s1 << '\n';
      std::string s2;
      for(auto c: s1)
      s2 += std::tolower(c);
      std::cout << "s2 = " << s2 << '\n';
      std::string s3;
      for(auto c: s1)
      s3 += std::toupper(c);
      std::cout << "s3 = " << s3 << '\n';
      }
      void test2() {
      std::string s1{"I love cosmos!"};
      std::cout << "s1 = " << s1 << '\n';
      std::string s2(s1.size(), ' ');
      std::transform(s1.begin(), s1.end(),
      s2.begin(),
      func);
      std::cout << "s2 = " << s2 << '\n';
      }

      運(yùn)行結(jié)果截圖:

      屏幕截圖 2025-10-16 171315

      1. func 將字母轉(zhuǎn)為下一個(gè)字母('z' 轉(zhuǎn) 'a','Z' 轉(zhuǎn) 'A'),非字母不變。
      2. tolower 轉(zhuǎn)小寫字母,toupper 轉(zhuǎn)大寫字母,非字母不變。
      3. transform 參數(shù)依次為輸入起始、輸入結(jié)束、輸出起始、轉(zhuǎn)換函數(shù);第 3 個(gè)參數(shù)改 s1.begin() 會(huì)覆蓋 s1 原內(nèi)容。

       

      任務(wù)4:

      源代碼task4.cpp

       

      #include <iostream>
      #include <string>
      #include <algorithm>
      #include <cctype>
      
      bool is_palindrome(const std::string &s);
      bool is_palindrome_ignore_case(const std::string &s);
      
      int main() {
          using namespace std;
          string s;
          while (cin >> s) {
              cout << boolalpha
                   << "區(qū)分大小寫: " << is_palindrome(s) << "\n"
                   << "不區(qū)分大小寫: " << is_palindrome_ignore_case(s) << "\n\n";
          }
      }
      bool is_palindrome(const std::string &s) {
          int left = 0, right = s.size() - 1;
          while (left < right) {
              if (s[left] != s[right]) {
                  return false;
              }
              left++;
              right--;
          }
          return true;
      }
      bool is_palindrome_ignore_case(const std::string &s) {
          int left = 0, right = s.size() - 1;
          while (left < right) {
              char c1 = tolower(s[left]);
              char c2 = tolower(s[right]);
              if (c1 != c2) {
                  return false;
              }
              left++;
              right--;
          }
          return true;
      }

      運(yùn)行結(jié)果截圖:

      屏幕截圖 2025-10-17 191929

      問題:用std::getline(cin, s)替代cin >> s,可輸入含空格的字符串。

      任務(wù)5:

      源代碼task5.cpp

      #include <iostream>
      #include <string>
      #include <algorithm>
      
      std::string dec2n(int x, int n = 2);
      
      int main() {
          int x;
          while (std::cin >> x) {
              std::cout << "十進(jìn)制: " << x << '\n'
                        << "二進(jìn)制: " << dec2n(x) << '\n'
                        << "八進(jìn)制: " << dec2n(x, 8) << '\n'
                        << "十二進(jìn)制: " << dec2n(x, 12) << '\n'
                        << "十六進(jìn)制: " << dec2n(x, 16) << '\n'
                        << "三十二進(jìn)制: " << dec2n(x, 32) << "\n\n";
          }
      }
      std::string dec2n(int x, int n) {
          if (x == 0) { 
              return "0";
          }
          std::string result;
          while (x > 0) {
              int remainder = x % n;
              char digit;
              if (remainder < 10) {
                  digit = '0' + remainder;
              } else {
                  digit = 'A' + remainder - 10;
              }
              result.push_back(digit);
              x = x / n;
          }
          std::reverse(result.begin(), result.end()); 
          return result;
      }

      運(yùn)行結(jié)果截圖:

      屏幕截圖 2025-10-17 192700

      任務(wù)6:

      源代碼task6.cpp

      #include <iostream>
      #include <string>
      #include <cctype>
      using namespace std;
      
      int main() {
          string src = "abcdefghijklmnopqrstuvwxyz";
          cout << src << endl;
          for (int line = 1; line <= 26; line++) {
              string cipher = src.substr(line) + src.substr(0, line);
              for (char& ch : cipher) {
                  ch = toupper(ch);
              }
              printf("%2d%s\n", line, cipher.c_str());
          }
      
          return 0;
      }

      運(yùn)行結(jié)果截圖:

      屏幕截圖 2025-10-17 194034

      任務(wù)7:

      源代碼task7.cpp

      #include <iostream>
      #include <cstdlib>
      #include <ctime>
      #include <iomanip>
      using namespace std;
      
      int main() {
          srand(time(0));
          int correct = 0; 
          for (int i = 1; i <= 10; i++) {   
              int a = rand() % 10 + 1;
              int b = rand() % 10 + 1;
              int op = rand() % 4;
              int result, user_ans;
              switch (op) {
                  case 0: 
                      result = a + b;
                      cout << a << " + " << b << " = ";
                      break;
                  case 1: 
                      if (a < b) swap(a, b);
                      result = a - b;
                      cout << a << " - " << b << " = ";
                      break;
                  case 2: 
                      result = a * b;
                      cout << a << " * " << b << " = ";
                      break;
                  case 3: 
                      while (b == 0 || a % b != 0) {
                          b = rand() % 10 + 1;
                          a = rand() % 10 + 1;
                      }
                      result = a / b;
                      cout << a << " / " << b << " = ";
                      break;
              }
              cin >> user_ans;
              if (user_ans == result) {
                  correct++;
              }
          }
          double accuracy = (double)correct / 10 * 100;
          cout << "正確率:" << fixed << setprecision(2) << accuracy << "%" << endl;
      
          return 0;
      }

      運(yùn)行結(jié)果截圖:

      屏幕截圖 2025-10-17 194408

       

      posted on 2025-10-17 19:59  mm77777  閱讀(8)  評(píng)論(1)    收藏  舉報(bào)
      主站蜘蛛池模板: 国产高清亚洲一区亚洲二区| 国产精品国产三级国产午| 一本久久a久久精品综合| 丝袜a∨在线一区二区三区不卡| 国产一区二区三区九精品| 久久久精品国产精品久久| 亚洲成av人片天堂网老年人| 久久精品久久黄色片看看| 国产午精品午夜福利757视频播放| 国产丰满老熟女重口对白 | 国产精品乱一区二区三区| 驻马店市| 九九热精彩视频在线免费| 国产美女MM131爽爽爽| 麻豆国产AV剧情偷闻女邻居内裤| 少妇被粗大的猛烈进出视频| 国产AV影片麻豆精品传媒| 亚洲av在线观看| 精品人妻中文字幕在线| 各种少妇wbb撒尿| 日本中文一二区有码在线| 在线看无码的免费网站| 成人永久性免费在线视频| 一区二区三区av天堂| 久久精品蜜芽亚洲国产AV| 欧美亚洲h在线一区二区| 国产精品内射在线免费看| 精品亚洲精品日韩精品| 国产精品亚洲第一区在线| 国产精品亚洲二区在线播放| 亚洲AV无码专区亚洲AV桃| 日韩精品一区二区三区激| 免费看婬乱a欧美大片| av无码一区二区大桥久未| 丰都县| 国产仑乱无码内谢| 日韩有码中文在线观看| 久久久国产精品樱花网站| 99中文字幕国产精品| 少妇被粗大的猛进69视频 | 一区二区三区av天堂|