diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..cd5765f --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++", + "type": "cppdbg", + "request": "launch", + "program": "${fileDirname}/${fileBasenameNoExtension}.exe", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "miDebuggerPath": "gdb.exe", + "preLaunchTask": "compile", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + }, + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 4df6722..6dae62d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,14 +1,18 @@ { "files.associations": { - "vector": "cpp", - "algorithm": "cpp", + "sstream": "c", + "regex": "c", + "iostream": "cpp", + "ostream": "cpp", + "cmath": "cpp", + "ratio": "c", + "random": "c", + "array": "cpp", "atomic": "cpp", - "bit": "cpp", + "*.tcc": "cpp", "cctype": "cpp", "clocale": "cpp", - "cmath": "cpp", - "compare": "cpp", - "concepts": "cpp", + "cstdarg": "cpp", "cstddef": "cpp", "cstdint": "cpp", "cstdio": "cpp", @@ -16,37 +20,34 @@ "cstring": "cpp", "ctime": "cpp", "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "unordered_map": "cpp", + "vector": "cpp", "exception": "cpp", + "algorithm": "cpp", + "map": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "optional": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", "initializer_list": "cpp", - "ios": "cpp", "iosfwd": "cpp", - "iostream": "cpp", "istream": "cpp", - "iterator": "cpp", "limits": "cpp", - "memory": "cpp", "new": "cpp", - "ostream": "cpp", "stdexcept": "cpp", "streambuf": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", "typeinfo": "cpp", - "utility": "cpp", - "xfacet": "cpp", - "xiosbase": "cpp", - "xlocale": "cpp", - "xlocinfo": "cpp", - "xlocnum": "cpp", - "xmemory": "cpp", - "xstddef": "cpp", - "xstring": "cpp", - "xtr1common": "cpp", - "xutility": "cpp", - "queue": "cpp", - "numeric": "cpp", + "forward_list": "cpp", + "list": "cpp", "unordered_set": "cpp", - "xhash": "cpp" + "valarray": "cpp" } } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..5e55a48 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,43 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "shell", + "label": "compile", + "command": "gcc", + "args": [ + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "problemMatcher": [ + "$gcc" + ], + "group": "build" + }, + { + "type": "cppbuild", + "label": "C/C++: gcc.exe build active file", + "command": "E:\\mingw64\\mingw64\\mingw64\\bin\\gcc.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ] +} \ No newline at end of file diff --git a/BaiDu/list.cpp b/BaiDu/list.cpp new file mode 100644 index 0000000..515734c --- /dev/null +++ b/BaiDu/list.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; +struct ListNode +{ + int val; + ListNode* next; +}; + +struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2 ) { + // write code here + if(pHead1 == NULL)return pHead2; + if(pHead2 == NULL)return pHead1; + struct ListNode* p1 = (pHead1->val <= pHead2->val ? pHead1 : pHead2); + struct ListNode* p2 = (pHead1->val > pHead2->val ? pHead1 : pHead2); + struct ListNode* p = p1; //p1是主链 + // temp存放中间指针,有可能在p1中有可能在p2中,最终每次指向p1链两个node之间和大node最近的那个点 + struct ListNode* temp =NULL; + while((p1 != NULL)&&(p2 != NULL)){ + if(p1->val <= p2->val){ + temp = p1; + p1 = p1->next; + } + else { + temp->next = p2; + temp = p2; + p2 = p2->next; + temp->next = p1; + } + + } + if(p1 == NULL){ + temp->next =p2; + return p; + }else return p; +} + +int main(){ + ListNode* one = new ListNode; + one->val = 1; + ListNode* two = new ListNode; + two->val = 3; + ListNode* three = new ListNode; + three->val = 4; + ListNode* one2 = new ListNode; + one2->val = 1; + ListNode* two2 = new ListNode; + two2->val = 2; + ListNode* three2 = new ListNode; + three2->val = 5; + one->next = two; + two->next = three; + three->next = nullptr; + one2->next = two2; + two2->next = three2; + three2->next = nullptr; + ListNode* res = Merge(one, one2); + ListNode* cur = res; + cout << "init success" << endl; + while(cur != nullptr){ + cout << cur->val << endl; + cur = cur->next; + } + return 0; +} + diff --git a/BaiDu/output/list.exe b/BaiDu/output/list.exe new file mode 100644 index 0000000..2adcd6c Binary files /dev/null and b/BaiDu/output/list.exe differ diff --git a/BaiDu/output/ques1.exe b/BaiDu/output/ques1.exe new file mode 100644 index 0000000..19f8064 Binary files /dev/null and b/BaiDu/output/ques1.exe differ diff --git a/BaiDu/output/ques2.exe b/BaiDu/output/ques2.exe new file mode 100644 index 0000000..e08f0c4 Binary files /dev/null and b/BaiDu/output/ques2.exe differ diff --git a/BaiDu/output/ques3.exe b/BaiDu/output/ques3.exe new file mode 100644 index 0000000..1b97ef7 Binary files /dev/null and b/BaiDu/output/ques3.exe differ diff --git a/BaiDu/ques1.cpp b/BaiDu/ques1.cpp new file mode 100644 index 0000000..b73c670 --- /dev/null +++ b/BaiDu/ques1.cpp @@ -0,0 +1,46 @@ +/* +第一个的文字:小A的寝室里放着一个时钟。时钟有时针,分针,秒针三种指针。在某些时刻,时钟会记录下当前时间,格式为hh:mm:ss。 +时钟上看不出日期,每天零点,三根指针都会归零。现在小A得到了一系列被记录的时间,且这些时间是按照先后顺序被记录的。 +小A想让你算算,每两个时间点之间,秒针至少转了多少圈。注:一天有 24 个小时,1 小时有 60 分钟,1分钟有 60 秒,秒针一分钟转一圈。 +输入描述:第一行一个整数 n(2 ≤n < 10e5),代表时间序列中时间点的个数。第二行n个字符串,每个字符串代表一个时间点,格式为hh:mm:ss(0 ≤ hh< 24, 0≤ mm< 60, 0 ≤ ss < 60). +输出描述:n -1个数,第i个数表示第i到i十1时间点秒针转过的圈数。输出结果和答案的绝对误差或相对误差不超过 10e-5 即被认为正确。 + +*/ +#include +#include +#include +#include +#include +using namespace std; +// Helper function to convert time in "hh:mm:ss" format to total seconds from midnight +int timeToSeconds(const string& time) { + int hh = stoi(time.substr(0, 2)); + int mm = stoi(time.substr(3, 2)); + int ss = stoi(time.substr(6, 2)); + return hh * 3600 + mm * 60 + ss; +} +int main() { + int n; + cin >> n; + vector times(n); + + // Input all time points + for (int i = 0; i < n; ++i) { + cin >> times[i]; + } + // Calculate and print the number of full rotations of the second hand between consecutive time points + for (int i = 1; i < n; ++i) { + int t1 = timeToSeconds(times[i - 1]); + int t2 = timeToSeconds(times[i]); + + // If t2 is smaller, it means the time passed to the next day + if (t2 < t1) { + t2 += 24 * 3600; // add 24 hours in seconds + } + int secondsDiff = t2 - t1; + double rotations = secondsDiff / 60.0; // calculate the full rotations + printf("%.*f", 10, rotations); + // cout << setprecision(10) << rotations << endl; // print the result + } + return 0; +} diff --git a/BaiDu/ques2.cpp b/BaiDu/ques2.cpp new file mode 100644 index 0000000..36731af --- /dev/null +++ b/BaiDu/ques2.cpp @@ -0,0 +1,42 @@ +/* +我们称正整数A包含B,当且仅当(A|B)= A, |表示按位或运算,即B中的所有为1的二进制位,在A中都为1。现在给定n个正整数Ai,请你从中选出尽量少的整数,使得所有Ai,都至少被一个你选出来的整数包含。显然任何一个数总是包含其自身,即选择全部的数必定为一组合法答案(但不一定是最少的)。 +输入描述: +第一行一个正整数n,接下来一行n个整数,第i个整数表示Ai。0≤ Ai< 262144, 1 ≤n≤2x1e5 +输出描述: +一行一个整数,表示至少需要选出几个数字。 +*/ +#include +#include +#include +#include +using namespace std; +int main() { + int n; + cin >> n; + vector a(n); + unordered_set uniqueSet; + // Read input and add to the set to remove duplicates + for (int i = 0; i < n; ++i) { + cin >> a[i]; + uniqueSet.insert(a[i]); + } + // Convert set back to vector + vector uniqueNumbers(uniqueSet.begin(), uniqueSet.end()); + // Sort in descending order to apply the greedy strategy + sort(uniqueNumbers.rbegin(), uniqueNumbers.rend()); + int selectedCount = 0; + vector covered(uniqueNumbers.size(), false); + // For each number, see if it is necessary to cover the others + for (size_t i = 0; i < uniqueNumbers.size(); ++i) { + if (!covered[i]) { + selectedCount++; + for (size_t j = i + 1; j < uniqueNumbers.size(); ++j) { + if ((uniqueNumbers[i] | uniqueNumbers[j]) == uniqueNumbers[i]) { + covered[j] = true; + } + } + } + } + cout << selectedCount << endl; + return 0; +} diff --git a/BaiDu/ques3.cpp b/BaiDu/ques3.cpp new file mode 100644 index 0000000..5cbe032 --- /dev/null +++ b/BaiDu/ques3.cpp @@ -0,0 +1,135 @@ +// /* + +// */ +// #include +// using namespace std; +// // class T +// // { +// // public: +// // T(int i=0):x(i){ c++;} +// // ~T(){cout<num;//1 +// // cout<*q;//2 +// cout< +#include +#include +using namespace std; +int main() { + int n, x, y, k; + cin >> n >> x >> y >> k; + vector a(n), b(n), c(n); + for (int i = 0; i < n; ++i) cin >> a[i]; + for (int i = 0; i < n; ++i) cin >> b[i]; + for (int i = 0; i < n; ++i) cin >> c[i]; + // dp[i][0]表示第i秒在上路的最大收益 + // dp[i][1]表示第i秒在中路的最大收益 + // dp[i][2]表示第i秒在下路的最大收益 + vector> dp(n + 1, vector(3, 0)); + // 初始状态:第0秒,小明在中路 + dp[0][1] = 0; // 在中路 + for (int i = 1; i <= n; ++i) { + // 在上路的最大收益 + dp[i][0] = max(dp[i-1][0], dp[i-1][1] - x) + a[i-1]; + if (x <= k) { + dp[i][0] = max(dp[i][0], dp[i-1][1] + a[i-1] / 2); + } + // 在中路的最大收益 + dp[i][1] = max({dp[i-1][1], dp[i-1][0] - x, dp[i-1][2] - y}) + b[i-1]; + if (x <= k) { + dp[i][1] = max(dp[i][1], dp[i-1][0] + b[i-1] / 2); + } + if (y <= k) { + dp[i][1] = max(dp[i][1], dp[i-1][2] + b[i-1] / 2); + } + // 在下路的最大收益 + dp[i][2] = max(dp[i-1][2], dp[i-1][1] - y) + c[i-1]; + if (y <= k) { + dp[i][2] = max(dp[i][2], dp[i-1][1] + c[i-1] / 2); + } + } + // 结果是第n秒时,在三条路上获得的最大收益 + long long max_coins = max({dp[n][0], dp[n][1], dp[n][2]}); + cout << max_coins << endl; + return 0; +} +// int main() { +// int n, x, y, k; +// cin >> n >> x >> y >> k; + +// vector a(n), b(n), c(n); +// for (int i = 0; i < n; ++i) cin >> a[i]; +// for (int i = 0; i < n; ++i) cin >> b[i]; +// for (int i = 0; i < n; ++i) cin >> c[i]; + +// // dp[i][0]表示第i秒在上路的最大收益 +// // dp[i][1]表示第i秒在中路的最大收益 +// // dp[i][2]表示第i秒在下路的最大收益 +// vector> dp(n + 1, vector(3, 0)); + +// // 初始状态:第0秒,小明在中路 +// dp[0][1] = 0; // 在中路 + +// for (int i = 1; i <= n; ++i) { +// // 在上路的最大收益 +// dp[i][0] = max(dp[i-1][0], dp[i-1][1] - x) + a[i-1]; +// if (x <= k) { +// dp[i][0] = max(dp[i][0], dp[i-1][1] + a[i-1] / 2); +// } + +// // 在中路的最大收益 +// dp[i][1] = max({dp[i-1][1], dp[i-1][0] - x, dp[i-1][2] - y}) + b[i-1]; +// if (x <= k) { +// dp[i][1] = max(dp[i][1], dp[i-1][0] + b[i-1] / 2); +// } +// if (y <= k) { +// dp[i][1] = max(dp[i][1], dp[i-1][2] + b[i-1] / 2); +// } + +// // 在下路的最大收益 +// dp[i][2] = max(dp[i-1][2], dp[i-1][1] - y) + c[i-1]; +// if (y <= k) { +// dp[i][2] = max(dp[i][2], dp[i-1][1] + c[i-1] / 2); +// } +// } + +// // 结果是第n秒时,在三条路上获得的最大收益 +// long long max_coins = max({dp[n][0], dp[n][1], dp[n][2]}); +// cout << max_coins << endl; + +// return 0; +// } diff --git a/DeWu/question2.cpp b/DeWu/question2.cpp new file mode 100644 index 0000000..3532cf9 --- /dev/null +++ b/DeWu/question2.cpp @@ -0,0 +1,59 @@ +/* +c++实现以下问题: +小红一开始有 s串,s串是空串。她想获得一个字符串 t。 +小红有四种操作: +1.在s串的开头添加一个字符,花费为p。 +2.在s串的末尾添加一个字符,花费为p。 +3.在s串的开头添加一个s串的子串,花费为 q。 +4.在s串的末尾添加一个s串的子串,花费为 q。 +每次操作都是基于当前的s串,有对应的花费。 +小红想知道从s串变到t串的最小花费,你能帮帮她吗? +输入描述: +第一行输入一个字符串t,仅包含小写字符。 +第二行输入两个正整数 p,q。 + +示例 1 +输入 +bbcabc +3 1 +输出 +11 + +说明: +s串先变成"c”,目前花费为3. +s串变成"bc",目前花费为6。 +s串变成”bca”,目前花费为9. +s串变成"bcabc",目前花费为10. +s串变成“bbcabc",目前花费为11. +因此输出11。 +*/ +#include +#include +#include +using namespace std; +int main() { + // 读取输入 + string t; + cin >> t; + int p, q; + cin >> p >> q; + int n = t.size(); + vector dp(n + 1, INT_MAX); // dp数组,初始为最大值 + dp[0] = 0; // 空串到空串的花费为0 + // 进行动态规划计算 + for (int i = 1; i <= n; ++i) { + // 操作1和操作2: 添加一个字符 + dp[i] = dp[i - 1] + p; + // 操作3和操作4: 添加一个子串 + for (int j = 0; j < i; ++j) { + // 如果t的子串t[j:i]可以在s中形成,花费为q + string sub = t.substr(j, i - j); + if (t.substr(0, j).find(sub) != string::npos || t.substr(0, i - sub.size()).find(sub) != string::npos) { + dp[i] = min(dp[i], dp[j] + q); + } + } + } + // 输出最小花费 + cout << dp[n] << endl; + return 0; +} diff --git a/DiDi/ques1.cpp b/DiDi/ques1.cpp deleted file mode 100644 index 36d794e..0000000 --- a/DiDi/ques1.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/* -从数组中选择尽可能多的元素,只能连续选择,使得相加小于m,求最多可以选择几个数。输入为一个个数为n的数组和一个整数,输出为最多选择元素的个数。 -*/ -#include -#include -using namespace std; -int maxConsecutiveElementsUnderSum(const std::vector& nums, int m) { - int n = nums.size(); - int max_count = 0; // 最多选择的元素个数 - int current_sum = 0; // 当前窗口内元素的和 - int left = 0; // 左指针 - // 遍历数组,用右指针表示窗口的右端 - for (int right = 0; right < n; ++right) { - current_sum += nums[right]; // 增加当前右指针对应的元素 - // 当当前窗口和大于 m 时,收缩左边界 - while (current_sum > m && left <= right) { - current_sum -= nums[left]; // 减去左指针的元素 - left++; // 左指针右移 - } - // 更新最多选择的元素个数 - max_count = std::max(max_count, right - left + 1); - } - return max_count; -} - -int main() { - vector nums; - int n,m; - cin>>n>>m; - int val; - for(int i = 0; i < n; i++){ - cin >> val; - nums.push_back(val); - } - int result = maxConsecutiveElementsUnderSum(nums, m); - cout << result << endl; - return 0; -} diff --git a/DiDi/ques2.cpp b/DiDi/ques2.cpp deleted file mode 100644 index 0c3fbbe..0000000 --- a/DiDi/ques2.cpp +++ /dev/null @@ -1,77 +0,0 @@ -/* -小A正在玩游戏,在游戏中一共有n个不同星球,星球间共有m条双向航道,小A的任务是摧毁这些星球。若有多个星球间两两可达,则我们称它们属于同一个联盟。特别的,若一个星球与其他星球间都没有航道,则也称其为一个联盟。小A将按照星球的编号从小到大依次摧毁各个星球,当一个星球被摧毁后,与之相连的航道也将相继被摧毁,现在小A想知道在每个星球被摧毁时还剩下多少个联盟。不同星球间可能有多条航道,但每条航道连接的两个星球必然不同。上述题意可以被简化为,给定n个点,m条边的无向图,按照编号大小依次删去各个节点,请问在每个节点被删去时,还剩下多少个连通块。保证给定图无自环,但可能有重边, - -输入描述 -第一行两个正整数n,m,表示星球数与航道数。接下来m行,每行2个正整数u,v,表示两星球间有一条航道。1 -#define MAXNUM 1000 -int Group[MAXNUM]; //记录图中节点是否被访问过 -int dist[MAXNUM][MAXNUM]; //通过邻接矩阵保存图 -void DFS(int i, int n); -int main() -{ - int n, m, p, q, k = 0; - for (int i = 0; i < MAXNUM; i++) - { - for (int j = 0; j < MAXNUM; j++) - dist[i][j] = 0; //初始化邻接矩阵,初始值为0 - Group[i] = 0; //同上 - } - scanf("%d%d", &n, &m); //获取图的节点数n,和边数m - for (int i = 0; i < m; i++) - { - scanf("%d%d", &p, &q); - dist[p-1][q-1] = 1; //如果节点1和节3相连 则另dist[1][3]和dist[3][1]的值为1 - dist[q-1][p-1] = 1; - } - - for(int zz = 1; zz <= n; zz++){ - for(int j = 1; j <= n; j++){ // 删除节点zz相连的边 - p = zz; - q = j; - dist[p-1][q-1] = 0; - dist[q-1][p-1] = 0; - } - - for (int i = 0; i < MAXNUM; i++) // 清零 - { - Group[i] = 0; - } - - for (int i = 0; i < n; i++) - { - if (Group[i] != 1) - { - DFS(i, n); - k++; //执行一次DFS就是k的值自增1, - } - } - printf("%d", k - zz); // k为删除边但没有删除节点的连通数 zz表示每次删除一个节点 - k = 0; - } - return 0; -} -void DFS(int i,int n) -{ - Group[i] = 1; - for (int j = 0; j < n; j++) - { - if (dist[i][j] == 1) - if (Group[j] != 1) - DFS(j, n); - } -} diff --git a/Djiang/output/question1 copy.exe b/Djiang/output/question1 copy.exe new file mode 100644 index 0000000..a3f5e97 Binary files /dev/null and b/Djiang/output/question1 copy.exe differ diff --git a/Djiang/output/question1.exe b/Djiang/output/question1.exe new file mode 100644 index 0000000..6ff8d36 Binary files /dev/null and b/Djiang/output/question1.exe differ diff --git a/Djiang/output/question3.exe b/Djiang/output/question3.exe new file mode 100644 index 0000000..1c26fc4 Binary files /dev/null and b/Djiang/output/question3.exe differ diff --git a/Djiang/output/question4.exe b/Djiang/output/question4.exe new file mode 100644 index 0000000..433176e Binary files /dev/null and b/Djiang/output/question4.exe differ diff --git a/Djiang/question1.cpp b/Djiang/question1.cpp new file mode 100644 index 0000000..a5af979 --- /dev/null +++ b/Djiang/question1.cpp @@ -0,0 +1,88 @@ +/* +问题描述: +题目描述: +二叉树中的 路径 被定义为一条节点序列,序列中每对相邻节点之间都存在一条边。同一个节点在一条路径序列中 至多出现一次。该路径 至少包含一个节点,目不一定经过根节点。路径和 是路径中各节点值的总和,以先序遍历给定一个二叉树的根节点root,返回其最大路径和。 +输入描述: +输入:[1,2,3,4,5] +输出:11 +*/ +#include +using namespace std; + +#define emp -1 +int maxSum = 0; +struct Node{ + int value; + Node* left; + Node* right; +}; + +Node* createNode(){ + Node* root = new Node(); + root->left = nullptr; + root->right = nullptr; + return root; +} + +void createFullBT_DFS(Node *&root, vector &numbers, int len, int i) { + if(i <= len) { + root->value = numbers[i - 1]; + if(2 * i <= len && numbers[2 * i - 1] != emp) { + root->left = createNode(); + createFullBT_DFS(root->left, numbers, len, 2 * i); + } + if((2 * i + 1) <= len && numbers[2 * i] != emp) { + root->right = createNode(); + createFullBT_DFS(root->right, numbers, len, 2 * i + 1); + } + } +} + +int maxGain(Node* node) { + if (node == nullptr) { + return 0; + } + // 递归计算左右子节点的最大贡献值 + // 只有在最大贡献值大于 0 时,才会选取对应子节点 + int leftGain = max(maxGain(node->left), 0); + int rightGain = max(maxGain(node->right), 0); + // 节点的最大路径和取决于该节点的值与该节点的左右子节点的最大贡献值 + int priceNewpath = node->value + leftGain + rightGain; + // 更新答案 + maxSum = max(maxSum, priceNewpath); + // 返回节点的最大贡献值 + return node->value + max(leftGain, rightGain); +} + +void preOrder(Node *root) { + if(root != NULL) { + cout << root->value << " "; + preOrder(root->left); + preOrder(root->right); + } +} + +int main(){ + string str = "[1,2,3,4,5]"; + string str_update = str.substr(1, str.length()-2); + char* str_input = (char*)str_update.c_str(); + vector nums; + int val = 0; + while(*str_input != '\0'){ + if(*str_input != ','){ + val = val * 10 + (*str_input - '0'); + } + else{ + nums.push_back(val); + val = 0; + } + str_input++; + } + nums.push_back(val); + Node* root = createNode(); + createFullBT_DFS(root, nums, nums.size(), 1); + // preOrder(root); + maxGain(root); + cout << maxSum < +using namespace std; + +class Solution { +public: + string compileSeq(string input) { + //首先完成有向无环图的构建 + //统计图中各节点的个数 并标记头节点为-1 + //使用优先队列 按照先小后大的顺序遍历输出节点值 + int len = input.size(); + /*********构建有向无环图(指向关系)*********/ + map> mp;//first为先 second为后, 也就是second依赖于first + string tmp; + int idx = 0; + for(auto& s:input){ + if(s != ',') + tmp += s; + else{ + mp[stoi(tmp)].push_back(idx++); + string().swap(tmp);//清空string + } + } + if(!tmp.empty()) + mp[stoi(tmp)].push_back(idx++); + + /**********统计各节点个数 并保存头节点***********/ + vector indexcount(len, 0);//统计各节点个数 + priority_queue, greater<>> pq;//保存节点 + for(auto& m:mp){ + if(m.first == -1){ + for(auto& a:m.second){ + pq.push(a); + indexcount[a] = -1; + } + }else{ + for(auto& a:m.second) + ++indexcount[a]; + } + } + + /************根据指向关系遍历图 并按照优先队列输出结果********/ + vector ans; + while(!pq.empty()){ + int node = pq.top(); + pq.pop();//输出了就需要从优先队列中弹出 + ans.push_back(node); + for(auto& m:mp[node]){ + if(--indexcount[m] == 0)//如果该节点是最后一次在图中出现 则放入队列中 + pq.push(m); + } + } + + /***********输出结果************/ + string res; + for(auto& i:ans){ + res += to_string(i); + res.push_back(','); + } + if(!res.empty()) + res.pop_back(); + + return res; + } +}; + +int main(){ + Solution OnecompileSeq; + string res = OnecompileSeq.compileSeq("1,2,-1,1"); + cout << res << endl; + return 0; +} \ No newline at end of file diff --git a/Djiang/question3.cpp b/Djiang/question3.cpp new file mode 100644 index 0000000..0b2c46f --- /dev/null +++ b/Djiang/question3.cpp @@ -0,0 +1,38 @@ +/* +问题描述: +并查集模板 +*/ +#include +using namespace std; + +const int N = 100; // 节点数量 +int f[N]; + +int init() { + // 初始化 + for (int i=0; i +using namespace std; + +class Solution { +public: + vector> getAncestors(int n, vector>& edges) { + vector> anc(n); // 存储每个节点祖先的辅助数组 + vector> e(n); // 邻接表 + vector indeg(n); // 入度表 + // 预处理 + for (const auto& edge: edges) { + e[edge[0]].push_back(edge[1]); + ++indeg[edge[1]]; + } + // 广度优先搜索求解拓扑排序 + queue q; + for (int i = 0; i < n; ++i) { + if (!indeg[i]) { + q.push(i); + } + } + while (!q.empty()) { + int u = q.front(); + q.pop(); + for (int v: e[u]) { + // 更新子节点的祖先哈希表 + anc[v].insert(u); + for (int i: anc[u]) { + anc[v].insert(i); + } + --indeg[v]; + if (!indeg[v]) { + q.push(v); + } + } + } + // 转化为答案数组 + vector> res(n); + for (int i = 0; i < n; ++i) { + for (int j: anc[i]) { + res[i].push_back(j); + } + sort(res[i].begin(), res[i].end()); + } + return res; + } +}; + +int main(){ + Solution test; + int n = 8; + //[[0,3],[0,4],[1,3],[2,4],[2,7],[3,5],[3,6],[3,7],[4,6]] + vector> nums = {{0,3},{0,4},{1,3},{2,4},{2,7},{3,5},{3,6},{3,7},{4,6}}; + vector> res = test.getAncestors(n, nums); + for(int i = 0; i < res.size(); i++){ + for(int j = 0; j < res[i].size(); j++){ + cout << res[i][j]; + } + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/HaiKang/que1.cpp b/HaiKang/que1.cpp new file mode 100644 index 0000000..0cc68f0 --- /dev/null +++ b/HaiKang/que1.cpp @@ -0,0 +1,24 @@ +// #include +// #include +// using namespace std; + +// int main(){ + +// cout<<"asd"< +#include +using namespace std; + +int main(){ + int* a[10] = {0}; + string str = "132456"; + cout< +#include + +struct student +{ + string name; + int old; + char NJ; +}; + + +int main(){ + + return 0; +} \ No newline at end of file diff --git a/HeSai/test2.cpp b/HeSai/test2.cpp new file mode 100644 index 0000000..4eb396b --- /dev/null +++ b/HeSai/test2.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +using namespace std; +struct student +{ + string name; + int old; + int nianji; +}; + +int main(){ + student student1 = {"xiaoming", 10, 2}; + student student2 = {"xiaolan", 12, 4}; + student student3 = {"xiaohong", 12, 8}; + string name; + int old, nianji,j,i; + student studentx; + vector aa; + + aa.push_back(student1); + aa.push_back(student2); + aa.push_back(student3); + + cout<<"please input the student name"<>name; + cout<<"please input the student old"<>old; + cout<<"please input the student nianji"<>nianji; + + studentx.name = name; + studentx.old = old; + studentx.nianji = nianji; + + for(i = 0; i < aa.size(); i++){ + if(studentx.nianji > aa[i].nianji)continue; + student tmp = aa[i]; + aa[i] = studentx; + // cout<<"i = "< i + 1; j--){ + cout<<"1111111111111111111"< +using namespace std; + +int subarraySum(vector& nums, int k) { + unordered_map mp; + mp[0] = 1; + int count = 0, pre = 0; + for (auto& x:nums) { + pre += x; + if (mp.find(pre - k) != mp.end()) { + count += mp[pre - k]; + } + mp[pre]++; + } + return count; +} +int main() { + int n; + cin >> n; + vector nums(n); + for(int i = 0; i < n; i++){ + cin >> nums[i]; + } + int k; + cin >> k; + cout << subarraySum(nums, k) << endl; + return 0; +} diff --git a/HongRuan/question2.cpp b/HongRuan/question2.cpp new file mode 100644 index 0000000..8d6e0ff --- /dev/null +++ b/HongRuan/question2.cpp @@ -0,0 +1,36 @@ +/* +描述:编写一个温控报警程序,根据给定的三个32位寄存器的值reg1、reg2、reg3,以及报警调值 alarm 进行判 +断,其中,reg1的第八位为状态位,状志位置1,表示传感器失效,reg2的0~3位为高位,reg3 的第 24~31位为 +:位。温度寄存器的精度为0.1,范围为[-204, 204],需要计算温度的值,并判断温度是否大于报警调值,如果大于报 +警闯值,则输出tue,否则输出false。状态位置1,直接输出ture +输入:reg1、reg2、reg3 的值(无符号32位整数,16进制表示),以及报警阈值alam(32 位有符号整数,10进 +制表示)。输出:布尔值,表示温度是否大于报警值。 +输入:88 18000A1F 12345678 120 +解释:根据给定的奇存器值,可以提取温度的高位为0x,低位为0x12,计算得到温度值为181.8、由于温度值大于输出:tue报警阈值 120,因此输出 tue. + +输入:88 12000a1a df12fe2d 120解释;根据给定的寄存器值,可以提取温度的高位为0xa,低位为0xd,计算得到温度值为74.3。由于温度值小于编出:false报警國值 120,因此输出false。 +*/ +#include +using namespace std; + +int main() { + unsigned int reg1,reg2,reg3; + cin >> hex >> reg1 >> hex >> reg2 >> hex >> reg3; + int alarm; + cin >> dec >> alarm; + if(reg1 & 0x100){ + cout << "true" << endl; + cout << "111" << endl; + } + else{ + reg2 &= 0x0f; + reg3 &= 0xff000000; + reg3 = reg3 >> 24; + reg2 = reg2 << 8; + unsigned int tmp = (reg2 + reg3); + unsigned int tmp2 = tmp - 0x7f8; + if(tmp2 > alarm*10)cout << "true" << endl; + else cout << "false" << endl;; + } + return 0; +} diff --git a/HongRuan/question2.exe b/HongRuan/question2.exe new file mode 100644 index 0000000..62e940a Binary files /dev/null and b/HongRuan/question2.exe differ diff --git a/HongRuan/question3.cpp b/HongRuan/question3.cpp new file mode 100644 index 0000000..1bb2568 --- /dev/null +++ b/HongRuan/question3.cpp @@ -0,0 +1,56 @@ +/* +验证ipv4是否合法 + +*/ +#include +using namespace std; + +vector split(string s, string spliter) { + vector res; + int i; + //遍历字符串查找spliter + while ((i = s.find(spliter)) && i != s.npos) { + //将分割的部分加入vector中 + res.push_back(s.substr(0, i)); + s = s.substr(i + 1); + } + res.push_back(s); + return res; +} +bool isIPv4(string IP) { + vector s = split(IP, "."); + if (s.size() != 4) + return false; + for (int i = 0; i < s.size(); i++) { + //不可缺省,有一个分割为零,说明两个点相连 + if (s[i].size() == 0) + return false; + //比较数字位数及不为零时不能有前缀零 + if (s[i].size() < 0 || s[i].size() > 3 || (s[i][0] == '0' && s[i].size() != 1)) + return false; + //遍历每个分割字符串,必须为数字 + for (int j = 0; j < s[i].size(); j++) + if (!isdigit(s[i][j])) + return false; + //转化为数字比较,0-255之间 + int num = stoi(s[i]); + if (num < 0 || num > 255) + return false; + } + return true; +} + +int solve(string IP) { + // write code here + if (IP.size() == 0) + return 0; + if (isIPv4(IP)) + return 1; + return 0; +} +int main() { + string str; + cin >> str; + cout << solve(str) << endl; + return 0; +} diff --git a/HongRuan/question3.exe b/HongRuan/question3.exe new file mode 100644 index 0000000..22faf8c Binary files /dev/null and b/HongRuan/question3.exe differ diff --git a/HuaWei/.vscode/settings.json b/HuaWei/.vscode/settings.json new file mode 100644 index 0000000..0c83701 --- /dev/null +++ b/HuaWei/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "iostream": "cpp", + "ostream": "cpp" + } +} \ No newline at end of file diff --git a/HuaWei/output/que.exe b/HuaWei/output/que.exe new file mode 100644 index 0000000..a25aef7 Binary files /dev/null and b/HuaWei/output/que.exe differ diff --git a/HuaWei/output/ques.exe b/HuaWei/output/ques.exe deleted file mode 100644 index cb59e60..0000000 Binary files a/HuaWei/output/ques.exe and /dev/null differ diff --git a/HuaWei/output/ques2.exe b/HuaWei/output/ques2.exe deleted file mode 100644 index b62f89a..0000000 Binary files a/HuaWei/output/ques2.exe and /dev/null differ diff --git a/HuaWei/que.cpp b/HuaWei/que.cpp new file mode 100644 index 0000000..6122356 --- /dev/null +++ b/HuaWei/que.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; + +int main(){ + string str; + int YinWen_num = 0, KongGe_num = 0, ShuZi_num = 0, Others_num = 0; + cout<<"please input str:"<= 0 && str[i] - 'z' <= 0) || (str[i] - 'A' >= 0 && str[i] - 'Z' <= 0))YinWen_num++; + else if(str[i] - '0' >= 0 && str[i] - '9' <= 0)ShuZi_num++; + else if(str[i] == ' ')KongGe_num++; + else Others_num++; + } + cout<<"YinWen_num is:"< +#include +#include + +using namespace std; + +bool canFillBox(int N, const vector& toys, int c) { + // dp[i] indicates if we can fill exactly i capacity with given toys and fillers + vector dp(N + 1, false); + dp[0] = true; + + for (int toy : toys) { + for (int j = N; j >= toy; --j) { + if (dp[j - toy]) { + dp[j] = true; + } + } + } + + // After using all toys, check if we can fill the remaining space with fillers + return dp[N] || (N <= c); +} + +int main() { + int T; + cin >> T; + vector res; + while (T--) { + int N, n, c; + cin >> N >> n >> c; + + vector toys(n); + for (int i = 0; i < n; ++i) { + cin >> toys[i]; + } + + if (canFillBox(N, toys, c)) { + res.push_back("YES"); + } else { + res.push_back("NO"); + } + } + for(int i = 0; i < res.size(); i++){ + cout << res[i] << endl; + } + return 0; +} diff --git a/HuaWei/que2.exe b/HuaWei/que2.exe new file mode 100644 index 0000000..6a05311 Binary files /dev/null and b/HuaWei/que2.exe differ diff --git a/HuaWei/que3.py b/HuaWei/que3.py new file mode 100644 index 0000000..39cfd8e --- /dev/null +++ b/HuaWei/que3.py @@ -0,0 +1,69 @@ +# +# wukun 华为云笔试 + +# 软件安装工具 +# 有一个比较复杂的软件系统需要部署到客户提供的服务器上。该软件系统的安装过程非常繁琐,为了降低操作成本,需要开发一个工具实现自动化部署。软件的安装过程可以分成若干个小步骤,某些步骤间存在依赖关系,被依赖的步骤必须先执行完,才能执行后续的安装步骤。满足依赖条件的多个步骤可以并行执行。请你开发一个调度程序,以最短的时间完成软件的部署。 +# 输入: +# 第一行:总步骤数 N(0 -#include -#include -#include -using namespace std; -const int INF = 1e9; // 定义一个足够大的值表示不可达 -int N, K; -vector> radiation; -// 四个方向的移动 -int dx[] = {1, -1, 0, 0}; -int dy[] = {0, 0, 1, -1}; -// 判断是否可以在指定的防护能力下从 (0,0) 到 (N-1,N-1) -bool canReach(int limit) { - if (radiation[0][0] > limit || radiation[N-1][N-1] > limit) { - return false; - } - vector> visited(N, vector(N, false)); - queue> q; - q.push({0, 0}); - visited[0][0] = true; - int time = 0; - while (!q.empty()) { - int qsize = q.size(); - if (time > K) return false; // 时间超过 K - while (qsize--) { - int x = q.front().first; - int y = q.front().second; - q.pop(); - - if (x == N-1 && y == N-1) { - return true; // 成功到达终点 - } - for (int i = 0; i < 4; ++i) { - int nx = x + dx[i]; - int ny = y + dy[i]; - if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny] && radiation[nx][ny] <= limit) { - visited[nx][ny] = true; - q.push({nx, ny}); - } - } - } - ++time; // 每次扩展一层,时间增加 - } - return false; // 没有在 K 时间内到达终点 -} - -int main() { - // 读入 N 和 K - cin >> N >> K; - radiation.resize(N, vector(N)); - // 读入迷宫的辐射值 - for (int i = 0; i < N; ++i) { - for (int j = 0; j < N; ++j) { - cin >> radiation[i][j]; - } - } - // 二分查找防护能力 - int low = 0, high = 1e4, ans = INF; - while (low <= high) { - int mid = (low + high) / 2; - if (canReach(mid)) { - ans = mid; - high = mid - 1; - } else { - low = mid + 1; - } - } - // 输出结果 - cout << ans << endl; - return 0; -} diff --git a/HuaWei/ques2.cpp b/HuaWei/ques2.cpp deleted file mode 100644 index 9fc7af4..0000000 --- a/HuaWei/ques2.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include -#include -using namespace std; -// 日志文件结构体 -struct LogFile { - int process; // 进程序号 - int size; // 文件大小 - int downloads; // 下载次数 -}; -int main() { - int N, M, C; - cin >> N >> M >> C; - vector> logs(N); // 每个进程的日志文件 - // 读取日志文件信息 - for (int i = 0; i < N * M; ++i) { - int process, size, downloads; - cin >> process >> size >> downloads; - logs[process].push_back({process, size, downloads}); - } - int minCapacity = 0; // 最少需要的容量 - vector mustHaveDownloads; // 每个进程必须要保存的日志文件的下载次数 - vector> optionalFiles; // 额外的日志文件,保存 {文件大小,下载次数} - // 选择每个进程的最小日志文件,并记录额外的文件 - for (int i = 0; i < N; ++i) { - // 找到当前进程中最小的文件 - int minSize = 21, minDownloads = 0; - for (const auto& log : logs[i]) { - if (log.size < minSize) { - minSize = log.size; - minDownloads = log.downloads; - } - } - // 计算最少需要的容量 - minCapacity += minSize; - mustHaveDownloads.push_back(minDownloads); - - // 记录下当前进程中非最小的日志文件,作为可选的日志文件 - for (const auto& log : logs[i]) { - if (log.size != minSize || log.downloads != minDownloads) { - optionalFiles.push_back({log.size, log.downloads}); - } - } - } - // 如果最小容量已经超出了U盘容量,返回-1 - if (minCapacity > C) { - cout << -1 << endl; - return 0; - } - // 计算每个进程必须保存的文件的下载次数之和 - int totalDownloads = 0; - for (int d : mustHaveDownloads) { - totalDownloads += d; - } - // 剩余的U盘容量 - int remainingCapacity = C - minCapacity; - // 动态规划数组,dp[j] 表示容量为 j 时的最大下载次数 - vector dp(remainingCapacity + 1, 0); - // 0-1 背包:选择可选的日志文件 - for (const auto& file : optionalFiles) { - int size = file.first; - int downloads = file.second; - for (int j = remainingCapacity; j >= size; --j) { - dp[j] = max(dp[j], dp[j - size] + downloads); - } - } - // 最大的下载次数等于已选择的最小文件的下载次数 + 背包中选出的最大下载次数 - int maxDownloads = totalDownloads + dp[remainingCapacity]; - // 输出结果 - cout << maxDownloads << endl; - return 0; -} diff --git a/JingWeiHengRun/output/question1.exe b/JingWeiHengRun/output/question1.exe new file mode 100644 index 0000000..9d64a9d Binary files /dev/null and b/JingWeiHengRun/output/question1.exe differ diff --git a/JingWeiHengRun/question1.cpp b/JingWeiHengRun/question1.cpp new file mode 100644 index 0000000..d3ee948 --- /dev/null +++ b/JingWeiHengRun/question1.cpp @@ -0,0 +1,35 @@ +/* +问题描述: +输入一个整数n,求1~n这n个整数的十进制表示中1出现的次数 +例如,1~13中包含1的数字有1、10、11、12、13因此其出现6次 +*/ + +#include +using namespace std; +// 函数用于计算从1到n中'1'出现的次数 +int countDigitOne(int n){ + int count = 0; + long long factor = 1;// 用来表示当前分析的位(个位、十位、百位等) + while(n / factor > 0){ + long long lower= n - (n / factor) * factor;// 当前位以下的数字 + long long current=(n / factor) % 10; + long long higher=n / (factor * 10); + if(current == 0){ + count += higher * factor; + } + else if(current == 1){ + count += higher * factor + lower + 1; + } + else { + count +=(higher + 1) * factor; + } + factor *= 10;// 进入到下一位 + } + return count; +} +int main(){ + int n; + cin >> n; + cout<< countDigitOne(n)< -#include -using namespace std; - -/*杨辉三角最终显示效果是一个等腰三角形,两个最外边都是1 - 杨辉三角的根本在于,每个数等于它上方两数之和 -*/ -int main(){ - int n;//变量n在此处起到了限制输出行数的作用,可优化成用户输入 - cin>>n; - int i,j,a[n][n]; - - //使第一列和对角线元素的值为1 - for (i=0;i +#include +#define MAX_LEN 1000 + +int main() +{ + + char pp[MAX_LEN]; + printf("imput string:"); + scanf("%[^\n]", pp); + printf("ouput string:%s\n", pp); + // printf("%c\n", *(pp+4)); + + int i =0; + for(i = 0; i < MAX_LEN; i++){ + if((*(pp+i) == 'm')||(*(pp+i) == 'M')){ + if((*(pp+i+1) == 't')||(*(pp+i+1) == 'T')){ + *(pp+i) = '$'; + *(pp+i+1) = '$'; + printf("%d\n", i); + } + } + printf("%d\n", i); + if(*(pp+i) == '\0')break; + } + + printf("ouput string:%s\n", pp); + + return 0; +} + +// // #include + +// // #define MAX_LEN 100 // 定义字符串的最大长度 + +// // int main() { +// // char str[MAX_LEN]; +// // printf('请输入一个字符串:'); +// // fgets(str, MAX_LEN, stdin); +// // printf('你输入的字符串是:%s\n', str); +// // return 0; +// // } + + +// #include +// #include + +// #define MAX_LEN 100 // 定义字符串的最大长度 + +// void replaceChar(char *str, char oldChar, char newChar) { +// for (int i = 0; str[i] != '\0'; i++) { +// if (str[i] == oldChar) { +// str[i] = newChar; +// } +// } +// } + +// int main() { +// char str[MAX_LEN]; +// printf("请输入一个字符串:"); +// fgets(str, MAX_LEN, stdin); + +// // 去掉字符串末尾的换行符 +// str[strcspn(str, "\n")] = 0; + +// // 替换字符 'a' 为 '!' +// replaceChar(str, 'm', '!'); + +// printf("替换后的字符串是:%s\n", str); +// return 0; +// } \ No newline at end of file diff --git a/MeiTuan1/question2.cpp b/MeiTuan1/question2.cpp new file mode 100644 index 0000000..4c29e61 --- /dev/null +++ b/MeiTuan1/question2.cpp @@ -0,0 +1,70 @@ +// #include +// #include +// using namespace std; +// int main() { +// string s; +// getline(cin, s); +// for (size_t i = 0; i < s.length() - 1; ++i) { +// if (s[i] == 'm' || s[i] == 'M') { +// if (s[i + 1] == 't' || s[i + 1] == 'T') { +// s.replace(i, 2, "$$"); +// i++; +// } +// } +// } +// cout << s << endl; +// return 0; +// } + +#include +#include +#include + +using namespace std; + +const int N = 1000; +int n; +char str[N][N]; +int m[N][N], s[N][N]; + +int ans[N]; + +int GetPreSub(int x1, int y1, int x2, int y2) { + return s[x2 + 1][y2 + 1] - s[x1][y2 + 1] - s[x2 + 1][y1] + s[x1][y1]; +} + +int main() { + cin >> n; + for (int i = 0; i < n; i++) cin >> str[i]; + //数据存储 + for (int i = 0; i < n; i ++) { + for (int j = 0; j < n; j ++) { + if (str[i][j] == '1') { + m[i + 1][j + 1] = 1; + } else { + m[i + 1][j + 1] = 0; + } + } + } + // 计算前缀和 + for (int i = 1; i <= n; i ++) { + for (int j = 1; j <= n; j ++) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + m[i][j]; + } + } + for (int i = 0; i < n; i ++) { + for (int j = 0; j < n; j ++) { + for (int k = 0; k < n; k ++) { + if(i + k >= n || j + k >= n) { + break; + } + int sub = GetPreSub(i, j, i + k, j + k); + if (sub * 2 == (k + 1) * (k + 1)) { + ans[k] ++; + } + } + } + } + for (int i = 0; i < n; i ++) cout << ans[i] << endl; + return 0; +} \ No newline at end of file diff --git a/MeiTuan1/question2.exe b/MeiTuan1/question2.exe new file mode 100644 index 0000000..9413b83 Binary files /dev/null and b/MeiTuan1/question2.exe differ diff --git a/MeiTuan2/question1.cpp b/MeiTuan2/question1.cpp new file mode 100644 index 0000000..a2cdcf1 --- /dev/null +++ b/MeiTuan2/question1.cpp @@ -0,0 +1,25 @@ +/*第一行输入一个正整数n,代表菜品总数。第二行输入n个正整数ai,代表每道菜的价格。 +第三行输入两个正整数x和y,x代表满减的价格,y代表红包的价格。计算订单总价。 +输入: +4 +10 20 10 20 +25 10 +输出: +25 +*/ +#include +using namespace std; +int main() +{ + int n,a = 0,sum = 0; + int manjian,hongbao; + cin>>n; + for(int i = 0;i < n; i++){ + cin>>a; + sum = sum + a; + } + cin>>manjian; + cin>>hongbao; + cout << sum - manjian - hongbao << endl; + return 0; +} \ No newline at end of file diff --git a/MeiTuan2/question1.exe b/MeiTuan2/question1.exe new file mode 100644 index 0000000..c9019a1 Binary files /dev/null and b/MeiTuan2/question1.exe differ diff --git a/MeiTuan2/question2.cpp b/MeiTuan2/question2.cpp new file mode 100644 index 0000000..0e0fe64 --- /dev/null +++ b/MeiTuan2/question2.cpp @@ -0,0 +1,21 @@ +/*小美定义以下三种单词是合法的:1.所有字母都是小写。例如: good。2.所有字母都是大写。例如:APP。 +3.第一个字母大写,后面所有字母都是小写。例如:Alice。 +现在小美拿到了一个单词,她每次操作可以修改任意一个字符的大小写。小美想知道最少操作几次可以使得单词变成合法的? +*/ +#include +#include +using namespace std; +int main() { + string s; + getline(cin, s); + + int l = 0, h = 0; + for(int i = 0; i < s.size(); i++){ + if(s[i] - 'a' >= 0)l++; + else h++; + } + int opt = min(l,h); + if((s[0] - 'a')<0)opt = min(opt,h - 1); + cout< +#include +using namespace std; + +int main() { + int n; + int val,q,xi; + long sum=0; + const int MOD = 1e9 + 7; + cin >> n; + cin>>q; + vector aa; + vector bb; + for(int i = 0;i < n; i++){ + cin>>val; + aa.push_back(val); + bb.push_back(val); + } + for(int i = 0; i < q; i++){ + cin>>xi; + xi--; + for(int j = 0; j < n; j++){ + if(j == xi)continue; + else{ + bb[j] = bb[j] * 2 % MOD; + } + } + } + for(int i = 0; i < bb.size(); i++)sum+=bb[i]; + std::cout< +#include +#include +typedef long long LL; +const int N = 200010; +int n; +int a[N], tr[N * 2]; +int sum[N]; +int lowbit(int x) { + return x & -x; +} +void modify(int x, int k) { + for (int i = x; i <= 2 * n + 5; i += lowbit(i)) tr[i] += k; +} +int query(int x) { + int res = 0; + for (int i = x; i; i -= lowbit(i)) res += tr[i]; + return res; +} +int main() { + scanf("%d", &n); + for (int i = 1; i <= n; i++) scanf("%d", &a[i]); + LL ans = 0; + modify(0 + n + 1, 1); + for (int i = 1; i <= n; i++) { + sum[i] = sum[i - 1]; + if (a[i] == 1) sum[i] += 1; + else sum[i] -= 1; + ans += query(sum[i] + n + 1) + (i - query(sum[i] + n + 1)) * 2; + modify(sum[i] + n + 1, 1); + } + printf("%lld", ans); + return 0; +} \ No newline at end of file diff --git a/MeiTuan2/question4.exe b/MeiTuan2/question4.exe new file mode 100644 index 0000000..f084721 Binary files /dev/null and b/MeiTuan2/question4.exe differ diff --git a/MeiTuan2/question5.cpp b/MeiTuan2/question5.cpp new file mode 100644 index 0000000..72f6fed --- /dev/null +++ b/MeiTuan2/question5.cpp @@ -0,0 +1,66 @@ +#include +#include +using namespace std; +const int N=200000; +void merge__(vector& arr, vector& tmp, int l, int mid, int r, int& ret) { + int i = l, j = mid + 1, k = 0; + + while (i <= mid && j <= r) { + if (arr[i] > arr[j]) { + tmp[k++] = arr[j++]; + ret += (mid - i + 1); + } + else { + tmp[k++] = arr[i++]; + } + } + while (i <= mid) { + tmp[k++] = arr[i++]; + } + while (j <= r) { + tmp[k++] = arr[j++]; + } + + for (k = 0, i = l; i <= r; ++i, ++k) { + arr[i] = tmp[k]; + } +} +void merge_sort__(vector& arr, vector& tmp, int l, int r, int& ret) { + if (l >= r) { + return; + } + + int mid = l + ((r - l) >> 1); + merge_sort__(arr, tmp, l, mid, ret); + merge_sort__(arr, tmp, mid + 1, r, ret); + merge__(arr, tmp, l, mid, r, ret); +} + +int InversePairs(vector& nums) { + // write code here + int ret = 0; + // 在最外层开辟数组 + vector tmp(nums.size()); + merge_sort__(nums, tmp, 0, nums.size() - 1, ret); + return ret; +} + +int main() +{ + vector q; + int n,a; + cin >> n; + int result; + for(int i=0;i>a; + q.push_back(a); + } + for(int i=0;i +using namespace std; +/* + *暴力法:每个柱子顶部可以储水的高度为:该柱子的左右两侧最大高度的较小者减去此柱子的高度。因此我们只需要遍历每个柱子,累加每个柱子可以储水的高度即可。 +*/ +int JieYuShui1(int height[], int len){ + int res = 0; + // 遍历每个柱子 + for (int i = 1; i < len - 1; i++) { + int leftMax = 0, rightMax = 0; + // 计算当前柱子左侧的柱子中的最大高度 + for (int j = 0; j <= i; j++) { + leftMax = max(leftMax, height[j]); + } + // 计算当前柱子右侧的柱子中的最大高度 + for (int j = i; j < len; j++) { + rightMax = max(rightMax, height[j]); + } + // 结果中累加当前柱子顶部可以储水的高度, + // 即 当前柱子左右两边最大高度的较小者 - 当前柱子的高度。 + res += min(leftMax, rightMax) - height[i]; + } + return res; +} + +/* + *动态规划:对于每个柱子,我们都需要从两头重新遍历一遍求出左右两侧的最大高度,这里是有很多重复计算的,很明显最大高度是可以记忆化的,具体在这里可以用数组边递推边存储,也就是常说的动态规划,DP。 + 1定义二维dp数组 int[][] dp = new int[n][2],其中,dp[i][0] 表示下标i的柱子左边的最大值,dp[i][1] 表示下标i的柱子右边的最大值。2分别从两头遍历height数组,为 dp[i][0]和 dp[i][1] 赋值。3同方法1,遍历每个柱子,累加每个柱子可以储水的高度。 +*/ +int JieYuShui2(int height[], int len){ + int res = 0; + if (len == 0) { + return 0; + } + // 定义二维dp数组 + // dp[i][0] 表示下标i的柱子左边的最大值 + // dp[i][1] 表示下标i的柱子右边的最大值 + + int dp[len][2]; + dp[0][0] = height[0]; + dp[len - 1][1] = height[len - 1]; + for (int i = 1; i < len; i++) { + dp[i][0] = max(height[i], dp[i - 1][0]); + } + for (int i = len - 2; i >= 0; i--) { + dp[i][1] = max(height[i], dp[i + 1][1]); + } + // 遍历每个柱子,累加当前柱子顶部可以储水的高度, + // 即 当前柱子左右两边最大高度的较小者 - 当前柱子的高度。 + for (int i = 1; i < len - 1; i++) { + res += min(dp[i][0], dp[i][1]) - height[i]; + } + return res; +} + +/* + *双指针:在上述的动态规划方法中,我们用二维数组来存储每个柱子左右两侧的最大高度,但我们递推累加每个柱子的储水高度时其实只用到了 dp[i][0]和 dp[i][1] 两个值,因此我们递推的时候只需要用 int leftMax 和 int rightMax 两个变量就行了。 +*/ +int JieYuShui3(int height[], int len){ + int res = 0; + int leftMax = 0, rightMax = 0, left = 0, right = len - 1; + while (left <= right) { + if (leftMax <= rightMax) { + leftMax = max(leftMax, height[left]); + res += leftMax - height[left++]; + } else { + rightMax = max(rightMax, height[right]); + res += rightMax - height[right--]; + } + } + return res; +} + +int main(){ + int arr[] = {0,1,0,2,1,0,1,3,2,1,2,1}; + int len = sizeof(arr) / sizeof(arr[0]); + int res = JieYuShui3(arr, len); + cout< +using namespace std; +/* + *暴力法:每个柱子顶部可以储水的高度为:该柱子的左右两侧最大高度的较小者减去此柱子的高度。因此我们只需要遍历每个柱子,累加每个柱子可以储水的高度即可。 +*/ +int JieYuShui1(int height[], int len){ + int res = 0; + // 遍历每个柱子 + for (int i = 1; i < len - 1; i++) { + int leftMax = 0, rightMax = 0; + // 计算当前柱子左侧的柱子中的最大高度 + for (int j = 0; j <= i; j++) { + leftMax = max(leftMax, height[j]); + } + // 计算当前柱子右侧的柱子中的最大高度 + for (int j = i; j < len; j++) { + rightMax = max(rightMax, height[j]); + } + // 结果中累加当前柱子顶部可以储水的高度, + // 即 当前柱子左右两边最大高度的较小者 - 当前柱子的高度。 + res += min(leftMax, rightMax) - height[i]; + } + return res; +} + +/* + *动态规划:对于每个柱子,我们都需要从两头重新遍历一遍求出左右两侧的最大高度,这里是有很多重复计算的,很明显最大高度是可以记忆化的,具体在这里可以用数组边递推边存储,也就是常说的动态规划,DP。 + 1定义二维dp数组 int[][] dp = new int[n][2],其中,dp[i][0] 表示下标i的柱子左边的最大值,dp[i][1] 表示下标i的柱子右边的最大值。2分别从两头遍历height数组,为 dp[i][0]和 dp[i][1] 赋值。3同方法1,遍历每个柱子,累加每个柱子可以储水的高度。 +*/ +int JieYuShui2(int height[], int len){ + int res = 0; + if (len == 0) { + return 0; + } + // 定义二维dp数组 + // dp[i][0] 表示下标i的柱子左边的最大值 + // dp[i][1] 表示下标i的柱子右边的最大值 + + int dp[len][2]; + dp[0][0] = height[0]; + dp[len - 1][1] = height[len - 1]; + for (int i = 1; i < len; i++) { + dp[i][0] = max(height[i], dp[i - 1][0]); + } + for (int i = len - 2; i >= 0; i--) { + dp[i][1] = max(height[i], dp[i + 1][1]); + } + // 遍历每个柱子,累加当前柱子顶部可以储水的高度, + // 即 当前柱子左右两边最大高度的较小者 - 当前柱子的高度。 + for (int i = 1; i < len - 1; i++) { + res += min(dp[i][0], dp[i][1]) - height[i]; + } + return res; +} + +/* + *双指针:在上述的动态规划方法中,我们用二维数组来存储每个柱子左右两侧的最大高度,但我们递推累加每个柱子的储水高度时其实只用到了 dp[i][0]和 dp[i][1] 两个值,因此我们递推的时候只需要用 int leftMax 和 int rightMax 两个变量就行了。 +*/ +int JieYuShui3(int height[], int len){ + int res = 0; + int leftMax = 0, rightMax = 0, left = 0, right = len - 1; + while (left <= right) { + if (leftMax <= rightMax) { + leftMax = max(leftMax, height[left]); + res += leftMax - height[left++]; + } else { + rightMax = max(rightMax, height[right]); + res += rightMax - height[right--]; + } + } + return res; +} + +int main(){ + int arr[] = {0,1,0,2,1,0,1,3,2,1,2,1}; + int len = sizeof(arr) / sizeof(arr[0]); + int res = JieYuShui3(arr, len); + cout< +#include +#include +#include +using namespace std; + +int main() { + int T; + cin >> T; // 读取测试数据组数 + while (T--) { + long long n, k, x; + cin >> n >> k >> x; + vector a(n); + for (int i = 0; i < n; ++i) { + cin >> a[i]; + } + // 动态规划数组dp[i]表示从i往后考虑的最小花费,最后最小花费就是dp[0]或者直接删除后续所有元素 + vector dp(n + 1, LLONG_MAX); + dp[n] = 0; + int suffix_mex = 0; + set vst; + for (int i = n-1; i >= 0; --i) { + vst.insert(a[i]); + while(vst.count(suffix_mex)){ + suffix_mex++; + } + dp[i] = min(dp[i + 1] + x, k * suffix_mex); + } + // 输出最小花费 + cout << dp[0] << endl; + } + return 0; +} \ No newline at end of file diff --git a/MeiTuan5/ques.c b/MeiTuan5/ques.c new file mode 100644 index 0000000..d269c71 --- /dev/null +++ b/MeiTuan5/ques.c @@ -0,0 +1,27 @@ +/* +看代码有没有错误或隐患 +*/ + + +#include +#include +#include +void get2(char *p){ + // char *aa; + // memcpy(aa, "bbb", 3); + // p = aa; + // p = (char *)malloc(10); + *p = 'c'; +} +int main(){ + char *m; + memcpy(m, "aaa", 3); + get2(m); + + for(int i = 0; i < 300; i++){ + char tmp = *(m + i); + if(tmp == '\0')break; + printf("%c", *(m + i)); + } + return 0; +} \ No newline at end of file diff --git a/MeiTuan5/ques.exe b/MeiTuan5/ques.exe new file mode 100644 index 0000000..d4f866e Binary files /dev/null and b/MeiTuan5/ques.exe differ diff --git a/PinDD/test.cpp b/PinDD/test.cpp new file mode 100644 index 0000000..c2c4898 --- /dev/null +++ b/PinDD/test.cpp @@ -0,0 +1,68 @@ +/* +多多有一个长度为n的数组,依次采用如下策略对数组从小到大进行排序 +1.将数组划分为k个非空子串(每个子串为原数组中连续数字组成的子序列) +2.调整k个子串的顺序,每个子串内元素顺序不变 +3.将k个子串首尾相连,组成一个新的数组 + +以上策略不保证最终得到的数组有(单调递增)请你计算一下,对于给定的数组与划分数k,能否最终得到一个有序递增的序列 + +输入描述: +第一行一个数字T(T < 5),代表测试用例个数 +对于每组测试用例:第一行两个数字:n,k,代表数组长度与划分子串个数,0 +using namespace std; +int main(){ + ios::sync_with_stdio(false); + cin.tie(0); + int T; + cin >> T; + while(T--){ + long long n, k; + cin >> n >> k; + vector arr(n); + for(auto &x : arr)cin >> x; + //Create a vector of pairs(value, original index) + vector> sorted_arr(n); + for(long long i = 0; i < n; i++){ + sorted_arr[i] = {arr[i], i+1};//1-based indexing + } + // Sort the array based on values + sort(sorted_arr.begin(), sorted_arr.end()); + // Count the number of blocks where indices are consecutive + long long m = 1;// At least one block + for(long long i = 1; i < n; i++){ + if(sorted_arr[i].second != sorted_arr[i-1].second +1){ + m++; + } + } + // If number of blocks <=k, it's possible + if(m <= k){ + cout <<"True\n"; + } + else{ + cout<<"False\n"; + } + } +} \ No newline at end of file diff --git a/PinDD/test.exe b/PinDD/test.exe new file mode 100644 index 0000000..a1dcfba Binary files /dev/null and b/PinDD/test.exe differ diff --git a/XiaoHongShu/output/ques.exe b/XiaoHongShu/output/ques.exe deleted file mode 100644 index a6c35a2..0000000 Binary files a/XiaoHongShu/output/ques.exe and /dev/null differ diff --git a/XiaoHongShu/ques.cpp b/XiaoHongShu/ques.cpp deleted file mode 100644 index 295a5a4..0000000 --- a/XiaoHongShu/ques.cpp +++ /dev/null @@ -1,92 +0,0 @@ -/* -小红书最近决定举办一场盛大的红蓝PK!为了公平的分出红蓝两个阵营,每个人都获得了一个数字ai代表目己的战斗力,随后,由小红书“抽奖组件”这个程序来自动划分阵营,使得红方队员的总战斗力等于蓝方的总战斗力 -然而,并不是每一局游戏都能找到这样完美的分配方案,已有的程序显然已经不够用了。让我们来新增一个总战斗力。前置程序吧,你只需要计算出每一局双方战斗力差值的最小绝对值。 -*/ -#include -#include -#include -#include -using namespace std; - -int findClosestSum(vector& nums, int target) { - int sum = accumulate(nums.begin(), nums.end(), 0); // 数组元素的总和 - int maxSum = min(sum, target + abs(sum - target)); // 限制最大的可能和 - vector dp(maxSum + 1, false); // dp数组,初始为 false - dp[0] = true; // 和为 0 是可能的 - // 动态规划填充 dp 数组 - for (int num : nums) { - for (int i = maxSum; i >= num; --i) { - dp[i] = dp[i] || dp[i - num]; - } - } - // 从 dp 数组中找到最接近目标值的和 - int closest = 0; - for (int i = 0; i <= maxSum; ++i) { - if (dp[i] && abs(i - target) < abs(closest - target)) { - closest = i; - } - } - return closest; -} - -int main() { - int n; - vector arr; - int val, sum = 0; - cin >> n; - for(int i = 0; i < n; i++){ - cin >> val; - sum += val; - arr.push_back(val); - } - int arv = sum / 2; - int tmp = findClosestSum(arr, arv); - cout << tmp << endl; - cout << arv - tmp + sum - tmp - arv << endl; - return 0; -} - - -#include -#include -#include -#include - -using namespace std; - -int findClosestSum(vector& nums, int target) { - int n = nums.size(); - int sum = accumulate(nums.begin(), nums.end(), 0); // 数组元素的总和 - - int maxSum = min(sum, target + abs(sum - target)); // 限制最大的可能和 - vector dp(maxSum + 1, false); // dp数组,初始为 false - dp[0] = true; // 和为 0 是可能的 - - // 动态规划填充 dp 数组 - for (int num : nums) { - for (int i = maxSum; i >= num; --i) { - dp[i] = dp[i] || dp[i - num]; - } - } - - // 从 dp 数组中找到最接近目标值的和 - int closest = 0; - for (int i = 0; i <= maxSum; ++i) { - if (dp[i] && abs(i - target) < abs(closest - target)) { - closest = i; - } - } - - return closest; -} - -int main() { - vector nums = {7,3,5,3}; // 输入数组 - int target = 10; // 目标值 - - int result = findClosestSum(nums, target); - - cout << result << endl; - - return 0; -} diff --git a/XiaoMi/ques1.cpp b/XiaoMi/ques1.cpp new file mode 100644 index 0000000..2875e1c --- /dev/null +++ b/XiaoMi/ques1.cpp @@ -0,0 +1,57 @@ +/* +小米定义了一种特殊的字符串,规则如下: +1.字符串必须是一个对称字符串,如:aaabbbaaa +2.字符串中第1、2、3个字符一样,第4、5、6个字符一样,依次类推 +3.字符串长度为奇数 + +输入: +3 +aaabbbaaa +aabbaa +aaa + +输出: +yes +no +yes +*/ + +#include +#include +#include +bool isSymmetric(const char* str){ + int n=strlen(str); + for(int i=0;i= n)break; + if(str[i]!=str[i +1] || str[i]!= str[i + 2])return false; + } + return true; +} +bool isValidstring(const char* str){ + if(strlen(str)%2==0)return false; + if(!isSymmetric(str))return false; + if(!isGrouped(str))return false; + return true; +} +int main(){ + int n; + scanf("%d", &n); + char inputs[n][101]; + for(int i = 0; i < n; ++i)scanf("%s",inputs[i]); + for (int i = 0; i < n; ++i){ + if(isValidstring(inputs[i])){ + printf("yes\n"); + } + else { + printf("no\n"); + } + } + return 0; +} diff --git a/XiaoMi/ques2.cpp b/XiaoMi/ques2.cpp new file mode 100644 index 0000000..3a45917 --- /dev/null +++ b/XiaoMi/ques2.cpp @@ -0,0 +1,42 @@ +/* +小米共有m个箱子用于打包行李,每个箱子承重为w。现有n件行李排成一行,重量为ai, +小米将从左向右开始打包。如果当前箱子装入该行李不会超重则优先装在当前箱子, +如果会超重则将当前箱子密封,将该行李装入下一个新的箱子中。小米更偏爱放在右边的行李, +所以为了能装入更多行李,他会选择从最左边开始连续地舍弃一些行李,以保证剩下的行李可以全部装完。 +现在想知道,他最多可以装入多少件行李? + +输入: +5 2 8 +6 3 2 5 3 + +输出: +4 +*/ + +#include +int main(){ + int n, m; + long long w; + scanf("%d %d %lld",&n,&m, &w); + long long weights[n]; + for(int i=0;i=0){ + if(currentweight + weights[rightIndex]<=w){ + currentweight += weights[rightIndex]; + } else { + boxcount++; + currentweight = weights[rightIndex]; + } + rightIndex--; + if(boxcount>m){ + rightIndex++; + break; + } + } + int itemsPacked=n - (rightIndex + 1); + printf("%d\n",itemsPacked); + return 0; +} \ No newline at end of file diff --git a/XiaoMi/ques2.exe b/XiaoMi/ques2.exe new file mode 100644 index 0000000..07a9359 Binary files /dev/null and b/XiaoMi/ques2.exe differ diff --git a/all/all.c b/all/all.c new file mode 100644 index 0000000..e125fc0 --- /dev/null +++ b/all/all.c @@ -0,0 +1,153 @@ +#include + +// 定义链表 +struct Node +{ + int data; + struct Node* next; +}; +struct Node* head; + +// 在链表头节点插入一个值为x的节点 +void Insert(int x){ + struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); + temp->data = x; + temp->next = head; + // if(head != NULL)temp->next = head; + head = temp; +} + +// 迭代法实现反转列表 +struct Node* Reverse1(struct Node* head){ + struct Node *Current,*Prev,*next; + Current = head; + Prev = NULL; + while(Current != NULL){ + next = Current->next; + Current->next = Prev; + Prev = Current; + Current = next; + } + head = Prev; + return head; +} + +// 递归法实现反转列表 +void Reverse2(struct Node* p){ + if(p->next ==NULL){ + head = p; + return; + } + Reverse2(p->next); + struct Node* q = p->next; + q->next = p; + p->next = NULL; +} + +// 链表内指定区间反转 调试好了 +struct Node* reverseBetween(struct Node* head, int m, int n ) { + // write code here + if(head == NULL)return head; + if(head->next == NULL)return head; + if(m == n)return head; + struct Node *Current,*Prev,*next,*start,*start_last; + int i; + Current = head; + Prev = NULL; + next = NULL; + // 先找到开始位置 + for (i=1; inext; + // Current->next = Prev; + Prev = Current; + Current = next; + } + // 标记 + start_last = Prev; + start = Current; + // 反转 + for (i=0; i<(n-m+1); i++) { + next = Current->next; + Current->next = Prev; + Prev = Current; + Current = next; + } + // 头尾节点重指向 + if(start != head){ + start->next = next; + start_last->next = Prev;//start!=head的情况下,需要保留start上一个指针 + } + else { + start->next = next; + head = Prev;//start==head的情况下,直接将head指向待反转的最后一个 + } + return head; +} + +//打印链表的所有值 +void Print(){ + struct Node* temp = head; + printf("List is:"); + while(temp){ + printf("%d",temp->data); + temp = temp->next; + } + printf("\n"); +} + +void Print2(struct Node*p){ + if(p ==NULL){ + printf("\n"); + return; + } + // 正序打印 + // printf("%d",p->data); + // Print2(p->next); + + // 反转打印 + printf("%d",p->data); + Print2(p->next); +} + +//计算数组的大小,传递数组其实传的地址会改变原来的值 +int RetureLen(int array[], int size){ + array[0] = 0; + return size; +} + +int main(){ + int a; + char b; + char str[10] = "zzwxxy"; + char str2[10]; + int array[] = {1,2,3,4,5,6,7,8,9}; + int len = RetureLen(array, sizeof(array) / sizeof(array[0])); + printf("%d", array[0]); + + // printf("please input string\n"); + // gets(str2); + // printf("input string is:\n%s", str2); +} + + +/* + * 定义字符串方式 +char string[] = "zhang"; +char string[] = {'z','h','a','n','g'}; +char str[] = {“zhang”}; +*/ + +/* +scanf和gets的区别: +使用方法:scanf("%s", str2); gets(str2); +scanf :当遇到回车,空格和tab键会自动在字符串后面添加’\0’,但是回车,空格和tab键仍会留在输入的缓冲区中。 +gets:可接受回车键之前输入的所有字符,并用’\0’替代 ‘\n’.回车键不会留在输入缓冲区中 + +printf()和puts()的区别: +printf("input string is:\n%s", str2); 和 puts(str2); +*/ + +/* +计算数组长度:int length = sizeof(array) / sizeof(array[0]); +如果数组是字符串的话:#include int length = strlen(strArray); +*/ \ No newline at end of file diff --git a/all/all.cpp b/all/all.cpp new file mode 100644 index 0000000..46fe69b --- /dev/null +++ b/all/all.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +using namespace std; // 为了使用cin和cout + + +/* +#include +unordered_set st; // 哈希集合 +st.insert(pHead1);//插入值 +st.count(pHead2);//查看有没有某个值 +// 迭代输出 +unordered_set::iterator itr; +for (itr = st.begin(); itr != st.end(); itr++) + cout << (*itr) << ' '; //fast is code c++ in +st.count(pHead1);//计算特定元素出现的次数 +st.empty();//是否为空 +*/ + +/* +#include +unordered_map mp; //哈希表 +mp.find(depth);//查找某个key +map.count(key);//查看出现的次数 +mp.erase(key);//移除某个元素 +*/ + +/* +#include +stack q; //以int型为例 +int x; +q.push(x); //将x压入栈顶 +q.top(); //返回栈顶的元素 +q.pop(); //删除栈顶的元素 +q.size(); //返回栈中元素的个数 +q.empty(); //检查栈是否为空,若为空返回true,否则返回false +*/ + +// 定义链表 +struct ListNode +{ + int a; + string name; + char c; + ListNode* next; +}; + +ListNode *head; + +int main(){ + + int a; + char b; + char c[8]; + string d; + cout<<"please input string"< 的作用: +可以使用string定义字符串 +可以使用strlen获得字符串(字符串需要使用char s[10])定义的长度 +int strcmp(const char *s1, const char *s2);当s1s2时,返回正数。 +*/ + +/* +cin和getline的区别: +cin>>str不会读取空格; getline(cin, str); 可以读取空格,但是需要#include 并且str需要通过 string定义; +*/ \ No newline at end of file diff --git a/all/all.exe b/all/all.exe new file mode 100644 index 0000000..9942122 Binary files /dev/null and b/all/all.exe differ diff --git a/all/output/all.exe b/all/output/all.exe new file mode 100644 index 0000000..b5f9866 Binary files /dev/null and b/all/output/all.exe differ diff --git a/all/sort.c b/all/sort.c new file mode 100644 index 0000000..c83f426 --- /dev/null +++ b/all/sort.c @@ -0,0 +1,266 @@ +#include + +// 1.插入排序,时间复杂度O(n2),空间复杂度O(1) +void InsertSort(int a[], int n) +{ + for (int i = 1; i < n; i++) + { + // 若第i个元素大于i-1元素,直接插入。小于的话,移动有序表后插入 + if (a[i] < a[i - 1]) + { + int j = i - 1; + // 复制为哨兵,即存储待排序元素 + int x = a[i]; + // 先后移一个元素 + a[i] = a[i - 1]; + // 查找在有序表的插入位置 + while (x < a[j]) + { + a[j + 1] = a[j]; + // 元素后移 + j--; + } + // 插入到正确位置 + a[j + 1] = x; + } + } +} + +// 2.希尔排序,时间复杂度O(N^1.5),空间复杂度O(1) +void ShellInsertSort(int a[], int n, int dk) +{ + for (int i = dk; i < n; ++i) + { + if (a[i] < a[i - dk]) + { // 若第i个元素大于i-1元素,直接插入。小于的话,移动有序表后插入 + int j = i - dk; + int x = a[i]; // 复制为哨兵,即存储待排序元素 + a[i] = a[i - dk]; // 首先后移一个元素 + while (j >= 0 && x < a[j]) + { // 查找在有序表的插入位置 + a[j + dk] = a[j]; + j -= dk; // 元素后移 + } + a[j + dk] = x; // 插入到正确位置 + } + } +} +void ShellSort(int a[], int n) +{ + + int dk = n / 2; + while (dk >= 1) + { + ShellInsertSort(a, n, dk); + dk = dk / 2; + } +} + +// 选择排序,时间复杂度:O(N^2), 空间复杂度:O(1) +int SelectMinKey(int a[], int n, int i) +{ + int k = i; + for (int j = i + 1; j < n; ++j) + { + if (a[k] > a[j]) + k = j; + } + return k; +} +void SelectSort(int a[], int n) +{ + int key, tmp; + for (int i = 0; i < n - 1; ++i) + { + key = SelectMinKey(a, n, i); // 选择最小的元素 + if (key != i) + { + tmp = a[i]; + a[i] = a[key]; + a[key] = tmp; // 最小元素与第i位置元素互换 + } + } +} + +// 堆排序,时间复杂度:O(N*logN),空间复杂度:O(1) +void HeapAdjust(int H[], int s, int length) +{ + int tmp = H[s]; + int child = 2 * s + 1; // 左孩子结点的位置。(i+1 为当前调整结点的右孩子结点的位置) + while (child < length) + { + if (child + 1 < length && H[child] < H[child + 1]) + { // 如果右孩子大于左孩子(找到比当前待调整结点大的孩子结点) + ++child; + } + if (H[s] < H[child]) + { // 如果较大的子结点大于父结点 + H[s] = H[child]; // 那么把较大的子结点往上移动,替换它的父结点 + s = child; // 重新设置s ,即待调整的下一个结点的位置 + child = 2 * s + 1; + } + else + { // 如果当前待调整结点大于它的左右孩子,则不需要调整,直接退出 + break; + } + H[s] = tmp; // 当前待调整的结点放到比其大的孩子结点位置上 + } +} + +/** + * 初始堆进行调整 + * 将H[0..length-1]建成堆 + * 调整完之后第一个元素是序列的最小的元素 + */ +void BuildingHeap(int H[], int length) +{ + // 最后一个有孩子的节点的位置 i= (length -1) / 2 + for (int i = (length - 1) / 2; i >= 0; --i) + HeapAdjust(H, i, length); +} +/** + * 堆排序算法 + */ +void HeapSort(int H[], int length) +{ + // 初始堆 + BuildingHeap(H, length); + // 从最后一个元素开始对序列进行调整 + for (int i = length - 1; i > 0; --i) + { + // 交换堆顶元素H[0]和堆中最后一个元素 + int temp = H[i]; + H[i] = H[0]; + H[0] = temp; + // 每次交换堆顶元素和堆中最后一个元素之后,都要对堆进行调整 + HeapAdjust(H, 0, i); + } +} + +// 冒泡排序,时间复杂度:O(N^2),空间复杂度:O(1) +void BubbleSort(int r[], int n) +{ + int low = 0; + int high = n - 1; // 设置变量的初始值 + int tmp, j; + while (low < high) + { + for (j = low; j < high; ++j) // 正向冒泡,找到最大者 + if (r[j] > r[j + 1]) + { + tmp = r[j]; + r[j] = r[j + 1]; + r[j + 1] = tmp; + } + --high; // 修改high值, 前移一位 + for (j = high; j > low; --j) // 反向冒泡,找到最小者 + if (r[j] < r[j - 1]) + { + tmp = r[j]; + r[j] = r[j - 1]; + r[j - 1] = tmp; + } + ++low; // 修改low值,后移一位 + } +} + +// 快速排序递归实现,时间复杂度:O(N*logN), 空间复杂度:O(logN) +int QuickSort(int *a, int low, int high) +{ + int i = low; // 第一位 + int j = high; // 最后一位 + int key = a[i]; // 将第一个数作为基准值-- 先找到一个基准值 + + while (i < j) + { + while (i < j && a[j] >= key) + { + j--; + } + a[i] = a[j]; + + while (i < j && a[i] <= key) + { + i++; + } + a[j] = a[i]; + } + a[i] = key; + if (i - 1 > low) + { + QuickSort(a, low, i - 1); + } + + if (i + 1 < high) + { + QuickSort(a, i + 1, high); + } + + return 0; +} + +// 归并排序迭代实现,时间复杂度:O(N*logN), 空间复杂度:O(N) +void merge(int arr[], int start, int mid, int end, int len) +{ + int result[len]; + int k = 0; + int i = start; + int j = mid + 1; + while (i <= mid && j <= end) + { + if (arr[i] < arr[j]) + { + result[k++] = arr[i++]; + } + else + { + result[k++] = arr[j++]; + } + } + if (i == mid + 1) + { + while (j <= end) + result[k++] = arr[j++]; + } + if (j == end + 1) + { + while (i <= mid) + result[k++] = arr[i++]; + } + for (j = 0, i = start; j < k; i++, j++) + { + arr[i] = result[j]; + } +} + +void MergeSort(int arr[], int start, int end, int len) +{ + if (start >= end) + return; + int mid = (start + end) / 2; + MergeSort(arr, start, mid, len); + MergeSort(arr, mid + 1, end, len); + merge(arr, start, mid, end, len); +} + +int main() +{ + int i; + int array[] = {9, 5, 6, 1, 4, 7, 3, 8, 2}; + int array2[9]; + // InsertSort(array, sizeof(array)/sizeof(array[0])); + // ShellSort(array, sizeof(array)/sizeof(array[0])); + // SelectSort(array, sizeof(array)/sizeof(array[0])); + // HeapSort(array, sizeof(array)/sizeof(array[0])); + // BubbleSort(array, sizeof(array)/sizeof(array[0])); + // QuickSort(array, 0, 9-1); + int len = sizeof(array) / sizeof(array[0]); + MergeSort(array, 0, 9 - 1, len); + + printf("sort result is:"); + for (i = 0; i < 9; i++) + { + printf("%d", array[i]); + } + return 0; +} diff --git a/all/sort.exe b/all/sort.exe new file mode 100644 index 0000000..0ade5f9 Binary files /dev/null and b/all/sort.exe differ diff --git a/all/sort2.cpp b/all/sort2.cpp new file mode 100644 index 0000000..4a30fa2 --- /dev/null +++ b/all/sort2.cpp @@ -0,0 +1,303 @@ +#include +using namespace std; + +int len = 10; +int a[10] = {5,7,2,6,4,1,3,9,8,10}; + +/* + *冒泡排序,时间复杂度O(N^2);空间复杂度:O(1);稳定排序 + *思路:第一趟:两两元素相比,前一个比后一个大就交换,直到将最大的元素交换到末尾位置;一共进行n-1趟这样的交换将可以把所有的元素排好。 +*/ +void MaoPao(int a[]){ + int temp; + int flag = 0; + // n-1趟排序 + for(int i = 0; i < len - 1; i++){ + for(int j = 0; j < len - 1 -i; j++){ + if(a[j] > a[j + 1]){ + temp = a[j]; + a[j] = a[j + 1]; + a[j + 1] = temp; + flag = 1; + } + } + // 若某一趟排序中没有元素交换则说明所有元素已经有序,不需要再排序 + if(flag == 0)break; + } +} + +/* + *插入排序,时间复杂度O(N^2);空间复杂度O(1);稳定排序 + *思路:假设数组除最后一个元素都有序了,那么将最后一个元素与前面的比较,如果前面的元素大则向右移动。实际过程从数组的第二个元素开始执行插入排序 +*/ +void ChaRu(int a[]){ + // 从第一轮就从第二个元素开始找,所以n-1轮就可以 + for(int i = 0; i < len - 1; i++){ + // 已经有序的最后一个元素 + int end = i; + // 需要排序的元素 + int temp = a[end + 1]; + while(end >= 0){ + if(a[end] > temp){ + // 直接替换 循环外面再将被替换的值放到适当位置 + a[end + 1] = a[end]; + end--; + } + else{ + break; + } + } + a[end + 1] = temp; + } +} + +/* + *希尔排序,时间复杂度O(N^1.5);空间复杂度O(1);不稳定排序。 + *思路:先选定一个整数gap,把待排序文件中所有记录分成gap个组,所有距离为gap的记录分在同一组内,并对每一组内的元素进行排序。然后将gap逐渐减小重复上述分组和排序的工作。当到达gap=1时,所有元素在统一组内排好序。 +*/ +void ShellSort(int a[], int n){ + int gap = n; + while (gap > 1) + { + //gap /= 2; + gap = gap / 3 + 1; + for (int i = 0; i < n - gap; i++) + { + int end = i; + int x = a[end + gap]; + while (end >= 0) + { + if (a[end] > x) + { + a[end + gap] = a[end]; + end -= gap; + } + else + { + break; + } + } + a[end + gap] = x; + } + } +} + +/* + *选择排序,时间复杂度O(N^2); 空间复杂度O(1);不稳定排序。 + *思路:第一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始(末尾)位置,然后选出次小(或次大)的一个元素,存放在最大(最小)元素的下一个位置,重复这样的步骤直到全部待排序的数据元素排完。优化:每次最大和最小同时选. +*/ +void SelectSort(int a[], int n){ + //保存数组的起始位置 + int begin = 0; + //保存换数组的末尾位置 + int end = n - 1; + int temp; + while (begin < end) + { + int maxi = begin;//保存最大元素下标 + int mini = begin;//保存最小元素下标 + //遍历数组寻找最小和最大元素 + for (int i = begin; i <= end; i++) + { + if (a[i] < a[mini]) + { + mini = i; + } + if (a[i] > a[maxi]) + { + maxi = i; + } + } + //将最小元素交换到起始位置 + temp = a[begin]; + a[begin] = a[mini]; + a[mini] = temp; + + //判断最大值的位置是否在起始位置 + if (maxi == begin) + { + maxi = mini; + } + //将最大元素交换到末尾位置 + temp = a[end]; + a[end] = a[maxi]; + a[maxi] = temp; + //移动数组起始和末尾位置 + begin++; + end--; + } +} + +/* + *堆排序,时间复杂度O(N*logN);空间复杂度O(1);不稳定排序。 + *思路:升序为例,构建一个堆结构,然后每个父节点均替换为子节点的最大值,然后根节点就是最大值,重复这个步骤。 +*/ +void HeapAdjust(int H[], int s, int length) +{ + int tmp = H[s]; + // 左孩子结点的位置。(i+1 为当前调整结点的右孩子结点的位置) + int child = 2 * s + 1; + while (child < length) + { + if (child + 1 < length && H[child] < H[child + 1]) + { // 如果右孩子大于左孩子(找到比当前待调整结点大的孩子结点) + child++; + } + if (H[s] < H[child]) + { // 如果较大的子结点大于父结点 + H[s] = H[child]; // 那么把较大的子结点往上移动,替换它的父结点 + s = child; // 重新设置s ,即待调整的下一个结点的位置 + child = 2 * s + 1; + } + else + { // 如果当前待调整结点大于它的左右孩子,则不需要调整,直接退出 + break; + } + H[s] = tmp; // 当前待调整的结点放到比其大的孩子结点位置上 + } +} +/** + * 初始堆进行调整 + * 将H[0..length-1]建成堆 + * 调整完之后第一个元素是序列的最小的元素 + */ +void BuildingHeap(int H[], int length) +{ + // 最后一个有孩子的节点的位置 i= (length -1) / 2 + for (int i = (length - 1) / 2; i >= 0; --i) + HeapAdjust(H, i, length); +} +void HeapSort(int H[], int length) +{ + // 初始堆 + BuildingHeap(H, length); + // 从最后一个元素开始对序列进行调整 + for (int i = length - 1; i > 0; --i) + { + // 交换堆顶元素H[0]和堆中最后一个元素 + int temp = H[i]; + H[i] = H[0]; + H[0] = temp; + // 每次交换堆顶元素和堆中最后一个元素之后,都要对堆进行调整 + HeapAdjust(H, 0, i); + } +} + +/* + *快速排序,时间复杂度O(N*logN);空间复杂度O(logN);不稳定排序。 + *思路:最右边的值为基准值,按照该排序码将待排序集合分割成两子序列,左子序列中所有元素均小于基准值,右子序列中所有元素均大于基准值,然后最左右子序列重复该过程,直到所有元素都排列在相应位置上为止。确定两个指针left 和right 分别从左边和右边向中间遍历数组。如果选最右边为基准值,那么left指针先走,如果遇到大于基准值的数就停下来。然后右边的指针再走,遇到小于基准值的数就停下来。交换left和right指针对应位置的值。重复以上步骤,直到left = right ,最后将基准值与left(right)位置的值交换。 +*/ +int PartSort(int a[], int left, int right){ + // 选最右面为基准 + int key = right; + while(left < right){ + //选右边为基准值,左指针先走 + while(left < right && a[left] <= a[key]){ + left++; + } + //右指针再走 + while(left < right && a[right] >= a[key]){ + right--; + } + // 交换 + int temp = a[right]; + a[right] = a[left]; + a[left] = temp; + } + int temp = a[key]; + a[key] = a[left]; + a[left] = temp; + return left; +} +void QuickSort(int a[], int left, int right){ + if(left >= right)return; + // 第一次快排 + int keyi = PartSort(a, left, right); + // 左子序列快排 + QuickSort(a, left, keyi - 1); + // 右子序列快排 + QuickSort(a, keyi + 1, right); +} + +/* + *归并排序,递归实现,时间复杂度O(N*logN);空间复杂度O(N);稳定排序。 + *思路:采用分治法,将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。 +*/ +void _MergeSort(int* a, int left, int right,int* tmp) +{ + //区间中没有元素时不再合并 + if (left >= right) + { + return; + } + + //划分数组,每次一分为二 + int mid = (left + right) / 2; + _MergeSort(a, left, mid,tmp);//划分左区间 + _MergeSort(a, mid + 1, right,tmp);//划分右区间 + + //合并有序序列 + int begin1 = left, end1 = mid;//有序序列1 + int begin2 = mid + 1, end2 = right;//有序序列2 + int i = left; + //注意结束条件为一个序列为空时就停止 + while (begin1 <= end1 && begin2 <= end2) + { + if (a[begin1] < a[begin2]) + { + tmp[i++] = a[begin1++]; + } + else + { + tmp[i++] = a[begin2++]; + } + } + + //两序列不可能同时为空,将剩余元素合并 + while (begin1 <= end1) + { + tmp[i++] = a[begin1++]; + } + + while (begin2 <= end2) + { + tmp[i++] = a[begin2++]; + } + + //将合并后的序列拷贝到原数组中 + //在这里拷贝的原因是 保证返回到上一层递归后两个子序列中的元素是有序的 + int j = 0; + for (j = left; j <= right; j++) + { + a[j] = tmp[j]; + } +} +void MergeSort(int* a, int n) +{ + //因为需要将两个有序序列合并,需借助额外数组 + int* tmp = (int*)malloc(sizeof(int) * n); + if (tmp == NULL) + { + perror("malloc"); + exit(-1); + } + _MergeSort(a, 0, n - 1,tmp); + free(tmp); + tmp = NULL; +} + +int main(){ + + // MaoPao(a); + // ChaRu(a); + // ShellSort(a, len); + // SelectSort(a, len); + // HeapSort(a, len); + // QuickSort(a, 0, len - 1); + // MergeSort(a, len); + cout<<"out:"< +#include +struct Node +{ + int data; + struct Node* next; +}; +struct Node* head; + +// 在链表头节点插入一个值为x的节点 +void Insert(int x){ + struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); + temp->data = x; + temp->next = head; + // if(head != NULL)temp->next = head; + head = temp; +} + +// 迭代法实现反转列表 +struct Node* Reverse1(struct Node* head){ + struct Node *Current,*Prev,*next; + Current = head; + Prev = NULL; + while(Current != NULL){ + next = Current->next; + Current->next = Prev; + Prev = Current; + Current = next; + } + head = Prev; + return head; +} + +// 递归法实现反转列表 +void Reverse2(struct Node* p){ + if(p->next ==NULL){ + head = p; + return; + } + Reverse2(p->next); + struct Node* q = p->next; + q->next = p; + p->next = NULL; +} + +// 链表内指定区间反转 调试好了 +struct Node* reverseBetween(struct Node* head, int m, int n ) { + // write code here + if(head == NULL)return head; + if(head->next == NULL)return head; + if(m == n)return head; + struct Node *Current,*Prev,*next,*start,*start_last; + int i; + Current = head; + Prev = NULL; + next = NULL; + // 先找到开始位置 + for (i=1; inext; + // Current->next = Prev; + Prev = Current; + Current = next; + } + // 标记 + start_last = Prev; + start = Current; + // 反转 + for (i=0; i<(n-m+1); i++) { + next = Current->next; + Current->next = Prev; + Prev = Current; + Current = next; + } + // 头尾节点重指向 + if(start != head){ + start->next = next; + start_last->next = Prev;//start!=head的情况下,需要保留start上一个指针 + } + else { + start->next = next; + head = Prev;//start==head的情况下,直接将head指向待反转的最后一个 + } + return head; +} + +//打印链表的所有值 +void Print(){ + struct Node* temp = head; + printf("List is:"); + while(temp){ + printf("%d",temp->data); + temp = temp->next; + } + printf("\n"); +} + +void Print2(struct Node*p){ + if(p ==NULL){ + printf("\n"); + return; + } + // 正序打印 + // printf("%d",p->data); + // Print2(p->next); + + // 反转打印 + printf("%d",p->data); + Print2(p->next); +} + +int main(){ + head = NULL; + int n,x,i; + printf("Please input the number of node:\n"); + scanf("%d",&n); + for(i = 0;i +void func(void){ + printf("hhh"); +} +int main(){ + func(); + func(2); + return 0; +} \ No newline at end of file diff --git a/question1.cpp b/question1.cpp new file mode 100644 index 0000000..48d194a --- /dev/null +++ b/question1.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +int subarraySum(vector& nums, int k) { + unordered_map mp; + mp[0] = 1; + int count = 0, pre = 0; + for (auto& x:nums) { + pre += x; + if (mp.find(pre - k) != mp.end()) { + count += mp[pre - k]; + } + mp[pre]++; + } + return count; +} +int main() { + int n; + cin >> n; + vector nums(n); + for(int i = 0; i < n; i++){ + cin >> nums[i]; + } + int k; + cin >> k; + cout << subarraySum(nums, k) << endl; + return 0; +} diff --git a/question1.exe b/question1.exe new file mode 100644 index 0000000..407d8dd Binary files /dev/null and b/question1.exe differ diff --git a/tempCodeRunnerFile.c b/tempCodeRunnerFile.c new file mode 100644 index 0000000..4227f73 --- /dev/null +++ b/tempCodeRunnerFile.c @@ -0,0 +1 @@ +p[3] = '3'; //error C3892: “p”: 不能给常量赋值 \ No newline at end of file diff --git a/tempCodeRunnerFile.cpp b/tempCodeRunnerFile.cpp new file mode 100644 index 0000000..9d63646 --- /dev/null +++ b/tempCodeRunnerFile.cpp @@ -0,0 +1,2 @@ + + // getline(cin, s); \ No newline at end of file diff --git a/test.cpp b/test.cpp index e61987f..19168de 100644 --- a/test.cpp +++ b/test.cpp @@ -1,59 +1,54 @@ -#include -#include -#include -#include -#include -#include - +#include +#include +#include using namespace std; - -// 计算一个数的质因数的最小公倍数 -long long lcm(long long a, long long b) { - return a / __gcd(a, b) * b; +pair fun(const string &str) +{ + vector substrs; + int len = str.length(); + string substring; + int maxcount(0); + //后缀数组 + for (int i = 0; i < len; ++i) + { + substrs.push_back(str.substr(i)); + // cout << substrs[i] << endl; + } + for (int i = 0; i < len; ++i) + { + for (int j = 1; j <= len; j++) { + int count = 1; + int sublen = j; + for (int k = i + 1; k < len; k++) { + + if (substrs[k].length() < sublen) { + break; + } + string str1 = substrs[i].substr(0, sublen); + string str2 = substrs[k].substr(0, sublen); + if (str1.compare(str2)==0) + { + ++count; + } + } + if (count > maxcount||(count == maxcount && sublen > substring.length())) + { + maxcount = count; + substring = substrs[i].substr(0, sublen); + } + } + } + return make_pair(maxcount, substring); } -// 计算一个城堡的完美度 -long long calculateCastlePerfectness(const vector>& blocks) { - long long product = 1; - for (const auto& block : blocks) { - product *= pow(block.first, block.second); - } - return product; +int main() +{ + string str; + int len; + cin >> len; + cin >> str; + auto res = fun(str); + cout << res.first << endl; + // cout << "the matched substring is:" << res.second << endl; + return 0; } - -int main() { - int n; - cin >> n; - - vector perfectness(n); - vector>> castles(n); - - for (int i = 0; i < n; ++i) { - int ti; - cin >> ti; - castles[i].resize(ti); - for (int j = 0; j < ti; ++j) { - int Pij, Cij; - cin >> Pij >> Cij; - castles[i][j] = {Pij, Cij}; - } - perfectness[i] = calculateCastlePerfectness(castles[i]); - } - - // 计算总完美度 - set uniquePerfectness; - for (int i = 0; i < n; ++i) { - long long currentLcm = 1; - for (int j = 0; j < n; ++j) { - if (j != i) { - currentLcm = lcm(currentLcm, perfectness[j]); - } - } - uniquePerfectness.insert(currentLcm); - } - - // 输出不同的总完美度的种数 - cout << uniquePerfectness.size() << endl; - - return 0; -} \ No newline at end of file diff --git a/test.exe b/test.exe new file mode 100644 index 0000000..b2f9653 Binary files /dev/null and b/test.exe differ diff --git a/test.py b/test.py deleted file mode 100644 index c7338af..0000000 --- a/test.py +++ /dev/null @@ -1,35 +0,0 @@ -def outer(fun): - def wrapper(s): - # 获取字符串的前三个字符 - first_three = s[1:4] - #倒序前三个字符 - reversed_three = first_three[::-1] - # 返回拼接后的字符串 - return first_three + reversed_three - return wrapper -@outer -def fun(s): - return s - - -str = input() -print(fun(str)) - - -# def process_list(li): -# li[:] = li[:6] -# li.sort(reverse=True) -# l1 = li[1::2] -# l2 = li[0::2] -# l2.reverse() -# li[:]= l2 + l1 -# return li - -# # str="[2,1,6,4,5,3,7,8]" -# str=input() -# str=str[1:-1] - -# after5 = str.split(',') -# res = process_list(after5) -# res2 = [int(i) for i in res] -# print(res2) \ No newline at end of file diff --git a/tree/tempCodeRunnerFile.c b/tree/tempCodeRunnerFile.c new file mode 100644 index 0000000..d6e44ca --- /dev/null +++ b/tree/tempCodeRunnerFile.c @@ -0,0 +1,6 @@ + root = Insert(root,10); + root = Insert(root,15); + root = Insert(root,20); + root = Insert(root,10); + root = Insert(root,35); + root = Insert(root,50); \ No newline at end of file diff --git a/tree/tree_search.c b/tree/tree_search.c new file mode 100644 index 0000000..46f7d70 --- /dev/null +++ b/tree/tree_search.c @@ -0,0 +1,182 @@ +#include +#include +#include +struct BstNode +{ + int data; + struct BstNode *left; + struct BstNode *right; +}; + +struct BstNode *GetNewNode(int data) +{ + struct BstNode *temp = (struct BstNode *)malloc(sizeof(struct BstNode)); + temp->data = data; + temp->left = NULL; + temp->right = NULL; + return temp; +} + +// 在二叉搜索树中插入一个节点 +struct BstNode *Insert(struct BstNode *root, int data) +{ + if (root == NULL) + { + root = GetNewNode(data); + } + else if (data <= root->data) + { + root->left = Insert(root->left, data); + } + else + { + root->right = Insert(root->right, data); + } + return root; +} + +// 在二叉搜索树中搜索一个节点 +bool Search(struct BstNode *root, int data) +{ + if (root == NULL) + return false; + else if (root->data == data) + return true; + else if (data <= root->data) + return Search(root->left, data); + else + return Search(root->right, data); +} + +// 二叉搜索树的最小值 +int FindMin(struct BstNode *root) +{ + struct BstNode *temp = root; + if (temp == NULL) + { + printf("root is empty\n"); + return -1; + } + else + { + while (temp->left != NULL) + { + temp = temp->left; + } + return temp->data; + } +} + +// 二叉搜索树的最大值 +int FindMax(struct BstNode *root) +{ + struct BstNode *temp = root; + if (temp == NULL) + { + printf("root is empty\n"); + return -1; + } + else + { + while (temp->right != NULL) + { + temp = temp->right; + } + return temp->data; + } +} +// 二叉搜索树的前序遍历 +void Preorder(struct BstNode *root) +{ + if (root == NULL) + return; + printf("%d,", root->data); + Preorder(root->left); + Preorder(root->right); +} +// 二叉搜索树的中序遍历 +void Inorder(struct BstNode *root) +{ + if (root == NULL) + return; + Inorder(root->left); + printf("%d,", root->data); + Inorder(root->right); +} +// 二叉搜索树的后序遍历 +void Poseorder(struct BstNode *root) +{ + if (root == NULL) + return; + Poseorder(root->left); + Poseorder(root->right); + printf("%d,", root->data); +} + +// 判断是否是二叉搜索树 +bool IsSubtreeLesser(struct BstNode *root, int data) +{ + if (root == NULL) + return true; + if (root->data <= data && IsSubtreeLesser(root->left, data) && IsSubtreeLesser(root->right, data)) + return true; + else + return false; +} +bool IsSubtreeGreater(struct BstNode *root, int data) +{ + if (root == NULL) + return true; + if (root->data >= data && IsSubtreeGreater(root->left, data) && IsSubtreeGreater(root->right, data)) + return true; + else + return false; +} +bool IsBinarySearchTree(struct BstNode *root) +{ + if (root == NULL) + return true; + if (IsSubtreeLesser(root->left, root->data) && IsSubtreeGreater(root->right, root->data) && IsBinarySearchTree(root->left) && IsBinarySearchTree(root->right)) + return true; + else + return false; +} +// 二叉搜索树的高度 +int FindHeight(struct BstNode *root) +{ + int LeftHeight = 0; + int RightHeight = 0; + if (root == NULL) + { + return -1; + } + LeftHeight = FindHeight(root->left); + RightHeight = FindHeight(root->right); + if (LeftHeight >= RightHeight) + return LeftHeight + 1; + else + return RightHeight + 1; +} + +int main() +{ + struct BstNode *root = NULL; + printf("input start!\n"); + root = Insert(root, 10); + root = Insert(root, 8); + root = Insert(root, 7); + root = Insert(root, 10); + root = Insert(root, 35); + root = Insert(root, 50); + printf("input yes!\n"); + printf("%d", FindHeight(root)); + // Inorder(root); + + // if(IsBinarySearchTree(root) == true)printf("This is IsBinarySearchTree"); + // else printf("This is not IsBinarySearchTree"); + // if(Search(root,30) == true)printf("Found!\n"); + // else printf("Not Found!\n"); + + // printf("%d\n",FindMax(root)); + // printf("%d",FindMin(root)); +} \ No newline at end of file diff --git a/tree/tree_search.exe b/tree/tree_search.exe new file mode 100644 index 0000000..66aca77 Binary files /dev/null and b/tree/tree_search.exe differ diff --git a/第二次.txt b/第二次.txt deleted file mode 100644 index b6fc4c6..0000000 --- a/第二次.txt +++ /dev/null @@ -1 +0,0 @@ -hello \ No newline at end of file