實驗任務1
task1.cpp:
View Code
執行測試截圖:
實驗任務2
task2.cpp:
View Code
執行測試截圖:
實驗任務3
task3.cpp:
#include <iostream> #include <string> #include <algorithm> bool is_palindrome(std::string s); int main() { using namespace std; string s; while (cin >> s) // 多組輸入,直到按下Ctrl+Z後結束測試 cout << boolalpha << is_palindrome(s) << endl; } // 函式is_palindrom定義 bool is_palindrome(std::string s) { int len = s.length(); for (int i = 0; i < len / 2; i++) { if (s.at(i) != s.at(len - i - 1)) { return false; } } return true; }
執行測試截圖:
實驗任務4
task4.cpp:
#include <iostream> #include <string> #include <algorithm> std::string dec2n(int x, int n = 2); int main() { using namespace std; int x; while(cin >> x) { cout << "十進位制: " << x << endl; cout << "二進位制: " << dec2n(x) << endl; cout << "八進位制: " << dec2n(x, 8) << endl; cout << "十六進位制: " << dec2n(x, 16) << endl << endl; } } // 函式dec2n定義 std::string dec2n(int x, int n) { std::string result; if (x == 0) { return "0"; } while (x > 0) { int a = x % n; if (a < 10) { result += a + '0'; } else { result += (a - 10) + 'A'; } x /= n; } reverse(result.begin(), result.end()); return result; }
執行測試截圖:
實驗任務5
task5.cpp:
1 #include<iostream> 2 #include<iomanip> 3 using namespace std; 4 int main() 5 { 6 int count=1; 7 int i,j; 8 cout<<" "; 9 for(i=97;i<97+26;i++) 10 { 11 cout<<setw(2)<<char(i); 12 } 13 cout<<endl; 14 for(i=1;i<=26;i++) 15 { 16 cout<<setw(2)<<i; 17 for(j=i+1;j<=26;j++) 18 { 19 cout<<setw(2)<<char(j+64); 20 } 21 for(j=1;j<=i;j++) 22 { 23 cout<<setw(2)<<char(j+64); 24 } 25 cout<<endl; 26 } 27 return 0; 28 }
執行測試截圖:
實驗任務6
task6.cpp:
#include<iostream> #include<vector> #include<algorithm> #include<iomanip> #include<ctime> using namespace std; int main() { int count = 0; const int total = 10; for (int i = 0; i < total; i++) { int num1 = (time(nullptr) + i) % 11; int num2 = (time(nullptr) + i + 1) % 11; char operation; int answer; int r = (time(nullptr) + i + 2) % 4; switch (r) { case 0: operation = '+'; answer = num1 + num2; break; case 1: operation = '-'; if (num1 < num2) swap(num1, num2); answer = num1 - num2; break; case 2: operation = '*'; answer = num1 * num2; break; case 3: operation = '/'; while (num1 % num2 != 0) num1 = (time(nullptr) + i + 3) % 11; answer = num1 / num2; break; } int myanswer; cout << num1 << operation << num2 << "="; cin >> myanswer; if (myanswer == answer) { count++; } } double t = static_cast<double>(count) / (total) * 100; cout << "正確率:" << fixed << setprecision(2) << t << "%" << endl; return 0; }
執行測試截圖: