题目详细内容见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