Browse Source

笔记本上的代码

bijiben
zzwcreator 2 weeks ago
parent
commit
db8d5d0bee
  1. 26
      .vscode/launch.json
  2. 57
      .vscode/settings.json
  3. 43
      .vscode/tasks.json
  4. 65
      BaiDu/list.cpp
  5. BIN
      BaiDu/output/list.exe
  6. BIN
      BaiDu/output/ques1.exe
  7. BIN
      BaiDu/output/ques2.exe
  8. BIN
      BaiDu/output/ques3.exe
  9. 46
      BaiDu/ques1.cpp
  10. 42
      BaiDu/ques2.cpp
  11. 135
      BaiDu/ques3.cpp
  12. 59
      DeWu/question2.cpp
  13. 38
      DiDi/ques1.cpp
  14. 77
      DiDi/ques2.cpp
  15. BIN
      Djiang/output/question1 copy.exe
  16. BIN
      Djiang/output/question1.exe
  17. BIN
      Djiang/output/question3.exe
  18. BIN
      Djiang/output/question4.exe
  19. 88
      Djiang/question1.cpp
  20. 81
      Djiang/question2.cpp
  21. 38
      Djiang/question3.cpp
  22. 82
      Djiang/question4.cpp
  23. 24
      HaiKang/que1.cpp
  24. BIN
      HaiKang/que1.exe
  25. 6
      HeSai/.vscode/settings.json
  26. 28
      HeSai/.vscode/tasks.json
  27. 15
      HeSai/test.c
  28. 61
      HeSai/test2.cpp
  29. BIN
      HeSai/test2.exe
  30. 28
      HongRuan/question1.cpp
  31. 36
      HongRuan/question2.cpp
  32. BIN
      HongRuan/question2.exe
  33. 56
      HongRuan/question3.cpp
  34. BIN
      HongRuan/question3.exe
  35. 6
      HuaWei/.vscode/settings.json
  36. BIN
      HuaWei/output/que.exe
  37. BIN
      HuaWei/output/ques.exe
  38. BIN
      HuaWei/output/ques2.exe
  39. 20
      HuaWei/que.cpp
  40. 47
      HuaWei/que2.cpp
  41. BIN
      HuaWei/que2.exe
  42. 69
      HuaWei/que3.py
  43. 104
      HuaWei/ques.cpp
  44. 72
      HuaWei/ques2.cpp
  45. BIN
      JingWeiHengRun/output/question1.exe
  46. 35
      JingWeiHengRun/question1.cpp
  47. 0
      JingWeiHengRun/question2.cpp
  48. 34
      LiXiang/TEST.PY
  49. BIN
      LiXiang/output/ques.exe
  50. 32
      LiXiang/ques.cpp
  51. 71
      MeiTuan1/question1.c
  52. 70
      MeiTuan1/question2.cpp
  53. BIN
      MeiTuan1/question2.exe
  54. 25
      MeiTuan2/question1.cpp
  55. BIN
      MeiTuan2/question1.exe
  56. 21
      MeiTuan2/question2.cpp
  57. BIN
      MeiTuan2/question2.exe
  58. 41
      MeiTuan2/question3.cpp
  59. BIN
      MeiTuan2/question3.exe
  60. 43
      MeiTuan2/question4.cpp
  61. BIN
      MeiTuan2/question4.exe
  62. 66
      MeiTuan2/question5.cpp
  63. BIN
      MeiTuan2/question5.exe
  64. 2
      MeiTuan2/tempCodeRunnerFile.cpp
  65. 82
      MeiTuan3/question1.cpp
  66. BIN
      MeiTuan3/question1.exe
  67. 82
      MeiTuan3/tempCodeRunnerFile.cpp
  68. BIN
      MeiTuan3/tempCodeRunnerFile.exe
  69. BIN
      MeiTuan4/output/question1.exe
  70. 51
      MeiTuan4/question1.cpp
  71. 27
      MeiTuan5/ques.c
  72. BIN
      MeiTuan5/ques.exe
  73. 68
      PinDD/test.cpp
  74. BIN
      PinDD/test.exe
  75. BIN
      XiaoHongShu/output/ques.exe
  76. 92
      XiaoHongShu/ques.cpp
  77. 57
      XiaoMi/ques1.cpp
  78. 42
      XiaoMi/ques2.cpp
  79. BIN
      XiaoMi/ques2.exe
  80. 153
      all/all.c
  81. 72
      all/all.cpp
  82. BIN
      all/all.exe
  83. BIN
      all/output/all.exe
  84. 266
      all/sort.c
  85. BIN
      all/sort.exe
  86. 303
      all/sort2.cpp
  87. BIN
      all/sort2.exe
  88. 140
      linked_list/linked_list.c
  89. BIN
      linked_list/linked_list.exe
  90. 1
      linked_list/tempCodeRunnerFile.c
  91. BIN
      linked_list/test1.exe
  92. BIN
      output/question1.exe
  93. BIN
      output/test copy.exe
  94. BIN
      output/test.exe
  95. 9
      question1.c
  96. 28
      question1.cpp
  97. BIN
      question1.exe
  98. 1
      tempCodeRunnerFile.c
  99. 2
      tempCodeRunnerFile.cpp
  100. 93
      test.cpp

26
.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
}
],
},
]
}

57
.vscode/settings.json

@ -1,14 +1,18 @@
{ {
"files.associations": { "files.associations": {
"vector": "cpp",
"algorithm": "cpp",
"sstream": "c",
"regex": "c",
"iostream": "cpp",
"ostream": "cpp",
"cmath": "cpp",
"ratio": "c",
"random": "c",
"array": "cpp",
"atomic": "cpp", "atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp", "cctype": "cpp",
"clocale": "cpp", "clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp", "cstddef": "cpp",
"cstdint": "cpp", "cstdint": "cpp",
"cstdio": "cpp", "cstdio": "cpp",
@ -16,37 +20,34 @@
"cstring": "cpp", "cstring": "cpp",
"ctime": "cpp", "ctime": "cpp",
"cwchar": "cpp", "cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "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", "initializer_list": "cpp",
"ios": "cpp",
"iosfwd": "cpp", "iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp", "istream": "cpp",
"iterator": "cpp",
"limits": "cpp", "limits": "cpp",
"memory": "cpp",
"new": "cpp", "new": "cpp",
"ostream": "cpp",
"stdexcept": "cpp", "stdexcept": "cpp",
"streambuf": "cpp", "streambuf": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "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", "unordered_set": "cpp",
"xhash": "cpp"
"valarray": "cpp"
} }
} }

43
.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."
}
]
}

65
BaiDu/list.cpp

@ -0,0 +1,65 @@
#include<iostream>
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;
}

BIN
BaiDu/output/list.exe

BIN
BaiDu/output/ques1.exe

BIN
BaiDu/output/ques2.exe

BIN
BaiDu/output/ques3.exe

46
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 -1i个数表示第i到i十1时间点秒针转过的圈数 10e-5
*/
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <iomanip>
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<string> 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;
}

42
BaiDu/ques2.cpp

@ -0,0 +1,42 @@
/*
A包含B(A|B)= A, |B中的所有为1的二进制位A中都为1n个正整数Ai使Ai()
nn个整数i个整数表示Ai0 Ai< 262144, 1 n2x1e5
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_set>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
unordered_set<int> 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<int> uniqueNumbers(uniqueSet.begin(), uniqueSet.end());
// Sort in descending order to apply the greedy strategy
sort(uniqueNumbers.rbegin(), uniqueNumbers.rend());
int selectedCount = 0;
vector<bool> 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;
}

135
BaiDu/ques3.cpp

@ -0,0 +1,135 @@
// /*
// */
// #include <iostream>
// using namespace std;
// // class T
// // {
// // public:
// // T(int i=0):x(i){ c++;}
// // ~T(){cout<<c<<"_";c--;}
// // private:
// // static int c;
// // int x;
// // };
// // int T::c=0;
// // int main()
// // {
// // T *p2=new T(123);
// // delete p2;
// // T p1[3]={1,2,3};
// // return 0;
// // }
// class P
// {
// public:
// P(int i=0,int j=0):X(i),Y(j){num++;}
// int GetX(){return X;}
// int GetY(){return Y;}
// void show(){cout<<X<<" "<<Y<<"_";}
// static int num;
// private:int X,Y;
// };
// int P::num=0;
// int main(){
// P D(1,2),*j=&D;
// int *q=&P::num;
// cout<<j->num;//1
// // cout<<j->*q;//2
// cout<<P::num;//3
// cout<<P::*q; //4
// return 0;
// }
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n, x, y, k;
cin >> n >> x >> y >> k;
vector<int> 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<vector<long long>> dp(n + 1, vector<long long>(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<int> 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<vector<long long>> dp(n + 1, vector<long long>(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;
// }

59
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 <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
// 读取输入
string t;
cin >> t;
int p, q;
cin >> p >> q;
int n = t.size();
vector<int> 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;
}

38
DiDi/ques1.cpp

@ -1,38 +0,0 @@
/*
使mn的数组和一个整数
*/
#include <iostream>
#include <vector>
using namespace std;
int maxConsecutiveElementsUnderSum(const std::vector<int>& 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<int> 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;
}

77
DiDi/ques2.cpp

@ -1,77 +0,0 @@
/*
A正在玩游戏n个不同星球m条双向航道A的任务是摧毁这些星球A将按照星球的编号从小到大依次摧毁各个星球A想知道在每个星球被摧毁时还剩下多少个联盟n个点m条边的无向图
n,mm行2u,v1<n,m<5e4
n个正整数
5 6
1 2
2 3
3 1
4 5
5 1
2 4
1 2 1 1 0
*/
#include <stdio.h>
#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);
}
}

BIN
Djiang/output/question1 copy.exe

BIN
Djiang/output/question1.exe

BIN
Djiang/output/question3.exe

BIN
Djiang/output/question4.exe

88
Djiang/question1.cpp

@ -0,0 +1,88 @@
/*
:
root
[1,2,3,4,5]
11
*/
#include <bits/stdc++.h>
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<int> &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<int> 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 <<endl;
return 0;
}

81
Djiang/question2.cpp

@ -0,0 +1,81 @@
/*
:
A.cpp B.cpp B.cpp A.cpp 0123 011231 2,1,0,3 2,1,3,0 1,2,-1,101122312,1,0,3
1:
"1,2,-1,1"
1:
"2,1,0,3"
*/
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string compileSeq(string input) {
//首先完成有向无环图的构建
//统计图中各节点的个数 并标记头节点为-1
//使用优先队列 按照先小后大的顺序遍历输出节点值
int len = input.size();
/*********构建有向无环图(指向关系)*********/
map<int, vector<int>> 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<int> indexcount(len, 0);//统计各节点个数
priority_queue<int, vector<int>, 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<int> 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;
}

38
Djiang/question3.cpp

@ -0,0 +1,38 @@
/*
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 100; // 节点数量
int f[N];
int init() {
// 初始化
for (int i=0; i<N; i++)
f[i] = i;
}
int getFather(int x) {
// 查询所在团伙代表人
return f[x]==x ? x : getFather(f[x]);
}
int merge(int a, int b) {
// 合并操作
f[getFather(a)] = getFather(b);
}
bool query(int a, int b) {
// 查询操作
return getFather(a) == getFather(b);
}
int main() {
init();
merge(3, 1); // 3和1是亲戚
merge(1, 4); // 1和4是亲戚
cout << getFather(3) << endl; // 输出3的团伙代表人+换行
cout << query(3, 1) << endl; // 输出3和1是否是亲戚+换行
}

82
Djiang/question4.cpp

@ -0,0 +1,82 @@
/*
n 0 n - 1
edges edges[i] = [fromi, toi] fromi toi
answer answer[i] i
u v u v
n = 8, edgeList = [[0,3],[0,4],[1,3],[2,4],[2,7],[3,5],[3,6],[3,7],[4,6]]
[[],[],[],[0,1],[0,2],[0,1,3],[0,1,2,3,4],[0,1,2,3]]
- 0 1 2
- 3 2 0 1
- 4 2 0 2
- 5 3 0 1 3
- 6 5 0 1 2 3 4
- 7 4 0 1 2 3
*/
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {
vector<unordered_set<int>> anc(n); // 存储每个节点祖先的辅助数组
vector<vector<int>> e(n); // 邻接表
vector<int> indeg(n); // 入度表
// 预处理
for (const auto& edge: edges) {
e[edge[0]].push_back(edge[1]);
++indeg[edge[1]];
}
// 广度优先搜索求解拓扑排序
queue<int> 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<vector<int>> 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<vector<int>> nums = {{0,3},{0,4},{1,3},{2,4},{2,7},{3,5},{3,6},{3,7},{4,6}};
vector<vector<int>> 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;
}

24
HaiKang/que1.cpp

@ -0,0 +1,24 @@
// #include<iostream>
// #include<vector>
// using namespace std;
// int main(){
// cout<<"asd"<<endl;
// return 0;
// }
#include<iostream>
#include<vector>
using namespace std;
int main(){
int* a[10] = {0};
string str = "132456";
cout<<str.length()<<endl;
return 0;
}

BIN
HaiKang/que1.exe

6
HeSai/.vscode/settings.json

@ -0,0 +1,6 @@
{
"files.associations": {
"ostream": "cpp",
"iostream": "cpp"
}
}

28
HeSai/.vscode/tasks.json

@ -0,0 +1,28 @@
{
"tasks": [
{
"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."
}
],
"version": "2.0.0"
}

15
HeSai/test.c

@ -0,0 +1,15 @@
#include<stdio.h>
#include<string.h>
struct student
{
string name;
int old;
char NJ;
};
int main(){
return 0;
}

61
HeSai/test2.cpp

@ -0,0 +1,61 @@
#include <iostream>
#include <cstring>
#include <vector>
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<student> aa;
aa.push_back(student1);
aa.push_back(student2);
aa.push_back(student3);
cout<<"please input the student name"<<endl;
cin>>name;
cout<<"please input the student old"<<endl;
cin>>old;
cout<<"please input the student nianji"<<endl;
cin>>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<<endl;
aa.push_back(studentx);
// cout<<"size "<<aa.size()<<endl;
for(j = aa.size() - 1; j > i + 1; j--){
cout<<"1111111111111111111"<<endl;
aa[j] = aa[j - 1];
cout<<"122222222222222"<<endl;
}
cout<<"122222222222222"<<endl;
aa[j--] = tmp;
break;
}
if(i == aa.size())aa.push_back(studentx);
for(i = 0; i < aa.size(); i++){
cout<<aa[i].name<<endl;
}
return 0;
}

BIN
HeSai/test2.exe

28
HongRuan/question1.cpp

@ -0,0 +1,28 @@
#include <bits/stdc++.h>
using namespace std;
int subarraySum(vector<int>& nums, int k) {
unordered_map<int, int> 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<int> nums(n);
for(int i = 0; i < n; i++){
cin >> nums[i];
}
int k;
cin >> k;
cout << subarraySum(nums, k) << endl;
return 0;
}

36
HongRuan/question2.cpp

@ -0,0 +1,36 @@
/*
:32reg1reg2reg3 alarm
reg1的第八位为状态位1reg2的0~3reg3 24~31
:0.1[-204, 204],
tuefalse1ture
:reg1reg2reg3 (3216)alam(32 10
):
:88 18000A1F 12345678 120
:0x0x12181.8:tue报警阈值 120 tue.
:88 12000a1a df12fe2d 120;0xa0xd74.3:false报警國值 120false
*/
#include <bits/stdc++.h>
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;
}

BIN
HongRuan/question2.exe

56
HongRuan/question3.cpp

@ -0,0 +1,56 @@
/*
ipv4是否合法
*/
#include <bits/stdc++.h>
using namespace std;
vector<string> split(string s, string spliter) {
vector<string> 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<string> 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;
}

BIN
HongRuan/question3.exe

6
HuaWei/.vscode/settings.json

@ -0,0 +1,6 @@
{
"files.associations": {
"iostream": "cpp",
"ostream": "cpp"
}
}

BIN
HuaWei/output/que.exe

BIN
HuaWei/output/ques.exe

BIN
HuaWei/output/ques2.exe

20
HuaWei/que.cpp

@ -0,0 +1,20 @@
#include<iostream>
using namespace std;
int main(){
string str;
int YinWen_num = 0, KongGe_num = 0, ShuZi_num = 0, Others_num = 0;
cout<<"please input str:"<<endl;
getline(cin, str);
for(int i = 0; i < str.length(); i++){
if((str[i] - 'a' >= 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:"<<YinWen_num<<endl;
cout<<"KongGe_num is:"<<KongGe_num<<endl;
cout<<"ShuZi_num is:"<<ShuZi_num<<endl;
cout<<"Others_num is:"<<Others_num<<endl;
return 0;
}

47
HuaWei/que2.cpp

@ -0,0 +1,47 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool canFillBox(int N, const vector<int>& toys, int c) {
// dp[i] indicates if we can fill exactly i capacity with given toys and fillers
vector<bool> 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<string> res;
while (T--) {
int N, n, c;
cin >> N >> n >> c;
vector<int> 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;
}

BIN
HuaWei/que2.exe

69
HuaWei/que3.py

@ -0,0 +1,69 @@
#
# wukun 华为云笔试
# 软件安装工具
# 有一个比较复杂的软件系统需要部署到客户提供的服务器上。该软件系统的安装过程非常繁琐,为了降低操作成本,需要开发一个工具实现自动化部署。软件的安装过程可以分成若干个小步骤,某些步骤间存在依赖关系,被依赖的步骤必须先执行完,才能执行后续的安装步骤。满足依赖条件的多个步骤可以并行执行。请你开发一个调度程序,以最短的时间完成软件的部署。
# 输入:
# 第一行:总步骤数 N(0<N<=10000)
# 第二行:N个以空格分隔的整数,代表每个步骤所需的时间。该行所有整数之和不大于int32
# 第三行开始的N行:表示每个步骤所依赖的其它步骤的编号(编号从1开始,行号减2表示步骤的编号),如果依赖多个步骤,用空格分隔。-1表示无依赖测试用例确保各个安装步骤不会出现循环依赖。
# 输出:
# 1个数字,代表最短执行时间。
# 样例1
# 输入:4
# 6 2 1 2
# -1
# 1
# 3
# 输出:9
# 样例2:
# 输入:4
# 1 2 3 4
# 2 3
# 3
# -1
# 1
# 输出:10
from collections import defaultdict, deque
def func(N, step_times, dependencies):
# 构建图和入度计数
graph = defaultdict(list)
in_degree = [0] * N
time_to_complete = [0] * N
# 填充依赖关系
for i in range(N):
for dep in dependencies[i]:
if dep != -1:
graph[dep - 1].append(i)
in_degree[i] += 1
# 使用队列处理入度为0的步骤
queue = deque()
for i in range(N):
if in_degree[i] == 0:
queue.append(i)
time_to_complete[i] = step_times[i]
# 进行拓扑排序
while queue:
current = queue.popleft()
current_completion_time = time_to_complete[current]
for neighbor in graph[current]:
in_degree[neighbor] -= 1
time_to_complete[neighbor] = max(time_to_complete[neighbor], current_completion_time + step_times[neighbor])
if in_degree[neighbor] == 0:
queue.append(neighbor)
return max(time_to_complete)
if __name__ == "__main__":
# 输入示例
N = int(input())
step_times = list(map(int, input().split()))
print(step_times)
dependencies = []
for _ in range(N):
deps = list(map(int, input().split()))
dependencies.append(deps)
# 输出最短执行时间
print(func(N, step_times, dependencies))

104
HuaWei/ques.cpp

@ -1,104 +0,0 @@
/*
c++
N x N 沿 1 K ( K )穿:
:
: C/C++1000ms,:2000ms内存限制: C/C++256MB,:512MB
:
N K后 N N ,2N100 K2N-2 1e4
:
穿
1
:
2
2
1 3
2 1
:2
:(2)线2
2
:
5
12
0 0 0 0 0
9 9 3 9 0
0 0 0 0 0
0 9 5 9 9
0 0 0 0 0
:3
:线:22224123
K=160;K=85
*/
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 1e9; // 定义一个足够大的值表示不可达
int N, K;
vector<vector<int>> 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<vector<bool>> visited(N, vector<bool>(N, false));
queue<pair<int, int>> 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<int>(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;
}

72
HuaWei/ques2.cpp

@ -1,72 +0,0 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 日志文件结构体
struct LogFile {
int process; // 进程序号
int size; // 文件大小
int downloads; // 下载次数
};
int main() {
int N, M, C;
cin >> N >> M >> C;
vector<vector<LogFile>> 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<int> mustHaveDownloads; // 每个进程必须要保存的日志文件的下载次数
vector<pair<int, int>> 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<int> 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;
}

BIN
JingWeiHengRun/output/question1.exe

35
JingWeiHengRun/question1.cpp

@ -0,0 +1,35 @@
/*
n1~n这n个整数的十进制表示中1出现的次数
1~1311101112136
*/
#include <iostream>
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)<<endl;
return 0;
}

0
JingWeiHengRun/question2.cpp

34
LiXiang/TEST.PY

@ -1,34 +0,0 @@
import math
# 定义向量x和y
x = (0, 1, 0, 1)
y = (1, 0, 1, 0)
# 计算余弦相似度
def cosine(x, y):
numerator = sum([x[i] * y[i] for i in range(len(x))])
denominator = math.sqrt(sum([x[i] ** 2 for i in range(len(x))])) * math.sqrt(sum([y[i] ** 2 for i in range(len(y))]))
return numerator / denominator
# 计算相关系数
def correlation(x, y):
n = len(x)
sum_x = sum(x)
sum_y = sum(y)
sum_xy = sum([x[i] * y[i] for i in range(n)])
sum_x2 = sum([x[i] ** 2 for i in range(n)])
sum_y2 = sum([y[i] ** 2 for i in range(n)])
numerator = n * sum_xy - sum_x * sum_y
denominator = math.sqrt((n * sum_x2 - sum_x ** 2) * (n * sum_y2 - sum_y ** 2))
return numerator / denominator
# 计算Jaccard系数
def jaccard(x, y):
intersection = sum([x[i] and y[i] for i in range(len(x))])
union = sum([x[i] or y[i] for i in range(len(x))])
return intersection / union
# 输出结果
print('余弦相似度:', cosine(x, y))
print('相关系数:', correlation(x, y))
print('Jaccard系数:', jaccard(x, y))

BIN
LiXiang/output/ques.exe

32
LiXiang/ques.cpp

@ -1,32 +0,0 @@
#include <iostream>
#include <iomanip>
using namespace std;
/*杨辉三角最终显示效果是一个等腰三角形,两个最外边都是1
*/
int main(){
int n;//变量n在此处起到了限制输出行数的作用,可优化成用户输入
cin>>n;
int i,j,a[n][n];
//使第一列和对角线元素的值为1
for (i=0;i<n;i++){ //前两行全为1,拿出来单独处理
a[i][i]=1;//使最右侧边全为1
a[i][0]=1;//使最左侧边全为1
}
//从第三行开始处理
for (i=2;i<n;i++) //三行开始出现变化
for (j=1;j<=i-1;j++) //j始终慢i一步
a[i][j]=a[i-1][j-1]+a[i-1][j];//每个数等于它上方两数之和,如a32=a21+a22
//输出数组各元素的值
for (i=0;i<n;i++){ //从第一行开始
for (j=0;j<=i;j++) //利用j将每一行的数据全部输出
cout<<a[i][j]<<" "; //在C++中,setw(int n)用来控制输出间隔,这里是指前元素末尾与后元素末尾之间有个5空格位
cout<<endl;
}
//cout<<endl;
return 0;
}

71
MeiTuan1/question1.c

@ -0,0 +1,71 @@
#include <stdio.h>
#include <stdlib.h>
#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 <stdio.h>
// // #define MAX_LEN 100 //
// // int main() {
// // char str[MAX_LEN];
// // printf('');
// // fgets(str, MAX_LEN, stdin);
// // printf('%s\n', str);
// // return 0;
// // }
// #include <stdio.h>
// #include <string.h>
// #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;
// }

70
MeiTuan1/question2.cpp

@ -0,0 +1,70 @@
// #include <iostream>
// #include <string>
// 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 <iostream>
#include <cstring>
#include <algorithm>
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;
}

BIN
MeiTuan1/question2.exe

25
MeiTuan2/question1.cpp

@ -0,0 +1,25 @@
/*第一行输入一个正整数n,代表菜品总数。第二行输入n个正整数ai,代表每道菜的价格。
x和y,x代表满减的价格y代表红包的价格
4
10 20 10 20
25 10
25
*/
#include <iostream>
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;
}

BIN
MeiTuan2/question1.exe

21
MeiTuan2/question2.cpp

@ -0,0 +1,21 @@
/*小美定义以下三种单词是合法的:1.所有字母都是小写。例如: good。2.所有字母都是大写。例如:APP。
3.:Alice
使?
*/
#include <iostream>
#include <string>
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<<opt;
return 0;
}

BIN
MeiTuan2/question2.exe

41
MeiTuan2/question3.cpp

@ -0,0 +1,41 @@
/*小美拿到了一个数组,她每次操作会将除了第x个元素的其余元素翻倍,一共操作了q次。请你帮小美计算操作结束后所有元素之和。
1e9+7
4 2
1 2 3 4
1 2
34
*/
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
int val,q,xi;
long sum=0;
const int MOD = 1e9 + 7;
cin >> n;
cin>>q;
vector<int> aa;
vector<int> 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<<sum % MOD;
return 0;
}

BIN
MeiTuan2/question3.exe

43
MeiTuan2/question4.cpp

@ -0,0 +1,43 @@
/*求区间众数和
nn个正整数1<n<200000,1<=a<=2
3
2 1 2
9
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
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;
}

BIN
MeiTuan2/question4.exe

66
MeiTuan2/question5.cpp

@ -0,0 +1,66 @@
#include<iostream>
#include<vector>
using namespace std;
const int N=200000;
void merge__(vector<int>& arr, vector<int>& 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<int>& arr, vector<int>& 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<int>& nums) {
// write code here
int ret = 0;
// 在最外层开辟数组
vector<int> tmp(nums.size());
merge_sort__(nums, tmp, 0, nums.size() - 1, ret);
return ret;
}
int main()
{
vector<int> q;
int n,a;
cin >> n;
int result;
for(int i=0;i<n;i++){
cin >>a;
q.push_back(a);
}
for(int i=0;i<n;i++){
q[i] = -q[i];
result = InversePairs(q);
cout << result<< ' ';
q[i] = -q[i];
}
return 0;
}

BIN
MeiTuan2/question5.exe

2
MeiTuan2/tempCodeRunnerFile.cpp

@ -0,0 +1,2 @@
for(int i = 0;i < n; i++){

82
MeiTuan3/question1.cpp

@ -0,0 +1,82 @@
// 接雨水问题
#include<iostream>
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
1dp数组 int[][] dp = new int[n][2]dp[i][0] i的柱子左边的最大值dp[i][1] i的柱子右边的最大值2height数组 dp[i][0] dp[i][1] 31
*/
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<<res<<endl;
return 0;
}

BIN
MeiTuan3/question1.exe

82
MeiTuan3/tempCodeRunnerFile.cpp

@ -0,0 +1,82 @@
// 接雨水问题
#include<iostream>
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
1dp数组 int[][] dp = new int[n][2]dp[i][0] i的柱子左边的最大值dp[i][1] i的柱子右边的最大值2height数组 dp[i][0] dp[i][1] 31
*/
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<<res<<endl;
return 0;
}

BIN
MeiTuan3/tempCodeRunnerFile.exe

BIN
MeiTuan4/output/question1.exe

51
MeiTuan4/question1.cpp

@ -0,0 +1,51 @@
/*
n a1,a2,.·,an :
1. a1x
2.k*MEX(a)MEX(a)a中未出现过的最小非负整数[0,1,2,4] MEX为3
a
T代表数据组数n,k,x
n个整数 a1, a2,...,an表示数组元素
a之和不超过 2x105.
*/
/*
+
dp[i]i往后考虑的最小花费dp[0]
mex是多少dp的过程中
O(n)
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int main() {
int T;
cin >> T; // 读取测试数据组数
while (T--) {
long long n, k, x;
cin >> n >> k >> x;
vector<long long> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
// 动态规划数组dp[i]表示从i往后考虑的最小花费,最后最小花费就是dp[0]或者直接删除后续所有元素
vector<long long> dp(n + 1, LLONG_MAX);
dp[n] = 0;
int suffix_mex = 0;
set<int> 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;
}

27
MeiTuan5/ques.c

@ -0,0 +1,27 @@
/*
*/
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}

BIN
MeiTuan5/ques.exe

68
PinDD/test.cpp

@ -0,0 +1,68 @@
/*
n的数组
1.k个非空子串()
2.k个子串的顺序
3.k个子串首尾相连
()k
T(T < 5)
::n,k0<k<n<1e5
n个数字a1,a2,,an0|ai|; 1e14
Tue"(不含引号),否则输出“False(不含引号)
3
5 3
8 12 7 -6 5
4 2
2 3 -6 4
5 5
10 6 8 -5 -10
True
False
True
K则可以
*/
#include <bits/stdc++.h>
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<long long> arr(n);
for(auto &x : arr)cin >> x;
//Create a vector of pairs(value, original index)
vector<pair<long long, long long>> 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";
}
}
}

BIN
PinDD/test.exe

BIN
XiaoHongShu/output/ques.exe

92
XiaoHongShu/ques.cpp

@ -1,92 +0,0 @@
/*
PK!ai代表目己的战斗力使
*/
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
int findClosestSum(vector<int>& nums, int target) {
int sum = accumulate(nums.begin(), nums.end(), 0); // 数组元素的总和
int maxSum = min(sum, target + abs(sum - target)); // 限制最大的可能和
vector<bool> 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<int> 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 <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
int findClosestSum(vector<int>& nums, int target) {
int n = nums.size();
int sum = accumulate(nums.begin(), nums.end(), 0); // 数组元素的总和
int maxSum = min(sum, target + abs(sum - target)); // 限制最大的可能和
vector<bool> 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<int> nums = {7,3,5,3}; // 输入数组
int target = 10; // 目标值
int result = findClosestSum(nums, target);
cout << result << endl;
return 0;
}

57
XiaoMi/ques1.cpp

@ -0,0 +1,57 @@
/*
:
1.:aaabbbaaa
2.123456
3.
3
aaabbbaaa
aabbaa
aaa
yes
no
yes
*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool isSymmetric(const char* str){
int n=strlen(str);
for(int i=0;i<n/2;++i){
if(str[i]!=str[n-1-i])return false;
}
return true;
}
bool isGrouped(const char* str){
int n= strlen(str);
for(int i=0;i< n; i += 3){
if(i + 2 >= 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;
}

42
XiaoMi/ques2.cpp

@ -0,0 +1,42 @@
/*
m个箱子用于打包行李wn件行李排成一行ai
?
5 2 8
6 3 2 5 3
4
*/
#include <stdio.h>
int main(){
int n, m;
long long w;
scanf("%d %d %lld",&n,&m, &w);
long long weights[n];
for(int i=0;i<n; ++i)scanf("%lld",&weights[i]);
int boxcount =1;
long long currentweight =0;
int rightIndex=n-1;
while(rightIndex>=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;
}

BIN
XiaoMi/ques2.exe

153
all/all.c

@ -0,0 +1,153 @@
#include<stdio.h>
//
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; i<m; i++) {
next = Current->next;
// 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键会自动在字符串后面添加\0tab键仍会留在输入的缓冲区中
gets\0 \n.
printf()puts():
printf("input string is:\n%s", str2); puts(str2);
*/
/*
int length = sizeof(array) / sizeof(array[0]);
#include<string.h> int length = strlen(strArray);
*/

72
all/all.cpp

@ -0,0 +1,72 @@
#include<iostream>
#include<cstring>
#include<string>
using namespace std; // 为了使用cin和cout
/*
#include<unordered_set>
unordered_set<int> st; // 哈希集合
st.insert(pHead1);//插入值
st.count(pHead2);//查看有没有某个值
// 迭代输出
unordered_set<int>::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>
unordered_map<int, int> mp; //哈希表
mp.find(depth);//查找某个key
map.count(key);//查看出现的次数
mp.erase(key)//移除某个元素
*/
/*
#include <stack>
stack<int> 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"<<endl;
getline(cin, d);
cout<<d<<endl;
return 0;
}
/*
#include <cstring> 的作用:
使string定义字符串
使strlen获得字符串(使char s[10])
int strcmp(const char *s1, const char *s2);s1<s2时s1=s2时= 0s1>s2时
*/
/*
cin和getline的区别
cin>>str不会读取空格 getline(cin, str); #include <string> str需要通过 string定义
*/

BIN
all/all.exe

BIN
all/output/all.exe

266
all/sort.c

@ -0,0 +1,266 @@
#include <stdio.h>
// 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;
}

BIN
all/sort.exe

303
all/sort2.cpp

@ -0,0 +1,303 @@
#include<iostream>
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)
*gapgap个组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:"<<endl;
for(int i = 0; i < len; i++){
cout<<a[i];
cout<<',';
}
}

BIN
all/sort2.exe

140
linked_list/linked_list.c

@ -0,0 +1,140 @@
#include <stdio.h>
#include <stdlib.h>
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; i<m; i++) {
next = Current->next;
// 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<n;i++){
printf("Please input the value of Node:\n");
scanf("%d",&x);
Insert(x);
Print();
}
head = Reverse1(head);
printf("Reverse linked list is:\n");
Print();
Reverse2(head);
printf("Reverse linked list is:\n");
Print();
// head = reverseBetween(head,2,4);
// Print();
// printf("Print2 linked list is:\n");
// Print2(head);
// char name[100];
// printf("What is your name?\n");
// scanf("%s",name);
// printf("Hello,%s,nice to meet you!\n",name);
}

BIN
linked_list/linked_list.exe

1
linked_list/tempCodeRunnerFile.c

@ -0,0 +1 @@

BIN
linked_list/test1.exe

BIN
output/question1.exe

BIN
output/test copy.exe

BIN
output/test.exe

9
question1.c

@ -0,0 +1,9 @@
#include <stdio.h>
void func(void){
printf("hhh");
}
int main(){
func();
func(2);
return 0;
}

28
question1.cpp

@ -0,0 +1,28 @@
#include <bits/stdc++.h>
using namespace std;
int subarraySum(vector<int>& nums, int k) {
unordered_map<int, int> 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<int> nums(n);
for(int i = 0; i < n; i++){
cin >> nums[i];
}
int k;
cin >> k;
cout << subarraySum(nums, k) << endl;
return 0;
}

BIN
question1.exe

1
tempCodeRunnerFile.c

@ -0,0 +1 @@
p[3] = '3'; //error C3892: p:

2
tempCodeRunnerFile.cpp

@ -0,0 +1,2 @@
// getline(cin, s);

93
test.cpp

@ -1,59 +1,54 @@
#include <iostream>
#include <vector>
#include <set>
#include <numeric>
#include <algorithm>
#include <cmath>
#include<iostream>
#include<string>
#include<vector>
using namespace std; using namespace std;
// 计算一个数的质因数的最小公倍数
long long lcm(long long a, long long b) {
return a / __gcd(a, b) * b;
}
// 计算一个城堡的完美度
long long calculateCastlePerfectness(const vector<pair<int, int>>& blocks) {
long long product = 1;
for (const auto& block : blocks) {
product *= pow(block.first, block.second);
pair<int, string> fun(const string &str)
{
vector<string> 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;
} }
return product;
}
int main() {
int n;
cin >> n;
vector<long long> perfectness(n);
vector<vector<pair<int, int>>> 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};
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;
} }
perfectness[i] = calculateCastlePerfectness(castles[i]);
string str1 = substrs[i].substr(0, sublen);
string str2 = substrs[k].substr(0, sublen);
if (str1.compare(str2)==0)
{
++count;
} }
// 计算总完美度
set<long long> 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]);
} }
if (count > maxcount||(count == maxcount && sublen > substring.length()))
{
maxcount = count;
substring = substrs[i].substr(0, sublen);
} }
uniquePerfectness.insert(currentLcm);
} }
}
return make_pair(maxcount, substring);
}
// 输出不同的总完美度的种数
cout << uniquePerfectness.size() << endl;
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; return 0;
} }

Some files were not shown because too many files changed in this diff

Loading…
Cancel
Save