diff --git "a/1+n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\2142224020139..cpp" "b/1+n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\2142224020139..cpp" new file mode 100644 index 0000000000000000000000000000000000000000..cb9408e2c6c82fd7e5988ae42ae36a2be0cf4a96 --- /dev/null +++ "b/1+n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\2142224020139..cpp" @@ -0,0 +1,44 @@ +#include +#include +#include +long fangfa1(long n) +{int i,sum; +sum = 0; +for(i=1;i<=n;i++) +sum += i; +return sum; +} +void fangfashijian1(long n) +{ + clock_t t; + long sum; + t=clock(); + sum= fangfa1(n); + t=clock()-t; + printf("方法1:\n"); + printf("结果:%ld",sum); + printf("用时:%lf秒\n",(float)t/CLOCKS_PER_SEC); + } +long fangfa2(long n) +{int sum; +sum = n*(n+1)/2; +return sum; +} +void fangfashijian2(long n) +{ + clock_t t; + long sum; + t=clock(); + sum= fangfa2(n); + t=clock()-t; + printf("方法2:\n"); + printf("结果:%ld",sum); + printf("用时:%lf秒\n",(float)t/CLOCKS_PER_SEC);} +int main() +{int n; +printf("n="); +scanf("%d",&n); +fangfashijian1(n); +fangfashijian2(n); +return 0; +} \ No newline at end of file diff --git "a/2104010149/chapter_4/P125 \351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" "b/2104010149/chapter_4/P125 \351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" deleted file mode 100644 index d3a327b1acc7daeaec370e0f4733f8eef6998cd2..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_4/P125 \351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - bool isPalindrome(string s) { - string a; - for (int i = 0; i < s.length(); i++) { - if (s[i] >= 'A' && s[i] <= 'Z' || s[i] >= 'a' && s[i] <= 'z' || s[i] >= '0' && s[i] <= '9') { - a += tolower(s[i]); - } - } - string newa; - for (int i = a.length() - 1; i >= 0; i--) { - newa += a[i]; - } - if (a == newa) { return true; } - else { return false; } - } -}; \ No newline at end of file diff --git "a/2104010149/chapter_4/P14 \346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.cpp" "b/2104010149/chapter_4/P14 \346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.cpp" deleted file mode 100644 index 808c12ed14f539385f6517ac99fc8766646d582e..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_4/P14 \346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - string longestCommonPrefix(vector& strs) { - string a; - bool judge = true; - string demo = strs[0]; - for (int i = 0; i < demo.length(); i++) { - for (int j = 0; j < strs.size(); j++) { - if (demo[i] != strs[j][i]) { judge = false; } - } - if (judge == true) { a += demo[i]; } - else { break; } - } - return a; - } -}; diff --git "a/2104010149/chapter_4/P28 \346\211\276\345\207\272\345\255\227\347\254\246\344\270\262\344\270\255\347\254\254\344\270\200\344\270\252\345\214\271\351\205\215\351\241\271\347\232\204\344\270\213\346\240\207.cpp" "b/2104010149/chapter_4/P28 \346\211\276\345\207\272\345\255\227\347\254\246\344\270\262\344\270\255\347\254\254\344\270\200\344\270\252\345\214\271\351\205\215\351\241\271\347\232\204\344\270\213\346\240\207.cpp" deleted file mode 100644 index 9bd4230a3e873a53ce51f065de35167f649f1d8c..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_4/P28 \346\211\276\345\207\272\345\255\227\347\254\246\344\270\262\344\270\255\347\254\254\344\270\200\344\270\252\345\214\271\351\205\215\351\241\271\347\232\204\344\270\213\346\240\207.cpp" +++ /dev/null @@ -1,22 +0,0 @@ -class Solution { -public: - int strStr(string haystack, string needle) { - int answerindex = -1; - if (needle.length() > haystack.length()) { return answerindex; } - for (int i = 0; i <= haystack.length() - needle.length(); i++) { - int index = 0; - int j = i; - if (haystack[j] == needle[index]) { - while (haystack[j] == needle[index]) { - if (index == needle.length() - 1 && haystack[j] == needle[index]) { - answerindex = i; - return answerindex; - } - index++; - j++; - } - } - } - return answerindex; - } -}; diff --git "a/2104010149/chapter_4/P459 \351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.cpp" "b/2104010149/chapter_4/P459 \351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.cpp" deleted file mode 100644 index f634d0bf670275896862dcda6ea70744f353740e..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_4/P459 \351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - bool repeatedSubstringPattern(string s) { - if (s.length() == 1) { return false; } - string a; - for (int i = 0; i <= s.length() / 2 - 1; i++) { - a += s[i]; - string temp; - while (temp.length() < s.length()) { - temp += a; - } - if (temp.compare(s) == 0) { return true; } - } - return false; - } -}; \ No newline at end of file diff --git "a/2104010149/chapter_4/\345\256\236\351\252\2143.cpp" "b/2104010149/chapter_4/\345\256\236\351\252\2143.cpp" deleted file mode 100644 index b5316d2ef2b5791c1a9be6c2559e1975ad2f50e8..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_4/\345\256\236\351\252\2143.cpp" +++ /dev/null @@ -1,109 +0,0 @@ -#include -using namespace std; - -int nextarr[100]; -int nextvalarr[100]; -void getnext(string t) {//求next数组 - int j = 0, k = -1; - nextarr[0] = -1; - while (j < t.length()) { - if (k == -1 || t[j] == t[k]) { - j++, k++; - nextarr[j] = k; - } - else { - k = nextarr[k]; - } - } - for (int i = 0; i < t.length(); i++) { - cout << nextarr[i] << ' '; - } -} - -void getnextval(string t) {//求nextval数组 - int j = 0, k = -1; - nextvalarr[0] = -1; - while (j < t.length()) { - if (k == -1 || t[j] == t[k]) { - j++, k++; - if (t[j] == t[k]) { - nextvalarr[j] = nextvalarr[k]; - } - else { - nextvalarr[j] = k; - } - } - else { - k = nextvalarr[k]; - } - }for (int i = 0; i < t.length(); i++) { - cout << nextvalarr[i] << ' '; - } -} - -int KMP(string s,string t) {//KMP匹配 - int i = 0, j = 0; - int answer = 0; - while (i < s.length()) { - if (j == -1 || s[i] == t[j]) { - i++, j++; - } - else { - j = nextarr[j]; - } - if (j == t.length()) { - answer = i - t.length(); - return answer; - } - } - return -1; -} - -int newKMP(string s, string t) {//KMP改进算法 - int i = 0, j = 0; - while (i < s.length() && j < t.length()) { - if (j == -1 || s[i] == t[j]) { - i++, j++; - } - else { - j = nextvalarr[j]; - } - } - if (j = t.length()) { return i - j; } - else { return -1; } -} - -int simplefind(string s, string x) {//简单匹配算法 - int i = 0, j = 0, index = 0; - while (i < s.length() && j < x.length()) { - if (s[i] == x[j]) { - i++; j++; - } - else { - j = 0; - index++; - i = index; - } - } - if (j == x.length()) { - return index; - } - return 0; -} - -int main() -{ - string s = "abcabcdabcdeabcdefabcdefg"; - string t = "abcdeabcdefab"; - cout << "简单匹配算法求出位置:" << simplefind(s, t) << endl; - cout << "获取next数组:"; - getnext(t); - cout << endl; - cout << "获取nextval数组:"; - getnextval(t); - cout << endl; - cout << "使用KMP算法求出位置:" << KMP(s, t) << endl; - cout << "使用改进KMP算法求出位置:" << newKMP(s, t) << endl; -} - - diff --git "a/2104010149/chapter_4/\345\256\236\351\252\2145.cpp" "b/2104010149/chapter_4/\345\256\236\351\252\2145.cpp" deleted file mode 100644 index a9ea12cf5931299f3944daa6c3427568ebddb4b8..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_4/\345\256\236\351\252\2145.cpp" +++ /dev/null @@ -1,27 +0,0 @@ -#include -using namespace std; -int index; -int maxlen; -void simplesearch(string s) { - string a; - for (int i = 0; i < s.length(); i++) { - for (int j = i + 1; j < s.length(); j++) { - if (s[i] == s[j]) { - int length = 1; - for (int k = 1; s[i + k] == s[j + k]; k++) { - length++; - } - if (length > maxlen) { - maxlen = length; - index = i; - } - } - } - } -} -int main() { - string s = "bananananannana"; - simplesearch(s); - cout << index << " " << maxlen; - return 0; -} diff --git "a/2104010149/chapter_4/\345\256\236\351\252\2146.cpp" "b/2104010149/chapter_4/\345\256\236\351\252\2146.cpp" deleted file mode 100644 index c64c7d865748f2c994ae5c8d6ec84482712291da..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_4/\345\256\236\351\252\2146.cpp" +++ /dev/null @@ -1,44 +0,0 @@ -#include -using namespace std; -int nextarr[100]; - -void getnext(string s) { - int j = 0, k = -1; - nextarr[0] = -1; - while (j < s.length()) { - if (k == -1 || s[j] == s[k]) { - j++; - k++; - nextarr[j] = k; - } - else { - k = nextarr[k]; - } - } -} - -int KMP(string s, string t) { - int i = 0, j = 0; - int cnt = 0; - while (i < s.length()) { - if (j == -1 || s[i] == t[j]) { - i++, j++; - } - else { - j = nextarr[j]; - } - if (j == t.length()) { - cnt++; - j = nextarr[j]; - } - } - return cnt; -} - -int main() { - string s = "aaabbdaabbde"; - string t = "aabbd"; - getnext(t); - cout << KMP(s, t); - return 0; -} diff --git "a/2104010149/chapter_5/P234 \345\233\236\346\226\207\351\223\276\350\241\250.cpp" "b/2104010149/chapter_5/P234 \345\233\236\346\226\207\351\223\276\350\241\250.cpp" deleted file mode 100644 index e2a26cf75709e343ebf14bdb99b56cd6cc662258..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_5/P234 \345\233\236\346\226\207\351\223\276\350\241\250.cpp" +++ /dev/null @@ -1,14 +0,0 @@ -class Solution { -public: - bool isPalindrome(ListNode* head) { - vectorarr; - while(head != NULL) { - arr.push_back(head->val); - head = head->next; - } - for (int i = 0, j = arr.size() - 1; i < j; i++, j--) { - if (arr[i] != arr[j]) { return false; } - } - return true; - } -}; \ No newline at end of file diff --git "a/2104010149/chapter_5/P509 \346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.cpp" "b/2104010149/chapter_5/P509 \346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.cpp" deleted file mode 100644 index f16ae56f7dd4a23290f2271b84e3c806344aca3e..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_5/P509 \346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.cpp" +++ /dev/null @@ -1,9 +0,0 @@ -class Solution { -public: - int fib(int n) { - if (n == 0) { return 0; } - if (n == 1) { return 1; } - if (n == 2) { return 1; } - return fib(n - 1) + fib(n - 2); - } -}; \ No newline at end of file diff --git "a/2104010149/chapter_5/\345\256\236\351\252\2141.cpp" "b/2104010149/chapter_5/\345\256\236\351\252\2141.cpp" deleted file mode 100644 index 4fab1fb0a5cf66e77e40569f5eab327e3d7632b3..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_5/\345\256\236\351\252\2141.cpp" +++ /dev/null @@ -1,23 +0,0 @@ -#include -int m = 0; -void Move(int n, char A, char B, char C) -{ - m++; - if (n == 1) { - printf("%c -> %c\n", A, C); - } - else { - Move(n - 1, A, C, B); - printf("%c -> %c\n", A, C); - Move(n - 1, B, A, C); - } -} - -int main() -{ - int n; - printf("请输入盘子数:"); - scanf_s("%d", &n); - Move(n, 'A', 'B', 'C'); - printf("移动次数:%d次", m); -} \ No newline at end of file diff --git "a/2104010149/chapter_5/\345\256\236\351\252\2143.cpp" "b/2104010149/chapter_5/\345\256\236\351\252\2143.cpp" deleted file mode 100644 index 5e77270890c51f374f4ee20917e1729229304901..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_5/\345\256\236\351\252\2143.cpp" +++ /dev/null @@ -1,38 +0,0 @@ -#include -using namespace std; - -string a; -vectorarr; -void dfs(string s,int begin,int len,int times) { - if (times > 3) { return; } - if (begin + len > a.size()) { return; } - string temp; - for (int i = begin; i < begin + len; i++) { - temp += a[i]; - } - if (stoi(temp) > 255) { return; } - for (int i = begin; i < begin + len; i++) { - s += a[i]; - } - if (times == 3 && s.size() - 3 == a.size()) { - arr.push_back(s); - return; - } - s += "."; - dfs(s, begin + len, 1, times + 1); - dfs(s, begin + len, 2, times + 1); - dfs(s, begin + len, 3, times + 1); -} -int main() { - cout << "请输入你的IP地址:" << endl; - cin >> a; - string temp; - dfs(temp, 0, 1, 0); - dfs(temp, 0, 2, 0); - dfs(temp, 0, 3, 0); - cout << "可以转换的IP地址为:" << endl; - for (int i = 0; i < arr.size(); i++) { - cout << arr[i] << endl; - } - return 0; -} diff --git "a/2104010149/chapter_5/\345\256\236\351\252\2147.cpp" "b/2104010149/chapter_5/\345\256\236\351\252\2147.cpp" deleted file mode 100644 index e25d308cd99b3e91f37b88d4dcb0605b7d7cc686..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_5/\345\256\236\351\252\2147.cpp" +++ /dev/null @@ -1,44 +0,0 @@ -#include -using namespace std; -typedef long long ll; -int n; -int a[100]; -int b[100]; -int c[100]; -int d[100]; -int total; -void getin() { - cin >> n; -} -void dfs(int i) { - if (i > n) { - total++; - if (total <= 3) { - for (int q = 1; q <= n; q++) { - cout << a[q] << ' '; - } - cout << endl; - } - return; - } - for (int j = 1; j <= n; j++) { - if (b[j] == 0 && c[i + j] == 0 && d[i - j + n] == 0) { - a[i] = j; - b[j] = 1; - c[i + j] = 1; - d[i - j + n] = 1; - dfs(i + 1); - b[j] = 0; - c[i + j] = 0; - d[i - j + n] = 0; - } - } -} -int main() { - std::ios::sync_with_stdio(false); - std::cin.tie(nullptr); - getin(); - dfs(1); - cout << total; - return 0; -} diff --git "a/2104010149/chapter_6/P1572 \347\237\251\351\230\265\345\257\271\350\247\222\347\272\277\345\205\203\347\264\240\347\232\204\345\222\214.cpp" "b/2104010149/chapter_6/P1572 \347\237\251\351\230\265\345\257\271\350\247\222\347\272\277\345\205\203\347\264\240\347\232\204\345\222\214.cpp" deleted file mode 100644 index d8c70c9ab53160d9f127a31cab5ed11e88f00652..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_6/P1572 \347\237\251\351\230\265\345\257\271\350\247\222\347\272\277\345\205\203\347\264\240\347\232\204\345\222\214.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - int diagonalSum(vector >& mat) { - int sum = 0; - for (int i = 0, j = 0; i < mat.size() && j < mat.size(); i++, j++) { - sum += mat[i][j]; - } - for (int i = 0, j = mat.size() - 1; i < mat.size() && j>=0; i++, j--) { - sum += mat[i][j]; - } - if (mat.size() % 2) { - sum -= mat[(mat.size() - 1) / 2][(mat.size() - 1) / 2]; - } - return sum; - } -}; \ No newline at end of file diff --git "a/2104010149/chapter_6/P283 \347\247\273\345\212\250\351\233\266.cpp" "b/2104010149/chapter_6/P283 \347\247\273\345\212\250\351\233\266.cpp" deleted file mode 100644 index 56ce72df78e2000aa4704d0d111dc87f3d1cc0c8..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_6/P283 \347\247\273\345\212\250\351\233\266.cpp" +++ /dev/null @@ -1,15 +0,0 @@ -class Solution { -public: - void moveZeroes(vector& nums) { - for (int i = 0; i < nums.size(); i++) { - if (nums[i] == 0) { - for (int j = i + 1; j < nums.size(); j++) { - if (nums[j] != 0) { - swap(nums[i], nums[j]); - break; - } - } - } - } - } -}; \ No newline at end of file diff --git "a/2104010149/chapter_6/P485 \346\234\200\345\244\247\350\277\236\347\273\2551\347\232\204\344\270\252\346\225\260.cpp" "b/2104010149/chapter_6/P485 \346\234\200\345\244\247\350\277\236\347\273\2551\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index d2c8d0d7193cc9ac5c458d46da7d1dbce4eb8060..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_6/P485 \346\234\200\345\244\247\350\277\236\347\273\2551\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - int findMaxConsecutiveOnes(vector& nums) { - int maxcnt = 0; - int cnt = 0; - for (int i = 0; i < nums.size(); i++) { - if (nums[i] == 1) { - cnt++; - maxcnt = max(maxcnt, cnt); - } - else { - cnt = 0; - } - } - return maxcnt; - } -}; \ No newline at end of file diff --git "a/2104010149/chapter_6/P566 \351\207\215\345\241\221\347\237\251\351\230\265.cpp" "b/2104010149/chapter_6/P566 \351\207\215\345\241\221\347\237\251\351\230\265.cpp" deleted file mode 100644 index 50e2b72309eca99f6a9da3e1ba9b467366bdd38e..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_6/P566 \351\207\215\345\241\221\347\237\251\351\230\265.cpp" +++ /dev/null @@ -1,15 +0,0 @@ -class Solution { -public: - vector> matrixReshape(vector>& mat, int r, int c) { - vector >temp(r, vector(c)); - if (mat.size() * mat[0].size() != r * c) { - return mat; - } - else { - for (int i = 0; i < mat.size() * mat[0].size(); i++) { - temp[i / c][i % c] = mat[i / mat[0].size()][i % mat[0].size()]; - } - return temp; - } - } -}; diff --git "a/2104010149/chapter_6/\345\256\236\351\252\2141.cpp" "b/2104010149/chapter_6/\345\256\236\351\252\2141.cpp" deleted file mode 100644 index cb5d5f323c02a9e6d6fad4f3247ba03648fcf77b..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_6/\345\256\236\351\252\2141.cpp" +++ /dev/null @@ -1,237 +0,0 @@ -#include -#include -#include -#include -#define false 0 -#define true 1 -#define max 100 -typedef struct { - int value; //value为具体数值 - int row,col; //row为行,col为列 -}Array; -typedef struct { - Array data[max+1]; - int rows,cols,nums; //nums为非零元素个数 -}Arrays; -//创建稀疏矩阵 -int InitArray(Arrays *L,int rows,int cols,int nums){ - int i,j; - L->nums=nums; - L->cols=cols; - L->rows=rows; - assert(L->numsnums; i++) { - scanf("%d %d %d", &L->data[i].row, &L->data[i].col, &L->data[i].value); - } - printf("创建成功!\n"); - return true; - } -//遍历输出稀疏矩阵 三元组形式 -void bianli1(Arrays *L) { - printf("-------------三元组形式形式遍历输出:\n"); - for (int i = 1; i <= L->rows; i++) { - printf("%d行%d列%d \n",L->data[i].row,L->data[i].col,L->data[i].value); - } -} - -//遍历输出稀疏矩阵 矩阵形式 -void bianli2(Arrays *L) { - int flag = 1; //flag代表 - for (int i = 1; i <= L->rows; i++) { - for (int j = 1; j <=L->cols; j++) { - if (L->data[flag].value!=0&&L->data[flag].row==i&&L->data[flag].col==j) { - printf(" %d", L->data[flag].value); - flag++; - } - else { - printf(" 0"); - } - } - printf("\n"); - } -} -//矩阵普通转置(按列) -int Transform(Arrays a,Arrays *b){ // //a为原矩阵,b为转置后的矩阵 - b->cols=a.cols; - b->nums=a.nums; - b->rows=a.rows; - if(b->nums>0){ - int j=1; - for(int k=1;k<=a.cols;k++){ - for(int i=1;i<=a.nums;i++){ //进入非零元素循环 - if(a.data[i].col==k){ //如果在每一个列中有非零元素,则进行转置操作 - b->data[j].row=a.data[i].col; //j代表表B的非零元素个数值,它从1开始。 - b->data[j].col=a.data[i].row; - b->data[j].value=a.data[i].value; - j++; - if(j>a.nums) return false; - } - } - } - } -} -//矩阵快速转置 -void FastTransform(Arrays *a,Arrays *b){ //a为原矩阵,b为转置后的矩阵 - int num[max],position[max]; - int col,i,k,m; - b->cols=a->cols; - b->nums=a->nums; - b->rows=a->rows; - if(b->nums>0){ - for(col=1;col<=a->cols;col++){ //首先将num数组的数值都赋值为0 - num[col]=0; - } - for(i=1;i<=a->nums;i++){ //再遍历非零元素,把非零元素的列所在的num数组进行++操作 - num[a->data[i].col]++; - } - position[1]=1; //第一个非零元素的position值定义为1 - for(col=2;col<=a->cols;col++){ - position[col]=position[col-1]+num[col-1]; //前一列非零元素的起始位置加非零元素的个数等于后一列的起始位置 - } - for(k=1;k<=a->nums;k++){ //对非零元素进行遍历并依次转置 - col=a->data[k].col; - m=position[col]; - b->data[m].row=a->data[k].col; - b->data[m].col=a->data[k].row; - b->data[m].value=a->data[k].value; - position[col]++; - } - } -} -//矩阵加法 -int ADD(Arrays a,Arrays b,Arrays *c){ - int k=1,i,j; //i为a的元素数目,j为b的元素数目,k为c的元素数目 - - //同行同列的才能相加 - if(a.cols!=b.cols||a.rows!=b.rows){ - return false; - } - - c->cols=a.cols; //赋值总行数,总列数 - c->rows=a.rows; - //进行遍历赋值 - for(i=1,j=1;i<=a.nums&&j<=b.nums;){ - - //B的行号大于A直接将A中元素加入C矩阵 - if(a.data[i].rowdata[k].col=a.data[i].col; - c->data[k].row=a.data[i].row; - c->data[k].value=a.data[i].value; - k++; //C元素向后增一位 - i++; //a赋值则a元素向后增一位,如果时b则b元素向后增一位 - } - //B的行号小于A直接将B中元素加入C矩阵 - else if(a.data[i].row>b.data[j].row){ - c->data[k].col=b.data[j].col; - c->data[k].row=b.data[j].row; - c->data[k].value=b.data[j].value; - k++; - j++; - }else{ //行号相同 - - //B的列号小于A直接将B中元素加入C矩阵 - if(a.data[i].col>b.data[j].col) { - c->data[k].col=b.data[j].col; - c->data[k].row=b.data[j].row; - c->data[k].value=b.data[j].value; - k++; - j++; - } - - //B的列号大于A直接将A中元素加入C矩 - else if(a.data[i].coldata[k].col=a.data[i].col; - c->data[k].row=a.data[i].row; - c->data[k].value=a.data[i].value; - k++; - i++; - } - //相等 - else { - - c->data[k].col=a.data[i].col; - c->data[k].row=a.data[i].row; - c->data[k].value=a.data[i].value+b.data[j].value; - k++; - i++; - j++; - } - } - } - while(i<=a.nums){ //B取完A未取完 - - //将A中所剩元素依次加入到C中 - c->data[k].col=a.data[i].col; - c->data[k].row=a.data[i].row; - c->data[k].value=a.data[i].value; - k++; - i++; - } - while(j<=b.nums){ //A取完B未取完 - - //将A中所剩元素依次加入到C中 - c->data[k].col=b.data[j].col; - c->data[k].row=b.data[j].row; - c->data[k].value=b.data[j].value; - k++; - j++; - } -} - - - - -//矩阵减法 -int reduce(Arrays a,Arrays b,Arrays *c){ //调用加法操作,在b矩阵基础上乘 -1 在加 a矩阵 - for(int i=1;i<=b.nums;i++){ - b.data[i].value=b.data[i].value*-1; - } - ADD(a,b,c); -} -//矩阵乘法 -int multipe(Arrays a,Arrays b,Arrays *c){ - int m=1,n=1,k=1; //m为a的元素数目,n为b的元素数目,k为c的元素数目 - if(a.cols!=b.cols||a.rows!=b.rows){ - return false; - } - c->cols=a.cols; - c->rows=a.rows; - while(m<=a.nums&&n<=b.nums){ - if(a.data[m].col==b.data[n].col&&a.data[m].row==b.data[n].row){ - c->data[k].col=a.data[m].col; - c->data[k].row=a.data[m].row; - c->data[k].value=a.data[m].value*b.data[n].value; - } - m++;n++;k++; - } -} -int main(){ - Arrays s; - Arrays s1; - Arrays s2; - Arrays s3; - int row, col, num; - //创建 - printf("请依次输入稀疏矩阵A的行数,列数,非零元个数(用空格隔开):\n"); - scanf("%d %d %d", &row, &col, &num); - InitArray(&s,row,col,num); - bianli2(&s); - printf("请依次输入稀疏矩阵B的行数,列数,非零元个数(用空格隔开):\n"); - scanf("%d %d %d", &row, &col, &num); - InitArray(&s1,row,col,num); - bianli2(&s1); - printf("---------矩阵B的转置为:\n"); - Transform(s1,&s3); - bianli2(&s3); - printf("---------矩阵相乘为:\n"); - multipe(s,s1,&s2); - bianli2(&s2); - printf("---------矩阵相加为:\n"); - ADD(s,s1,&s2); - bianli2(&s2); - printf("---------矩阵相减为:\n"); - reduce(s,s1,&s2); - bianli2(&s2); -} \ No newline at end of file diff --git "a/2104010149/chapter_6/\345\256\236\351\252\2143.cpp" "b/2104010149/chapter_6/\345\256\236\351\252\2143.cpp" deleted file mode 100644 index b20dd325768627a261f3c60dd8fabb6b15833c70..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_6/\345\256\236\351\252\2143.cpp" +++ /dev/null @@ -1,65 +0,0 @@ -#include -using namespace std; - -int arr[10][10] = { - {1,2,3,4,5}, - {16,17,18,19,6}, - {15,24,25,20,7}, - {14,23,22,21,6}, - {13,12,11,10,9} -}; -bool ifdetect[10][10]; - - -int main() { - int toward = 0;//0东 1南 2西 3北 - int i = 0, j = 0; - while (1) { - if (toward == 0) { - cout << arr[i][j] << " "; - ifdetect[i][j] = true; - if (arr[i][j] == 25) { break; } - if (arr[i][j + 1] == 0 || ifdetect[i][j + 1]) { - toward = 1; - i++; - } - else { - j++; - } - } - else if (toward == 1) { - cout << arr[i][j] << " "; - ifdetect[i][j] = true; - if (arr[i + 1][j] == 0 || ifdetect[i + 1][j]) { - toward = 2; - j--; - } - else { - i++; - } - } - else if (toward == 2) { - cout << arr[i][j] << " "; - ifdetect[i][j] = true; - if (arr[i][j-1] == 0 || ifdetect[i][j-1]) { - toward = 3; - i--; - } - else { - j--; - } - } - else if (toward == 3) { - cout << arr[i][j] << " "; - ifdetect[i][j] = true; - if (arr[i-1][j] == 0 || ifdetect[i -1][j]) { - toward = 0; - j++; - } - else { - i--; - } - } - } - return 0; -} diff --git "a/2104010149/chapter_6/\345\256\236\351\252\2145.cpp" "b/2104010149/chapter_6/\345\256\236\351\252\2145.cpp" deleted file mode 100644 index d1321a88b00b66e2fb8c7f7cd83c4d15c1d69efc..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_6/\345\256\236\351\252\2145.cpp" +++ /dev/null @@ -1,89 +0,0 @@ -#include -using namespace std; - -#define N 4 -#define M 10 -/* ----------------------输入矩阵a,b n表示矩阵所含的行数------------- */ -static void input(int n, int a[], int b[]) -{ - for(int k=0;k>a[k]; - } - for(int k=0;k>b[k]; - } -} -/*-----------------------返回压缩存储a中a[i][j]的值-----------------------*/ -/** -* 算法思路 -* 对称矩阵M的第i行和第j列的元素的数据存储在一维数组a中的位置k的计算公式: -* 1、当i大于或等于j时,k = (i * (i + 1)) / 2 + j (下三角) -* 2、当i小于j时,k = (j * (j + 1)) / 2 + i (上三角) -* -*/ -static int value(int a[], int i,int j) -{ - if (i>=j) - return a[i*(i+1)/2+j]; - else - return a[j*(j+1)/2+i]; -} -/*-----------------------求压缩存储a和b的和-----------------------*/ -static void madd(int a[],int b[],int c[][N]) -{ - for (int i=0;i>n; - int a[n*(n+1)/2],b[n*(n+1)/2]; - int c1[N][N],c2[N][N]; - input(n,a,b); - cout<<"两个矩阵之和"<> levelOrder(TreeNode* root) { - vector> answer; - queueq; - q.push(root); - if (root == NULL) { return answer; } - - while (!q.empty()) { - vectortemp; - int len=q.size(); - for (int i = 0; i < len; i++) { - root = q.front(); - q.pop(); - temp.push_back(root->val); - if (root->left) { q.push(root->left); } - if (root->right) { q.push(root->right); } - } - answer.push_back(temp); - } - return answer; - } -}; \ No newline at end of file diff --git "a/2104010149/chapter_7/P104 \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.cpp" "b/2104010149/chapter_7/P104 \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.cpp" deleted file mode 100644 index 47b6f945e00839f8c54cdf0ba1691d3e3116dc42..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_7/P104 \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.cpp" +++ /dev/null @@ -1,7 +0,0 @@ -class Solution { -public: - int maxDepth(TreeNode* root) { - if(root==NULL)return 0; - return max(maxDepth(root->left),maxDepth(root->right))+1; - } -}; \ No newline at end of file diff --git "a/2104010149/chapter_7/P105 \344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.cpp" "b/2104010149/chapter_7/P105 \344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.cpp" deleted file mode 100644 index 1b80dc1a7ec499debbe83aaa5612beb32832591b..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_7/P105 \344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.cpp" +++ /dev/null @@ -1,24 +0,0 @@ -class Solution { -private: - unordered_mapm; -public: - TreeNode* dfs(vector& preorder, vectorinorder, int pl, int pr, int il, int ir) { - if (pl > pr) { - return NULL; - } - int proot = pl; - int iroot = m[preorder[pl]]; - TreeNode* root = new TreeNode(preorder[pl]); - int len = iroot - il; - root->left = dfs(preorder, inorder, proot + 1, proot + len, il, iroot - 1); - root->right = dfs(preorder, inorder, proot + len + 1, pr, iroot + 1, ir); - return root; - } - TreeNode* buildTree(vector& preorder, vector& inorder) { - if (preorder.empty()) { return NULL; } - for (int i = 0; i < preorder.size(); i++) { - m[inorder[i]] = i; - } - return dfs(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1); - } -}; \ No newline at end of file diff --git "a/2104010149/chapter_7/P112 \350\267\257\345\276\204\346\200\273\345\222\214.cpp" "b/2104010149/chapter_7/P112 \350\267\257\345\276\204\346\200\273\345\222\214.cpp" deleted file mode 100644 index e4755c313e23da9af948b8f086d18424e96bfcce..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_7/P112 \350\267\257\345\276\204\346\200\273\345\222\214.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - void dfs(TreeNode* node,int sum,int target,bool& judge) { - if (node == NULL) { return ; } - sum += node->val; - if (sum == target && !node->left && !node->right) { judge = true; } - if (node->left) { dfs(node->left, sum, target, judge); } - if (node->right) { dfs(node->right, sum, target, judge); } - } - bool hasPathSum(TreeNode* root, int targetSum) { - if (!root) { return false; } - bool judge = false; - dfs(root, 0, targetSum, judge); - return judge; - } -}; diff --git "a/2104010149/chapter_7/P145 \344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.cpp" "b/2104010149/chapter_7/P145 \344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.cpp" deleted file mode 100644 index 36ad1308246fee96c7915caabd7498b79ae809b1..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_7/P145 \344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - void dfs(TreeNode* root, vector& answer) { - if (root == NULL) { - return; - } - dfs(root->left, answer); - dfs(root->right, answer); - answer.push_back(root->val); - } - vector postorderTraversal(TreeNode* root) { - vectoranswer; - dfs(root, answer); - return answer; - } -}; \ No newline at end of file diff --git "a/2104010149/chapter_7/P236 \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.cpp" "b/2104010149/chapter_7/P236 \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.cpp" deleted file mode 100644 index 5c4e1b73c7e215e9f9a2fe0bd81a416c937c04a1..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_7/P236 \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.cpp" +++ /dev/null @@ -1,14 +0,0 @@ -class Solution { -public: - TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { - if(root==NULL||root==p||root==q){ - return root; - }else{ - TreeNode*left=lowestCommonAncestor(root->left,p,q); - TreeNode*right=lowestCommonAncestor(root->right,p,q); - if(left==NULL){return right;} - if(right==NULL){return left;} - } - return root; - } -}; \ No newline at end of file diff --git "a/2104010149/chapter_7/P515 \345\234\250\346\257\217\344\270\252\346\240\221\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274.cpp" "b/2104010149/chapter_7/P515 \345\234\250\346\257\217\344\270\252\346\240\221\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274.cpp" deleted file mode 100644 index 2863e2ecd7b9eb162f804909d0f75c6fdcd35ee0..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_7/P515 \345\234\250\346\257\217\344\270\252\346\240\221\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274.cpp" +++ /dev/null @@ -1,22 +0,0 @@ -class Solution { -public: - vector largestValues(TreeNode* root) { - vectorarr; - queueq; - q.push(root); - if(root==NULL){return {};} - while(!q.empty()){ - int maxnum=INT_MIN; - int len=q.size(); - for(int i=0;ival); - if(root->left){q.push(root->left);} - if(root->right){q.push(root->right);} - } - arr.push_back(maxnum); - } - return arr; - } -}; \ No newline at end of file diff --git "a/2104010149/chapter_7/P589 N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.cpp" "b/2104010149/chapter_7/P589 N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.cpp" deleted file mode 100644 index faf3af8c76cb44e333d62606a254fd37afeb040c..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_7/P589 N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.cpp" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - void dfs(Node*node,vector& arr){ - if(!node){return ;} - arr.push_back(node->val); - for(int i=0;ichildren.size();i++){ - if(node->children[i]){ - dfs(node->children[i],arr); - } - } - } - vector preorder(Node* root) { - vectorarr; - dfs(root,arr); - return arr; - } -}; \ No newline at end of file diff --git "a/2104010149/chapter_7/P662 \344\272\214\345\217\211\346\240\221\346\234\200\345\244\247\345\256\275\345\272\246.cpp" "b/2104010149/chapter_7/P662 \344\272\214\345\217\211\346\240\221\346\234\200\345\244\247\345\256\275\345\272\246.cpp" deleted file mode 100644 index b23e2ddce0148553c1bf4b389b4d80222960287a..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_7/P662 \344\272\214\345\217\211\346\240\221\346\234\200\345\244\247\345\256\275\345\272\246.cpp" +++ /dev/null @@ -1,27 +0,0 @@ -class Solution { -public: - int widthOfBinaryTree(TreeNode* root) { - if(!root){ - return 0; - } - unsigned long long maxwidth=1; - queue> q; - q.push(make_pair(root, 1)); - while (!q.empty()) { - maxwidth = max(maxwidth, q.back().second - q.front().second + 1); - int len = q.size(); - for (int i = 0; i < len; i++) { - auto node = q.front().first; - auto index = q.front().second; - q.pop(); - if (node->left) { - q.push(make_pair(node->left, index * 2)); - } - if(node->right) { - q.push(make_pair(node->right, index * 2 + 1)); - } - } - } - return maxwidth; - } -}; \ No newline at end of file diff --git "a/2104010149/chapter_8/P997 \346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\263\225\345\256\230.cpp" "b/2104010149/chapter_8/P997 \346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\263\225\345\256\230.cpp" deleted file mode 100644 index e3a210db3ce24949acdd544e75908ebad695a382..0000000000000000000000000000000000000000 --- "a/2104010149/chapter_8/P997 \346\211\276\345\210\260\345\260\217\351\225\207\347\232\204\346\263\225\345\256\230.cpp" +++ /dev/null @@ -1,22 +0,0 @@ -class Solution { -private: - int arr[1001]; - int brr[1001]; -public: - int findJudge(int n, vector>& trust) { - for (int i = 0; i < trust.size(); i++) { - arr[trust[i][0]]++; - brr[trust[i][1]]++; - } - int cnt = 0; - int index = 0; - for (int i = 1; i <= n; i++) { - if (arr[i] == 0 && brr[i] == n - 1) { - cnt++; - index = i; - } - } - if (cnt == 1) { return index; } - else { return -1; } - } -}; \ No newline at end of file diff --git a/2110010054/chap-7/leetcode145.cpp b/2110010054/chap-7/leetcode145.cpp deleted file mode 100644 index 05988e70b89395ba0ed650363a3fc1ce5ffb9db8..0000000000000000000000000000000000000000 --- a/2110010054/chap-7/leetcode145.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * struct TreeNode *left; - * struct TreeNode *right; - * }; - */ -/** - * Note: The returned array must be malloced, assume caller calls free(). - */ - void postorder(struct TreeNode* root, int *ret,int* returnSize) - { - if (root==NULL) return; - postorder(root->left, ret,returnSize); - postorder(root->right, ret,returnSize); - ret[(*returnSize)++]=root->val; - - } -int* postorderTraversal(struct TreeNode* root, int* returnSize) { - int * ret=(int *)malloc(sizeof(int)*100); - *returnSize=0; - postorder(root, ret,returnSize); - return ret; -} \ No newline at end of file diff --git a/2124010070/chapter07/lc_102.cpp b/2124010070/chapter07/lc_102.cpp deleted file mode 100644 index 54b1aa58a9848648d6697a0268ef1713399eba0b..0000000000000000000000000000000000000000 --- a/2124010070/chapter07/lc_102.cpp +++ /dev/null @@ -1,39 +0,0 @@ -int numsize=0; - int **arr; - int arr_size=0; -int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes) { - *returnSize=0; - if(root==NULL) - return NULL; - arr_size=0; - arr=(int**)malloc(sizeof(int*)*2001); - *returnColumnSizes=malloc(sizeof(int*)*2001); - struct TreeNode* queue[2001]; - int front=-1,tail=-1; - struct TreeNode* p; - queue[++tail]=root; - while(front!=tail) - { - int current_count=tail-front; //求当前层次节点个数 - arr[arr_size]=(int*)malloc(sizeof(int)*current_count); - numsize=0; - for(int i=0;ival; - if(p->left!=NULL) - { - queue[++tail]=p->left; - } - if(p->right!=NULL) - { - queue[++tail]=p->right; - } - } - (*returnColumnSizes)[arr_size++]=numsize; - - } - *returnSize=arr_size; - return arr; - -} \ No newline at end of file diff --git a/2124010070/chapter07/lc_104.cpp b/2124010070/chapter07/lc_104.cpp deleted file mode 100644 index 793cb92520575db568f09bb72b966506e1ae6f74..0000000000000000000000000000000000000000 --- a/2124010070/chapter07/lc_104.cpp +++ /dev/null @@ -1,11 +0,0 @@ -int maxDepth(struct TreeNode* root) { - int left_len=0,right_len=0; - if(root==NULL) - return 0; - else - { - left_len=maxDepth(root->left); - right_len=maxDepth(root->right); - return left_len>right_len? (left_len+1):(right_len+1); - } -} \ No newline at end of file diff --git a/2124010070/chapter07/lc_105.cpp b/2124010070/chapter07/lc_105.cpp deleted file mode 100644 index b096158210c84370fc69b8640b3e9898a4dcb988..0000000000000000000000000000000000000000 --- a/2124010070/chapter07/lc_105.cpp +++ /dev/null @@ -1,18 +0,0 @@ -typedef struct TreeNode BTNode; -struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize) { - BTNode *root; - int *p; - int k; - if(preorderSize <= 0 || inorderSize <= 0) return NULL; - root=(BTNode*)malloc(sizeof(BTNode)); - root->val=*preorder;//头节点赋值 - for(p=inorder; pleft=buildTree(preorder+1,k,inorder,k); - root->right=buildTree(preorder+k+1,preorderSize-k-1,p+1,inorderSize-k-1); - return root; - -} \ No newline at end of file diff --git a/2124010070/chapter07/lc_112.cpp b/2124010070/chapter07/lc_112.cpp deleted file mode 100644 index d356d5c04227697d3f4bdc24cd2cfd4c354a92b5..0000000000000000000000000000000000000000 --- a/2124010070/chapter07/lc_112.cpp +++ /dev/null @@ -1,48 +0,0 @@ -typedef struct TreeNode BTNode; -typedef struct -{ - BTNode *p;//存放当前指针节点 - int parent;//存放父节点在队列中的位置 -}NodeType;//队列中的元素类型 - - -bool hasPathSum(struct TreeNode* root, int targetSum) { - if(root==NULL) return false; - int k; - BTNode *temp; - NodeType e; - NodeType Queue[5000];//定义队列 - int front=-1,tail=-1; - e.p=root;e.parent=-1; - Queue[++tail]=e;//根节点进队 - while(front!=tail) - { - e=Queue[++front]; - temp=e.p; - if(temp->left==NULL&&temp->right==NULL) //叶子节点 - { - k=front; - int sum=0; - while(Queue[k].parent!=-1) - { - sum+=Queue[k].p->val; - k=Queue[k].parent; - } - sum+=Queue[k].p->val; - if(sum==targetSum) return true; - } - if(temp->left!=NULL) - { - e.p=temp->left; - e.parent=front; - Queue[++tail]=e; - } - if(temp->right!=NULL) - { - e.p=temp->right; - e.parent=front; - Queue[++tail]=e; - } - } - return false; -} \ No newline at end of file diff --git a/2124010070/chapter07/lc_145.cpp b/2124010070/chapter07/lc_145.cpp deleted file mode 100644 index 5a8f1ab363d84de29bb8bf0570d73375d28b260e..0000000000000000000000000000000000000000 --- a/2124010070/chapter07/lc_145.cpp +++ /dev/null @@ -1,17 +0,0 @@ -void postorder(struct TreeNode* root ,int *result,int *resultSize) -{ - if(root==NULL) - { - return; - } - postorder(root->left,result,resultSize); - postorder(root->right,result,resultSize); - result[(*resultSize)++] = root->val; -} - -int* postorderTraversal(struct TreeNode* root, int* returnSize) { - *returnSize=0; - int *result = malloc(sizeof(int)*2001); - postorder(root,result,returnSize); - return result; -} \ No newline at end of file diff --git a/2124010070/chapter07/lc_236.cpp b/2124010070/chapter07/lc_236.cpp deleted file mode 100644 index baa5d78391f1ccc4b84b04b1966405ee90408af0..0000000000000000000000000000000000000000 --- a/2124010070/chapter07/lc_236.cpp +++ /dev/null @@ -1,16 +0,0 @@ -typedef struct TreeNode BTNode; - -struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) -{ - if (root == NULL || root == p || root == q) - { - return root; - } - BTNode* left = lowestCommonAncestor(root->left, p, q); - BTNode* right = lowestCommonAncestor(root->right, p, q); - if (left && right) { - return root; - } - return left ? left : right; - -} \ No newline at end of file diff --git a/2124010070/chapter07/lc_515.cpp b/2124010070/chapter07/lc_515.cpp deleted file mode 100644 index 3557fa1a943e560b5c87b17ebd099ed9b80ed7d2..0000000000000000000000000000000000000000 --- a/2124010070/chapter07/lc_515.cpp +++ /dev/null @@ -1,32 +0,0 @@ -int* largestValues(struct TreeNode* root, int* returnSize) { - *returnSize=0; - if(root==NULL) - return NULL; - int *ret; - int ret_count=0; - ret=(int *)malloc(sizeof(int)*10001); - struct TreeNode* queue[10001]; - struct TreeNode* p; - int front=-1,tail=-1; - queue[++tail]=root; - while(front!=tail) - { - int current_count=tail-front; - int current_max=queue[front+1]->val; - for(int i=0;ival > current_max) - current_max = p->val; - if(p->left!=NULL) - queue[++tail]=p->left; - if(p->right!=NULL) - queue[++tail]=p->right; - - } - ret[ret_count]=current_max; - ret_count++; - } - *returnSize=ret_count; - return ret; -} \ No newline at end of file diff --git a/2124010070/chapter07/lc_589.cpp b/2124010070/chapter07/lc_589.cpp deleted file mode 100644 index fe303c9624384aaf666280086cd833d4a4654bf4..0000000000000000000000000000000000000000 --- a/2124010070/chapter07/lc_589.cpp +++ /dev/null @@ -1,24 +0,0 @@ -int* preorder(struct Node* root, int* returnSize) { - if(root==NULL) - { - *returnSize=0; - return NULL; - } - int *result=(int *)malloc(sizeof(int)*10000); - struct Node **stack=(struct Node **)malloc(sizeof(struct Node *)*10000); - int pos=0,top=0; - stack[top++]=root; - while(top!=0) - { - struct Node * node = stack[--top]; - result[pos++]=node->val; - for(int i=node->numChildren-1;i>=0;i--) - { - stack[top++] =node->children[i]; - } - } - free(stack); - *returnSize=pos; - return result; - -} \ No newline at end of file diff --git a/2124010070/chapter07/lc_662.cpp b/2124010070/chapter07/lc_662.cpp deleted file mode 100644 index 8671fa84c0645b51351d5f2f8d4632a05e7bd9ea..0000000000000000000000000000000000000000 --- a/2124010070/chapter07/lc_662.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#define Max 3000 - -typedef struct -{ - struct TreeNode *node; - unsigned long long index; -}Pair; - -int widthOfBinaryTree(struct TreeNode* root){ - if(root==NULL) - return 0; - int front=0,tail=0; - int max=1; //初始最大宽度为1(不为空的时候) - int current_width=0; - struct TreeNode *next_end=NULL;//下一层的末尾节点 - struct TreeNode *cur_end=root;//当前层次的末尾节点 - Pair* queue=(Pair*)malloc(sizeof(Pair)*Max); //队列 - queue[tail].node=root; - queue[tail].index=1; - tail++; - int k=tail; - while(tail>front) - { - Pair temp=queue[front++]; - if(temp.node->left!=NULL) - { - next_end=temp.node->left; - queue[tail].node=temp.node->left; - queue[tail].index=2 * temp.index; - tail++; - - } - if(temp.node->right!=NULL) - { - next_end=temp.node->right; - queue[tail].node=temp.node->right; - queue[tail].index=2 * temp.index +1; - tail++; - - } - if(temp.node==cur_end) - { - current_width=(tail-k) == 0? 0:queue[tail-1].index-queue[k].index+1; - max=max > current_width ? max:current_width; - current_width=0; - k=tail; - cur_end=next_end; - } - } - return max; -} \ No newline at end of file diff --git a/2124010070/chapter09/lc_110.cpp b/2124010070/chapter09/lc_110.cpp deleted file mode 100644 index cf22bebfcd191dbda09d539e48996bad35a35794..0000000000000000000000000000000000000000 --- a/2124010070/chapter09/lc_110.cpp +++ /dev/null @@ -1,14 +0,0 @@ -int height(struct TreeNode* root) -{ - if(root==NULL) - return 0; - int left=height(root->left); - int right=height(root->right); - return left>right?(left+1):(right+1); -} -bool isBalanced(struct TreeNode* root) { - if(root==NULL) - return true; - else - return fabs(height(root->left)-height(root->right))<=1&&isBalanced(root->left)&&isBalanced(root->right); -} \ No newline at end of file diff --git a/2124010070/chapter09/lc_215.cpp b/2124010070/chapter09/lc_215.cpp deleted file mode 100644 index e719dd7068ad4c21e4378c527786adacad400dcc..0000000000000000000000000000000000000000 --- a/2124010070/chapter09/lc_215.cpp +++ /dev/null @@ -1,23 +0,0 @@ -int QuickSort(int *nums,int left,int right,int k) -{ - if(left>=right) return nums[left]; - int i=left-1,j=right+1,x=nums[left+right>>1]; - while(ix); - if(i=k) return QuickSort(nums,left,j,k); - else return QuickSort(nums,j+1,right,k-SL); -} - -int findKthLargest(int* nums, int numsSize, int k) { - return QuickSort(nums,0,numsSize-1,numsSize-k+1); -} \ No newline at end of file diff --git a/2124010070/chapter09/lc_235.cpp b/2124010070/chapter09/lc_235.cpp deleted file mode 100644 index d29998e7f142e7bcd2dcc9552b662a79cc691f35..0000000000000000000000000000000000000000 --- a/2124010070/chapter09/lc_235.cpp +++ /dev/null @@ -1,13 +0,0 @@ -typedef struct TreeNode BTNode; -struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) { - if (root == NULL || root == p || root == q) - { - return root; - } - BTNode* left = lowestCommonAncestor(root->left, p, q); - BTNode* right = lowestCommonAncestor(root->right, p, q); - if (left && right) { - return root; - } - return left ? left : right; -} \ No newline at end of file diff --git a/2124010070/chapter09/lc_380.cpp b/2124010070/chapter09/lc_380.cpp deleted file mode 100644 index 046605de207c372bab4abe4cf41937eabc91031e..0000000000000000000000000000000000000000 --- a/2124010070/chapter09/lc_380.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#define Max 200000 -#define INVAILD -1 - -typedef struct { - int value[Max]; - int count; -} RandomizedSet; - - -RandomizedSet* randomizedSetCreate() { - RandomizedSet * obj=(RandomizedSet *)malloc(sizeof(RandomizedSet)); - memset(obj->value,0,sizeof(int)*Max); - return obj; -} - -bool randomizedSetInsert(RandomizedSet* obj, int val) { - int cnt=obj->count; - if(cnt==0) - { - obj->value[cnt++]=val; - obj->count=cnt; - } - else - { - for(int i=0;ivalue[i]==val) - return false; - } - obj->value[cnt++]=val; - obj->count=cnt; - } - return true; -} - -bool randomizedSetRemove(RandomizedSet* obj, int val) { - int index=INVAILD; - int cnt=obj->count; - if(cnt==0) - return false; - else - { - for(int i=0;ivalue[i]==val) - { - index=i; - break; - } - } - if(index==INVAILD) return false; - for(int i=index;ivalue[i+1]; - obj->value[i]=temp; - } - cnt--; - obj->count=cnt; - return true; - } -} - -int randomizedSetGetRandom(RandomizedSet* obj) { - int cnt=obj->count; - int n=rand()%cnt; - return obj->value[n]; -} - -void randomizedSetFree(RandomizedSet* obj) { - free(obj); -} \ No newline at end of file diff --git a/2124010070/chapter09/lc_4.cpp b/2124010070/chapter09/lc_4.cpp deleted file mode 100644 index 63cb0b079f092e2ce19399b67dab2e241e2232da..0000000000000000000000000000000000000000 --- a/2124010070/chapter09/lc_4.cpp +++ /dev/null @@ -1,24 +0,0 @@ -double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) { - double mid; - int *s; - int i=0,j=0,temp=0; - long temp1,temp2; - s=(int *)malloc(sizeof(int)*(nums1Size+nums2Size)); - while(i *first) - { - third = second; - second = first; - first = &nums[i]; - } - else if ( nums[i] < *first && ( second == NULL || nums[i] > *second ) ) - { - third = second; - second = &nums[i]; - } - else if ( second != NULL && *second > nums[i] && ( third == NULL || nums[i] > *third ) ) - { - third = &nums[i]; - } - } - return third==NULL? *first:*third; -} \ No newline at end of file diff --git a/2124010070/chapter09/lc_704.cpp b/2124010070/chapter09/lc_704.cpp deleted file mode 100644 index e3c0a1865f8fa24979757754fa7fe3b39a7e26a4..0000000000000000000000000000000000000000 --- a/2124010070/chapter09/lc_704.cpp +++ /dev/null @@ -1,19 +0,0 @@ -int search(int* nums, int numsSize, int target) { - int left=0,right=numsSize-1; - int middle; - while(left<=right) - { - middle=(left+right)/2; - if(target>nums[middle]) - { - left=middle+1; - } - else if(targetvalue=(int*)malloc(sizeof(int)*MAXSIZE); - memset(obj->value, -1, sizeof(int) * MAXSIZE); - return obj; -} - -void myHashSetAdd(MyHashSet* obj, int key) { - if(obj->value[key]==-1) - obj->value[key]=key; -} - -void myHashSetRemove(MyHashSet* obj, int key) { - obj->value[key]=-1; -} - -bool myHashSetContains(MyHashSet* obj, int key) { - return obj->value[key]!=-1; -} - -void myHashSetFree(MyHashSet* obj) { - free(obj->value); - free(obj); -} \ No newline at end of file diff --git a/2124010070/experiment_07/BTree.cpp b/2124010070/experiment_07/BTree.cpp deleted file mode 100644 index 98b18638e9b784734bd9ad09c75b0914bb8fa128..0000000000000000000000000000000000000000 --- a/2124010070/experiment_07/BTree.cpp +++ /dev/null @@ -1,138 +0,0 @@ -#include -#include -#include -#include -#define Maxsize 100 -typedef char ElemType; - -typedef struct node -{ - ElemType value; - struct node *lchild; - struct node *rchild; -} BTNode; - -void CreateBTree(BTNode *&b, char *str) -{ - BTNode *St[Maxsize], *p; //数组模拟栈,用于存储双亲节点 - int top = -1, k = 0, i = 0; // k是标志位,1代表左子树,2代表右子树 - char ch; //用于遍历字符串 - b = NULL; //初始化根节点 - ch = str[i]; - while (ch != '\0') - { - switch (ch) - { - case '(': - top++; - St[top] = p; - k = 1; - break; - case ',': - k = 2; - break; - case ')': - St[top--]; - break; - default: - p = (BTNode *)malloc(sizeof(BTNode)); - p->lchild = NULL; - p->rchild = NULL; - p->value = ch; - if (b == NULL) - { - b = p; - } - else - { - switch (k) - { - case 1: - St[top]->lchild = p; - break; - case 2: - St[top]->rchild = p; - break; - } - } - break; - } - ch = str[++i]; - } -} - -void DestoryBTree(BTNode *&b) -{ - if (b != NULL) - { - DestoryBTree(b->lchild); - DestoryBTree(b->rchild); - free(b); - } -} - -BTNode *FindNode(BTNode *b, ElemType x) -{ - BTNode *p; - if (b == NULL) - { - return NULL; - } - else if (b->value == x) - { - return b; - } - else - { - p = FindNode(b->lchild, x); - if (p != NULL) - { - return p; - } - else - { - return FindNode(b->rchild, x); - } - } -} - -BTNode *LchildNode(BTNode *p) -{ - return p->lchild; -} - -BTNode *RchildNode(BTNode *p) -{ - return p->rchild; -} - -int BTHeight(BTNode *b) -{ - int lchildh, rchildh; - if (b == NULL) - { - return 0; - } - else - { - lchildh = BTHeight(b->lchild); - rchildh = BTHeight(b->rchild); - return lchildh > rchildh ? (lchildh + 1) : (rchildh + 1); - } -} - -void DispBTree(BTNode *b) -{ - if (b != NULL) - { - printf("%c", b->value); - if (b->lchild != NULL || b->rchild != NULL) - { - printf("("); - DispBTree(b->lchild); - printf(","); - DispBTree(b->rchild); - printf(")"); - } - } -} diff --git a/2124010070/experiment_07/exp7-1.cpp b/2124010070/experiment_07/exp7-1.cpp deleted file mode 100644 index 28d18f6bca4a4af3f6f9c7296c54c875c7cb3a84..0000000000000000000000000000000000000000 --- a/2124010070/experiment_07/exp7-1.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include -#include "BTree.cpp" - -int main(void) -{ - BTNode *b = NULL; - char tree[50] = "(A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"; - char *str = tree; - - printf("该二叉树的括号表示方法为:\n"); - CreateBTree(b, str); - DispBTree(b); - printf("\n"); - - BTNode *p = FindNode(b, 'H'); - printf("H节点左孩子的值为:%c\nH节点右孩子的值为:%c", p->lchild->value, p->rchild->value); - - int height = BTHeight(b); - printf("该二叉树的高度为:%d\n", height); - - DestoryBTree(b); -} \ No newline at end of file diff --git a/2124010070/experiment_07/exp7-10.cpp b/2124010070/experiment_07/exp7-10.cpp deleted file mode 100644 index 7fd2521a1f5cfdd8e796c6c05e3605a430352b3a..0000000000000000000000000000000000000000 --- a/2124010070/experiment_07/exp7-10.cpp +++ /dev/null @@ -1,202 +0,0 @@ -#include -#include -#include -#include -#define MaxSons 3 -#define MaxSize 66 -typedef char ElemType; -typedef struct node -{ - ElemType data[15]; //结点的值 - struct node *sons[MaxSons]; //指向孩子结点 -} TSonNode; //孩子链存储结构中的结点类型 -typedef struct -{ - char N[15]; - char n[15]; -} array; -//读取文件内容到数组R中 -void ReadFile(array R[], FILE *fp, int &n) -{ - while ((fscanf(fp, "%s", R[n].N)) != EOF && (fscanf(fp, "%s", R[n].n)) != EOF) - n++; -} -//创建一颗树 -TSonNode *CreateTree(char str[], array R[], int n) -{ - TSonNode *t; - int k, i = 0, j = 0; - t = (TSonNode *)malloc(sizeof(TSonNode)); - strcpy(t->data, str); - for (k = 0; k < MaxSons; k++) - t->sons[k] = NULL; - while (i < n) - { - if (strcmp(R[i].N, str) == 0) - { - t->sons[j] = CreateTree(R[i].n, R, n); - j++; - } - i++; - } - return t; -} -//输出树(孩子链存储结构) -void DispTree(TSonNode *t) -{ - int i = 0; - if (t == NULL) - printf("此树为空树!\n"); - else - { - printf("%s", t->data); - if (t->sons[i] != NULL) //若t结点至少有一个孩子 - { - printf("("); - for (i = 0; i < MaxSons; i++) - { - DispTree(t->sons[i]); - if (t->sons[i + 1] != NULL) - printf(","); - else - break; - } - printf(")"); - } - } -} -//销毁树 -void DestroyTree(TSonNode *t) -{ - if (t == NULL) - printf("此树为空树!\n"); - else - { - for (int i = 0; i < MaxSons; i++) - { - if (t->sons[i] != NULL) - DestroyTree(t->sons[i]); - else - break; - } - free(t); - } -} -//查找某一结点 -TSonNode *FindNode(TSonNode *t, char str[]) -{ - TSonNode *p; - if (t == NULL) - return NULL; - else - { - if (strcmp(t->data, str) == 0) - return t; - else - { - for (int i = 0; i < MaxSons; i++) - { - if (t->sons[i] != NULL) - { - p = FindNode(t->sons[i], str); - if (p != NULL) - return p; - } - } - return NULL; - } - } -} -//求某一结点的孩子个数 -int ChildCount(TSonNode *p) -{ - int count = 0; - for (int i = 0; i < MaxSons; i++) - { - if (p->sons[i] != NULL) - count++; - else - break; - } - return count; -} -//求某棵树中的叶子结点数 -//本例中,叶子结点数等于班级数,一个叶子结点对应一个班级 -int LeafCount(TSonNode *p) -{ - int count = 0; - if (p == NULL) - return 0; - else - { - if (p->sons[0] == NULL) - count++; - else - { - for (int i = 0; i < MaxSons; i++) - { - if (p->sons[i] != NULL) - count = count + LeafCount(p->sons[i]); - else - break; - } - } - } - return count; -} -//求某棵树的叶子结点值的和 -int LeafSumOfvalue(TSonNode *p) -{ - int sum = 0; - if (p == NULL) - return 0; - else - { - if (p->sons[0] == NULL) - return atoi(p->data); - else - { - for (int i = 0; i < MaxSons; i++) - { - if (p->sons[i] != NULL) - sum += LeafSumOfvalue(p->sons[i]); - else - break; - } - } - } - return sum; -} - -int main() -{ - int n = 0; - TSonNode *t; - array R[MaxSize]; - FILE *fp; - if ((fp = fopen("table.txt", "r")) == NULL) //以只读方式打开table.txt文件 - { - printf("error!cannot open the file!"); - exit(1); - } - printf("读取文件内容存入数组R中\n"); - ReadFile(R, fp, n); - printf("输出数组R:\n"); - for (int i = 0; i < n; i++) - printf("%s %s\n", R[i].N, R[i].n); //输出R数组查看是否读取正确 - printf("\n由数组R创建树T,"); - t = CreateTree(R[0].N, R, n); //创建一颗树 - printf("由括号表示输出树T:\n"); - DispTree(t); - char str[10]; - printf("\n请输入学院名:"); - scanf("%s", str); - printf("\n%s的专业数:%d\n", str, ChildCount(FindNode(t, str))); - printf("%s的班级数:%d\n", str, LeafCount(FindNode(t, str))); - printf("\n请输入学院名:"); - scanf("%s", str); - printf("\n%s的学生数:%d\n", str, LeafSumOfvalue(FindNode(t, str))); - printf("销毁树!\n"); - DestroyTree(t); - return 0; -} \ No newline at end of file diff --git a/2124010070/experiment_07/exp7-5.cpp b/2124010070/experiment_07/exp7-5.cpp deleted file mode 100644 index c9977d3d4ec640ae009c33b8d835ce4165faf4b8..0000000000000000000000000000000000000000 --- a/2124010070/experiment_07/exp7-5.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#include -#include -#include -#define N 30 -typedef struct -{ - char *value; //节点值 - int weight; //权重 - int parent; //父节点 - int left; - int right; -} HTNode; - -typedef struct -{ - char Code[15]; //用于存放当前节点的哈夫曼编码 - int start; //表示start~n为哈夫曼编码 -} HfmCode; - -void CreateHTree(HTNode ht[], int n) -{ - int i, k, lnode, rnode; - int first_min, second_min; - for (i = 0; i < 2 * n - 1; i++) - { - ht[i].parent = -1; - ht[i].left = -1; - ht[i].right = -1; - } - for (i = n; i < 2 * n - 1; i++) - { - first_min = second_min = 32767; - lnode = rnode = -1; - for (k = 0; k < i; k++) //在前i个元素中找两个最小的数 - { - if (ht[k].parent == -1) //根节点表示没有使用过的节点 - { - if (ht[k].weight < first_min) //小于最小的权,第一小的变为第二小,ht[k]变为最小 - { - second_min = first_min; - rnode = lnode; - first_min = ht[k].weight; - lnode = k; - } - else if (ht[k].weight < second_min) //小于第二小,此时ht[k]变为第二小的 - { - second_min = ht[k].weight; - rnode = k; - } - } - } - ht[i].weight = ht[lnode].weight + ht[rnode].weight; - ht[i].left = lnode; - ht[i].right = rnode; - ht[lnode].parent = i; - ht[rnode].parent = i; - } -} - -void CreateHcode(HTNode ht[], HfmCode hcd[], int n) -{ - int i, fu, current; - HfmCode hc; - for (i = 0; i < n; i++) - { - hc.start = n; - current = i; - fu = ht[i].parent; - while (fu != -1) - { - if (ht[fu].left == current) //判断当前节点为父节点的的左孩子还是右孩子 - hc.Code[hc.start--] = '0'; - else - hc.Code[hc.start--] = '1'; - current = fu; - fu = ht[fu].parent; - } - hc.start++; - hcd[i] = hc; - } -} - -int main(void) -{ - char words[15][15] = {"The", "of", "a", "to", "and", "in", "that", "he", "is", "at", "on", "for", "His", "are", "be"}; - int frequen[15] = {1192, 677, 541, 518, 462, 450, 242, 195, 190, 181, 174, 157, 138, 124, 123}; - - HTNode ht[29]; - for (int i = 0; i < 15; i++) - { - ht[i].value = words[i]; - ht[i].weight = frequen[i]; - } - CreateHTree(ht, 15); - HfmCode hcd[15]; - CreateHcode(ht, hcd, 15); - for (int i = 0; i < 15; i++) - { - printf("%s的编码为:", ht[i].value); - for (int j = hcd[i].start; j <= 15; j++) - { - printf("%c", hcd[i].Code[j]); - } - printf("\n"); - } - - //求平均查找长度 - double sum = 0; - for (int i = 0; i < 15; i++) - sum += frequen[i]; - double avgfind_len; - for (int i = 0; i < 15; i++) - { - avgfind_len += (15 - hcd[i].start + 1) * (frequen[i] / sum); - } - printf("平均查找长度为:%lf", avgfind_len); -} diff --git a/2124010070/experiment_07/exp7-7.cpp b/2124010070/experiment_07/exp7-7.cpp deleted file mode 100644 index 3b83597ae4c9a925a7991e6da1cbf0d1ce4ea954..0000000000000000000000000000000000000000 --- a/2124010070/experiment_07/exp7-7.cpp +++ /dev/null @@ -1,167 +0,0 @@ -#include -#include -#include -#include "BTree.cpp" - -void PreOrder_Path(BTNode *root, char path[], int path_len) -{ - int i; - if (root == NULL) - return; - - if (root->lchild == NULL && root->rchild == NULL) // root为叶子结点 - { - printf("%c到根结点逆路径: %c->", root->value, root->value); - for (i = path_len - 1; i > 0; i--) // 逆序输出 - printf("%c->", path[i]); - printf("%c\n", path[0]); // 输出根结点A - } - else - { - path[path_len] = root->value; // 将当前结点放入路径中 - path_len++; // 路径长度增1 - PreOrder_Path(root->lchild, path, path_len); // 递归扫描左子树 - PreOrder_Path(root->rchild, path, path_len); // 递归扫描右子树 - } -} - -void Longest_Path(BTNode *root, char path[], int path_len, char longest_path[], int &long_path_len) -{ - int i; - - if (root == NULL) - { - if (path_len > long_path_len) // 若当前路径更长,将路径保存在longest_path中 - { - for (i = path_len - 1; i >= 0; i--) // 逆序保存到longest_path - longest_path[i] = path[i]; - long_path_len = path_len; // 记录最长路径长度 - } - } - else - { - path[path_len] = root->value; // 将当前结点放入路径中 - path_len++; // 路径长度增1 - Longest_Path(root->lchild, path, path_len, longest_path, long_path_len); // 递归扫描左子树 - Longest_Path(root->rchild, path, path_len, longest_path, long_path_len); // 递归扫描右子树 - } -} - -void PostOrder(BTNode *root) -{ - BTNode *p, *r; - bool flag; - BTNode *stack[50]; - int top = -1; - p = root; // p->A - do - { - while (p != NULL) // 扫描结点p的所有左下结点并进栈 - { - stack[++top] = p; // 结点p进栈 - p = p->lchild; // 移动到左孩子 - } - r = NULL; // r指向刚刚访问的结点,初始时为空 - flag = true; // flag为真表示正在处理栈顶结点 - while (top != -1 && flag) // 栈不空且flag为真时循环 - { - p = stack[top]; // 取出当前的栈顶结点p - if (p->rchild == r) // 若结点p的右孩子为空或者为刚刚访问过的结点 - { - if (p->lchild == NULL && p->rchild == NULL) // 若为叶子结点 - { - // 输出栈中所有结点值 - printf(" %c到根结点逆路径: ", p->value); - for (int i = top; i > 0; i--) - printf("%c->", stack[i]->value); - // 输出根结点 - printf("%c\n", stack[0]->value); - } - top--; // 退栈 - r = p; // r指向刚访问过的结点 - } - else - { - p = p->rchild; // 转向处理其右子树 - flag = false; // 表示当前不是处理栈顶结点 - } - } - } while (top != -1); // 栈不空时循环 -} - -typedef struct -{ - BTNode *node; // 存放当前结点指针 - int parent; // 存放双亲结点在队列中的位置 -} QuNode; // 声明顺序队列结点类型 - -void LevelOrder(BTNode *root) -{ - int k; - BTNode *p; - QuNode queue[50]; - int front = -1, tail = -1; - QuNode e; - e.node = root; - e.parent = -1; - queue[++tail] = e; - while (front != tail) - { - e = queue[++front]; - p = e.node; - if (p->lchild == NULL && p->rchild == NULL) - { - k = front; - while (queue[k].parent != -1) - { - printf("%c->", queue[k].node->value); - k = queue[k].parent; - } - printf("%c\n", queue[k].node->value); - } - if (p->lchild != NULL) - { - e.node = p->lchild; - e.parent = front; - queue[++tail] = e; - } - if (p->rchild != NULL) - { - e.node = p->rchild; - e.parent = front; - queue[++tail] = e; - } - } -} - -int main(void) -{ - BTNode *b = NULL; - char tree[50] = "(A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"; - char *str = tree; - - printf("该二叉树的括号表示方法为:\n"); - CreateBTree(b, str); - DispBTree(b); - printf("\n"); - - printf("先序遍历的逆路径为:\n"); - char path[50]; - PreOrder_Path(b, path, 0); - - char longest_path[50]; - int longest_path_len = 0; - printf("最长的逆路径为:\n"); - Longest_Path(b, path, 0, longest_path, longest_path_len); - for (int i = longest_path_len - 1; i > 0; i--) - printf("%c->", longest_path[i]); - printf("%c\n", longest_path[0]); - - printf("后序非递归遍历方法:\n"); - PostOrder(b); - - printf("层次遍历方法:\n"); - LevelOrder(b); - - DestoryBTree(b); -} diff --git "a/2209020332/\347\254\254\344\270\203\347\253\240\344\275\234\344\270\232/LeetCode145.cpp" "b/2209020332/\347\254\254\344\270\203\347\253\240\344\275\234\344\270\232/LeetCode145.cpp" deleted file mode 100644 index 3de057714bdf4e176fbaeb82e01b8472edf7ab4b..0000000000000000000000000000000000000000 --- "a/2209020332/\347\254\254\344\270\203\347\253\240\344\275\234\344\270\232/LeetCode145.cpp" +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode() : val(0), left(nullptr), right(nullptr) {} - * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} - * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} - * }; - */ -class Solution { -public: -void pullpty(TreeNode *root,vertor &res) -{ - if(root == nullptr) - { - return; -} - pullpty(root->left,res); - pullpty(root->right,res); - res.push_back(root->val); -} - vector postorderTraversal(TreeNode* root) { - vector res; - pullpty(root,res); - return res; - } -}; \ No newline at end of file diff --git "a/2209020332/\347\254\254\344\270\211\347\253\240\344\275\234\344\270\232/LeetCode150.cpp" "b/2209020332/\347\254\254\344\270\211\347\253\240\344\275\234\344\270\232/LeetCode150.cpp" deleted file mode 100644 index f49b0fba2b8c819879f2be955d2db8ffcbe810d1..0000000000000000000000000000000000000000 --- "a/2209020332/\347\254\254\344\270\211\347\253\240\344\275\234\344\270\232/LeetCode150.cpp" +++ /dev/null @@ -1,32 +0,0 @@ -class Solution { -public: - int evalRPN(vector& tokens) { - stack st; - for (string s : tokens) { - if (s == "+") { - int a = st.top(); st.pop(); - int b = st.top(); st.pop(); - st.push(a+b); - } - else if (s == "-") { - int a = st.top(); st.pop(); - int b = st.top(); st.pop(); - st.push(b-a); - } - else if (s == "*") { - int a = st.top(); st.pop(); - int b = st.top(); st.pop(); - st.push(a*b); - } - else if (s == "/") { - int a = st.top(); st.pop(); - int b = st.top(); st.pop(); - st.push(b/a); - } - else { - st.push(stoi(s)); - } - } - return st.top(); - } -}; \ No newline at end of file diff --git "a/2209020332/\347\254\254\344\270\211\347\253\240\344\275\234\344\270\232/LeetCode20.cpp" "b/2209020332/\347\254\254\344\270\211\347\253\240\344\275\234\344\270\232/LeetCode20.cpp" deleted file mode 100644 index aaea12502cfc129744c28e371c20ffaa78d80737..0000000000000000000000000000000000000000 --- "a/2209020332/\347\254\254\344\270\211\347\253\240\344\275\234\344\270\232/LeetCode20.cpp" +++ /dev/null @@ -1,46 +0,0 @@ -class Solution { -public: - bool isValid(string s) { - stackstk; - if(s.empty()){ - return true; - } - for(auto c :s){ - if(c=='(' || c=='[' || c=='{'){ - stk.push(c); - } - else{ - if(stk.empty()){ - return false; - } - else{ - if(c==')'){ - if(stk.top()!='('){ - return false; - } - else{ - stk.pop(); - } - } - else if(c==']'){ - if(stk.top()!='['){ - return false; - } - else{ - stk.pop(); - } - } - else if(c=='}'){ - if(stk.top()!='{'){ - return false; - } - else{ - stk.pop(); - } - } - } - } - } - return stk.empty(); - } -}; \ No newline at end of file diff --git "a/2209020332/\347\254\254\344\270\211\347\253\240\344\275\234\344\270\232/LeetCode225.cpp" "b/2209020332/\347\254\254\344\270\211\347\253\240\344\275\234\344\270\232/LeetCode225.cpp" deleted file mode 100644 index c6844da481c102410ef3238bfe0fb9a8b301638d..0000000000000000000000000000000000000000 --- "a/2209020332/\347\254\254\344\270\211\347\253\240\344\275\234\344\270\232/LeetCode225.cpp" +++ /dev/null @@ -1,46 +0,0 @@ -class MyStack { -public: - queue que1; - queue que2; - - MyStack() { - - } - - void push(int x) { - que1.push(x); - } - - int pop() { - int size = que1.size(); - size--; - while(size--) { - que2.push(que1.front()); - que1.pop(); - } - int result = que1.front(); - que1.pop(); - que1 = que2; - while(!que2.empty()) { - que2.pop(); - } - return result; - } - - int top() { - return que1.back(); - } - - bool empty() { - return que1.empty(); - } -}; - -/** - * Your MyStack object will be instantiated and called as such: - * MyStack* obj = new MyStack(); - * obj->push(x); - * int param_2 = obj->pop(); - * int param_3 = obj->top(); - * bool param_4 = obj->empty(); - */ \ No newline at end of file diff --git "a/2209020332/\347\254\254\344\270\211\347\253\240\344\275\234\344\270\232/LeetCode232.cpp" "b/2209020332/\347\254\254\344\270\211\347\253\240\344\275\234\344\270\232/LeetCode232.cpp" deleted file mode 100644 index e7b04fac54dfa347137eb807d94dfe01b03edc95..0000000000000000000000000000000000000000 --- "a/2209020332/\347\254\254\344\270\211\347\253\240\344\275\234\344\270\232/LeetCode232.cpp" +++ /dev/null @@ -1,31 +0,0 @@ -class MyQueue { -private: - std::stack A, B; - -public: - MyQueue() {} - - void push(int x) { - A.push(x); - } - - int pop() { - int peek = this->peek(); - B.pop(); - return peek; - } - - int peek() { - if (!B.empty()) return B.top(); - if (A.empty()) return -1; - while (!A.empty()){ - B.push(A.top()), A.pop(); - } - int res = B.top(); - return res; - } - - bool empty() { - return A.empty() && B.empty(); - } -}; \ No newline at end of file diff --git "a/2209020332/\347\254\254\344\272\224\347\253\240\344\275\234\344\270\232/LeetCode234.cpp" "b/2209020332/\347\254\254\344\272\224\347\253\240\344\275\234\344\270\232/LeetCode234.cpp" deleted file mode 100644 index bae7359ebc60d69715c67f80f8ffaff979d73610..0000000000000000000000000000000000000000 --- "a/2209020332/\347\254\254\344\272\224\347\253\240\344\275\234\344\270\232/LeetCode234.cpp" +++ /dev/null @@ -1,13 +0,0 @@ -bool isPalindrome(struct ListNode* head) { - int a[5000], num = 0; - while (head != NULL) { - a[num++] = head->val; - head = head->next; - } - for (int i = 0, j = num - 1; i < j; ++i, --j) { - if (a[i] != a[j]) { - return false; - } - } - return true; -} \ No newline at end of file diff --git "a/2209020332/\347\254\254\344\272\224\347\253\240\344\275\234\344\270\232/LeetCode509.cpp" "b/2209020332/\347\254\254\344\272\224\347\253\240\344\275\234\344\270\232/LeetCode509.cpp" deleted file mode 100644 index 8bbf5c82565bbaaf88f72ac3cc3f62b3537d79ed..0000000000000000000000000000000000000000 --- "a/2209020332/\347\254\254\344\272\224\347\253\240\344\275\234\344\270\232/LeetCode509.cpp" +++ /dev/null @@ -1,11 +0,0 @@ -class Solution { -public: - int fib(int n) { - if(n == 0||n == 1) - return n; - else - { - return fib(n - 1)+ fib(n - 2); - } - } -}; \ No newline at end of file diff --git "a/2209020332/\347\254\254\345\205\255\347\253\240\344\275\234\344\270\232/LeetCode485.cpp" "b/2209020332/\347\254\254\345\205\255\347\253\240\344\275\234\344\270\232/LeetCode485.cpp" deleted file mode 100644 index 47b37d4ef39073c4d021fe3f75b2181331517694..0000000000000000000000000000000000000000 --- "a/2209020332/\347\254\254\345\205\255\347\253\240\344\275\234\344\270\232/LeetCode485.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { - public int findMaxConsecutiveOnes(int[] nums) { - int max = 0, count = 0; - int n = nums.size(); - for (int i = 0; i < n; i++) { - if (nums[i] == 1) { - count++; - } else { - max = Math.max(max, count); - count = 0; - } - } - max = Math.max(max, count); - return max; - } -} \ No newline at end of file diff --git "a/2209020332/\347\254\254\345\233\233\347\253\240\344\275\234\344\270\232/LeetCode125.cpp" "b/2209020332/\347\254\254\345\233\233\347\253\240\344\275\234\344\270\232/LeetCode125.cpp" deleted file mode 100644 index cc5730298585de025f42201be3e12048fc6a72ee..0000000000000000000000000000000000000000 --- "a/2209020332/\347\254\254\345\233\233\347\253\240\344\275\234\344\270\232/LeetCode125.cpp" +++ /dev/null @@ -1,21 +0,0 @@ -class Solution { -public: - bool isPalindrome(string s) { - string sgood; - for (char ch: s) { - if (isalnum(ch)) { - sgood += tolower(ch); - } - } - int n = sgood.size(); - int left = 0, right = n - 1; - while (left < right) { - if (sgood[left] != sgood[right]) { - return false; - } - ++left; - --right; - } - return true; - } -}; \ No newline at end of file diff --git "a/2209040001/chapter_7/\344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.cpp" "b/2209040001/chapter_7/\344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.cpp" deleted file mode 100644 index 35b03ba148aed7202e0ec01278104ed5f8f8b2e2..0000000000000000000000000000000000000000 --- "a/2209040001/chapter_7/\344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - vector treeNode; - vector postorderTraversal(TreeNode* root) { - postOrder(root); - return treeNode; - } - void postOrder(TreeNode* root) { - if(root == nullptr) { - return; - } - postOrder(root->left); - postOrder(root->right); - treeNode.push_back(root->val); - } -}; \ No newline at end of file diff --git "a/2209040001/chapter_7/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.cpp" "b/2209040001/chapter_7/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.cpp" deleted file mode 100644 index 971ae4aec41ddedce172952630fc835a06d66d62..0000000000000000000000000000000000000000 --- "a/2209040001/chapter_7/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.cpp" +++ /dev/null @@ -1,27 +0,0 @@ -class Solution { -public: - vector> levelOrder(TreeNode* root) { - vector > result; - queue q; - if(root) { - q.push(root); - } - while(!q.empty()) { - int size = q.size(); - vector v; - for(int i = 0; i < size; i++) { - TreeNode* node = q.front(); - q.pop(); - v.push_back(node->val); - if(node->left) { - q.push(node->left); - } - if(node->right) { - q.push(node->right); - } - } - result.push_back(v); - } - return result; - } -}; \ No newline at end of file diff --git "a/2209040001/chapter_7/\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.cpp" "b/2209040001/chapter_7/\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.cpp" deleted file mode 100644 index 4c207b1798cfeca66b592b35963f92379297a892..0000000000000000000000000000000000000000 --- "a/2209040001/chapter_7/\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.cpp" +++ /dev/null @@ -1,21 +0,0 @@ -class Solution { -public: - bool isSymmetric(TreeNode* root) { - if(!root) { - return true; - } - return symm(root->left, root->right); - } - bool symm(TreeNode* left, TreeNode* right) { - if(left == nullptr && right == nullptr) { - return true; - } - if(left == nullptr || right == nullptr) { - return false; - } - if(left->val != right->val) { - return false; - } - return symm(left->left, right->right) && symm(left->right, right->left); - } -}; \ No newline at end of file diff --git "a/2209040008/chapter1/\344\270\212\346\234\272\345\256\236\351\252\214\347\254\254\344\270\200\351\242\230.cpp" "b/2209040008/chapter1/\344\270\212\346\234\272\345\256\236\351\252\214\347\254\254\344\270\200\351\242\230.cpp" deleted file mode 100644 index b98fa61de5d288db3493c894cc8739e2054a03ae..0000000000000000000000000000000000000000 --- "a/2209040008/chapter1/\344\270\212\346\234\272\345\256\236\351\252\214\347\254\254\344\270\200\351\242\230.cpp" +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include -#include -long add(long n) -{ - long i,sum=0; - for(i=1;i<=n;i++) - { - sum+=i; - } - return sum; -} - -void addtime(long n) -{ - clock_t t; - long sum; - t=clock(); - sum=add(n); - t=clock()-t; - printf("结果:1~%d之和:%ld\n",n,sum); - printf("用时:%lf秒\n",((float)t)/CLOCKS_PER_SEC); -} -int main() - { - int n; - scanf("%d",&n); - if(n<1000000)return 0; - addtime(n); - return 1; - } \ No newline at end of file diff --git "a/2209040008/chapter1/\344\270\212\346\234\272\345\256\236\351\252\214\347\254\254\344\272\214\351\242\230.cpp" "b/2209040008/chapter1/\344\270\212\346\234\272\345\256\236\351\252\214\347\254\254\344\272\214\351\242\230.cpp" deleted file mode 100644 index a616ce1816c947f8786ec7c2df240d21f948c369..0000000000000000000000000000000000000000 --- "a/2209040008/chapter1/\344\270\212\346\234\272\345\256\236\351\252\214\347\254\254\344\272\214\351\242\230.cpp" +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -#include -bool prime(long n) -{ - long i; - for(i=2;inext==NULL) - { - return head; - } - struct ListNode* p=head->next;//保存结点 - head->next=swapPairs(p->next);//使用递归 - p->next=head; - return p; -} \ No newline at end of file diff --git "a/2209040008/chapter2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" "b/2209040008/chapter2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" deleted file mode 100644 index afefcbbd2824dd2c75d2566e83aea78edbae54b6..0000000000000000000000000000000000000000 --- "a/2209040008/chapter2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" +++ /dev/null @@ -1,24 +0,0 @@ -struct ListNode* deleteDuplicates(struct ListNode* head) { - if(head==NULL||head->next==NULL) - { - return head; - } - struct ListNode* p=head; - if(p->val!=p->next->val)//判断结点值与后续结点值是否相等 - { - p->next=deleteDuplicates(p->next);//递归 - return p; - } - while(p && p->next)//循环 - { - if(p->val==p->next->val) - { - p=p->next; - } - else - { - break; - } - } - return deleteDuplicates(p->next); -} \ No newline at end of file diff --git "a/2209040009/\344\275\234\344\270\232/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.cpp" "b/2209040009/\344\275\234\344\270\232/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.cpp" deleted file mode 100644 index 63eaea7e74efef79da1054de4f82634932ff3bc0..0000000000000000000000000000000000000000 --- "a/2209040009/\344\275\234\344\270\232/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.cpp" +++ /dev/null @@ -1,46 +0,0 @@ -void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n) -{ - int a[m+n]; //创造一个a数组,以空间换时间 - int i=0,j=0,k=0; - for (i=0;i=0) - { - i=0; - h=0; - k=0; - while(h+point<=arrSize&&i<=arrSize-1) - { - total+=arr[i]; //对符合条件情况进行累加 - // printf("%d# ",total); - i++; - k++; - // printf("%d@ ",k); - if (k==point) - { - h++; //同一个长度的不同形式累加 - // printf("%d &",h); - i=h; - k=0; - //break; - } - - } - // printf("%d ",total); - point-=2; - } - return total; -} \ No newline at end of file diff --git "a/2209040009/\344\275\234\344\270\232/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" "b/2209040009/\344\275\234\344\270\232/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" deleted file mode 100644 index e4bbbd5ac31836f2ced271a6b8586d174279cd25..0000000000000000000000000000000000000000 --- "a/2209040009/\344\275\234\344\270\232/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * struct ListNode *next; - * }; - */ -struct ListNode* removeElements(struct ListNode* head, int val) { - while (head!=NULL&&head->val==val) - { - head=head->next; - } - struct ListNode* tip=head; - struct ListNode* move=head; //不是move=head->next,不然会访问空指针的成员变量 - while (move!=NULL) - { - if (move->val==val) - { - tip->next=move->next; - } - else - { - tip=move; - } - move=move->next; - } - return head; -} diff --git "a/2209040009/\344\275\234\344\270\232/\347\277\273\350\275\254\351\223\276\350\241\2502.cpp" "b/2209040009/\344\275\234\344\270\232/\347\277\273\350\275\254\351\223\276\350\241\2502.cpp" deleted file mode 100644 index 2cde51617c9e9cba1cb4d6711f9a1a31193a81d2..0000000000000000000000000000000000000000 --- "a/2209040009/\344\275\234\344\270\232/\347\277\273\350\275\254\351\223\276\350\241\2502.cpp" +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * struct ListNode *next; - * }; - */ - struct ListNode* successor; - struct ListNode* reverse(struct ListNode* N,int n) -{ -if (n==1) -{ - successor=N->next; //防止边界情况时把指针断开 - return N; - } - struct ListNode* last=reverse(N->next,n-1); - N->next->next=N; - N->next=successor; //链接新节点时断开旧节点 - return last; -} -struct ListNode* reverseBetween(struct ListNode* head, int left, int right) { - if(left==1) - { - return reverse(head,right); - } - head->next=reverseBetween(head->next,left-1,right-1); //递归找到要翻转的链表 - return head; - - -} \ No newline at end of file diff --git "a/2209040014/chapter2/lc_24\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" "b/2209040014/chapter2/lc_24\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" deleted file mode 100644 index a9800a92ed42d89006e04484b61717ff0fe3d396..0000000000000000000000000000000000000000 --- "a/2209040014/chapter2/lc_24\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" +++ /dev/null @@ -1,9 +0,0 @@ -struct ListNode* swapPairs(struct ListNode* head) { - if (head == NULL || head->next == NULL) { - return head; - } - struct ListNode* newHead = head->next; - head->next = swapPairs(newHead->next); - newHead->next = head; - return newHead; -} diff --git "a/2209040015/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/chapter3/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" "b/2209040015/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/chapter3/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" deleted file mode 100644 index f86be065c0b18808cc98bd78a58550e7d9353a0e..0000000000000000000000000000000000000000 --- "a/2209040015/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/chapter3/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,24 +0,0 @@ -#include -int main() -{ - int i, j, m, n; - scanf("%d", &m); - double a[m + 1]; - for (i = 0; i <= m; i++) - scanf("%lf", &a[i]); - scanf("%d", &n); - double b[n + 1]; - for (i = 0; i <= n; i++) - scanf("%lf", &b[i]); - double c[m + n + 1]; - for (i = 0; i <= m + n; i++) - c[i] = 0; - for (i = 0; i <= m; i++) - for (j = 0; j <= n; j++) - c[i + j] += a[i] * b[j]; - printf("%f*x^%d", c[0], 0); - for (i = 1; i <= m + n; i++) - printf(" + %f*x^%d", c[i], i); - - return 0; -} diff --git "a/2209040015/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/chapter3/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\350\277\220\347\256\227.cpp" "b/2209040015/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/chapter3/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\350\277\220\347\256\227.cpp" deleted file mode 100644 index 13aa859f73482f4a8cbf529616adbeefcb105fd8..0000000000000000000000000000000000000000 --- "a/2209040015/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/chapter3/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,128 +0,0 @@ -#include -#include -typedef char ElemType; -typedef struct LNode -{ - ElemType data; - struct LNode *next; -}LinkNode; -void InitList(LinkNode *&L) -{ - L=(LinkNode *)malloc(sizeof(LinkNode)); - L->next=NULL; -} -bool ListInsert(LinkNode *&L,int i,ElemType e) -{ - int j=0; - LinkNode *p=L,*s; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - s=(LinkNode *)malloc(sizeof(LinkNode)); - s->data=e; - s->next=p->next; - p->next=s; - return true; - } -} -void DispList(LinkNode *L)/) -{ - LinkNode *p=L->next; - while(p!=NULL) - { - printf("%c",p->data); - p=p->next; - } - printf("\n"); -} -int ListLength(LinkNode *L) -{ - int n=0; - LinkNode *p=L; - while (p->next!=NULL) - { - n++; - p=p->next; - } - return (n); -} -bool ListEmpty(LinkNode *L) -{ - return (L->next==NULL); -} -bool GetElem(LinkNode *L,int i,ElemType &e) -{ - int j=0; - LinkNode *p=L; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - e=p->data; - return true; - } -} -int LocateElem(LinkNode *L,ElemType e) -{ - int i=1; - LinkNode *p=L->next; - while(p!=NULL&&p->data!=e) - { - p=p->next; - i++; - } - if(p==NULL) - return (0); - else - return (i); -} -bool ListDelete(LinkNode *&L,int i,ElemType &e) -{ - int j=0; - LinkNode *p=L,*q; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - q=p->next; - if(q==NULL) - return false; - e=q->data; - p->next=q->next; - free(q); - return true; - } -} -void DestroyList(LinkNode *&L) -{ - LinkNode *pre=L,*p=L->next; - while(p!=NULL) - { - free(pre); - pre=p; - p=pre->next; - } - free(pre); -} diff --git "a/2209040015/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/chapter3/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" "b/2209040015/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/chapter3/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" deleted file mode 100644 index e6b5827f3466fb7f6d35ee1c58df4428cf19a183..0000000000000000000000000000000000000000 --- "a/2209040015/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/chapter3/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" +++ /dev/null @@ -1,26 +0,0 @@ -bool DivideList(LinkNode*& L, ElemType x) - if (L->next==NULL) { - printf("单链表不存在。\n"); - return false; - } - LinkNode* p = NULL, * q = NULL, * r = NULL; - p = L; - while (!q) { - q = (LinkNode*)malloc(sizeof(LinkNode)); - } - r = q; - while (p->next) { - if (p->next->data < x) { - r->next = p->next; - r=r->next; - p->next= p->next->next; - r->next = NULL; - } - else - p = p->next; - } - r->next = L->next; - L->next = q->next; - free(q); - return true; -} diff --git "a/2209040018/\347\254\254\344\272\214\347\253\240\344\275\234\344\270\232/\345\210\240\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240" "b/2209040018/\347\254\254\344\272\214\347\253\240\344\275\234\344\270\232/\345\210\240\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240" deleted file mode 100644 index de9c01d54d2433e31a3bd5374a7b99d00f8c41f3..0000000000000000000000000000000000000000 --- "a/2209040018/\347\254\254\344\272\214\347\253\240\344\275\234\344\270\232/\345\210\240\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240" +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode() : val(0), next(nullptr) {} - * ListNode(int x) : val(x), next(nullptr) {} - * ListNode(int x, ListNode *next) : val(x), next(next) {} - * }; - */ -class Solution { -public: - ListNode* removeElements(ListNode* head, int val) { - if(head==nullptr) return nullptr; - ListNode ret(0,head),*p=&ret; - while(p && p->next){ - if(p->next->val==val){ - p->next=p->next->next; - }else{ - p=p->next; - } - } - return ret.next; - } -} \ No newline at end of file diff --git "a/2209040018/\347\254\254\344\272\214\347\253\240\344\275\234\344\270\232/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204" "b/2209040018/\347\254\254\344\272\214\347\253\240\344\275\234\344\270\232/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204" deleted file mode 100644 index c8828300d9979c355e961f2a9178c4ff3b6161df..0000000000000000000000000000000000000000 --- "a/2209040018/\347\254\254\344\272\214\347\253\240\344\275\234\344\270\232/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - void merge(vector& nums1, int m, vector& nums2, int n) { - int p1 = m - 1; - int p2 = n - 1; - int p = m + n; - while(p2>0) - { - if(p1 >= 0 && nums1[p1]>nums2[p2]){ - nums1[p--]=nums1[p1--] - } - else{ - nums[p--] = nums[p2--]; - } - } - } -}; \ No newline at end of file diff --git "a/2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/p118\351\241\265\351\253\230\346\225\210\346\261\202\350\247\243x\347\232\204n\346\254\241\346\226\271.cpp" "b/2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/p118\351\241\265\351\253\230\346\225\210\346\261\202\350\247\243x\347\232\204n\346\254\241\346\226\271.cpp" deleted file mode 100644 index 7681b174f4c2cd8cd9a3c8a9f7ae2ceff19cb542..0000000000000000000000000000000000000000 --- "a/2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/p118\351\241\265\351\253\230\346\225\210\346\261\202\350\247\243x\347\232\204n\346\254\241\346\226\271.cpp" +++ /dev/null @@ -1,19 +0,0 @@ -#include - double expx(double x,int n) - { - if(n == 1) - return x; - else if(n%2==0) - return expx(x,n/2)*expx(x,n/2); - else return x*expx(x,(n-1)/2)*expx(x,(n-1)/2); - } - int main(){ - double x; - int n; - printf(" x:"); - scanf("%lf",&x); - printf("n:"); - scanf("%d",&n); - printf("%g的%d次方:%g\n",x,n,expx(x,n)); - return 1; - } \ No newline at end of file diff --git "a/2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/p144\351\241\265\344\272\214\345\217\211\346\240\221\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/p144\351\241\265\344\272\214\345\217\211\346\240\221\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" deleted file mode 100644 index e8d9ec084ededad4b1392f552db65728be439961..0000000000000000000000000000000000000000 --- "a/2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/p144\351\241\265\344\272\214\345\217\211\346\240\221\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include -# define MaxSize 100 -typedef char ElemType; -typedef struct node{ - ElemType data; - struct node *lchild; - struct node *rchild; -}BTNode; -void CreateBtree(BTNode *&b,char *str){ - BTNode *St[MaxSize],*p; - int top=-1,k,j=0;char ch; - b=NULL; - ch=str[j]; - -while(ch!='\0'){ - switch(ch){ - case '(':top++;St[top]=p;k=1;break; - case ')':top--;break; - case ',':k=2;break; - default:p=(BTNode *)malloc(sizeof(BTNode)); - p->data=ch;p-lchild=p->rchild=NULL; - if(b==NULL) - b=p; - else - { - switch(k){ - case 1:St[top]->lchild=p;break; - case 2:St[top]->rchild=p;break; - } - } - } - j++; - ch=str[j]; -} -} \ No newline at end of file diff --git "a/2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/p64\351\241\265\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\350\277\220\347\256\227.cpp" "b/2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/p64\351\241\265\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\350\277\220\347\256\227.cpp" deleted file mode 100644 index 16972f8a9fb6dd60636b493f5a368e66524e45e4..0000000000000000000000000000000000000000 --- "a/2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/p64\351\241\265\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,71 +0,0 @@ -#include -#include -#define MaxSize 5 -typedef char ElemType; -typedef struct -{ - ElemType data[MaxSize]; - int front,rear; //队首和队尾指针 -} SqQueue; -void InitQueue(SqQueue *&q) //初始化队列 -{ q=(SqQueue *)malloc (sizeof(SqQueue)); - q->front=q->rear=0; -} -void DestroyQueue(SqQueue *&q) //销毁队列 -{ - free(q); -} -bool QueueEmpty(SqQueue *q) //判断队列空 -{ - return(q->front==q->rear); -} -bool enQueue(SqQueue *&q,ElemType e) //进队 -{ - if ((q->rear+1)%MaxSize==q->front) //队满上溢出 - return false; - q->rear=(q->rear+1)%MaxSize; - q->data[q->rear]=e; - return true; -} -bool deQueue(SqQueue *&q,ElemType &e) //出队 -{ - if (q->front==q->rear) //队空下溢出 - return false; - q->front=(q->front+1)%MaxSize; - e=q->data[q->front]; - return true; -} -extern void InitQueue(SqQueue *&q); -extern void DestroyQueue(SqQueue *&q); -extern bool QueueEmpty(SqQueue *q); -extern bool enQueue(SqQueue *&q,ElemType e); -extern bool deQueue(SqQueue *&q,ElemType &e); -int main() -{ - ElemType e; - SqQueue *q; - printf("环形队列基本运算如下:\n"); - printf(" (1)初始化队列q\n"); - InitQueue(q); - printf(" (2)依次进队列元素a,b,c\n"); - if (!enQueue(q,'a')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'b')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'c')) printf("\t提示:队满,不能进队\n"); - printf(" (3)队列为%s\n",(QueueEmpty(q)?"空":"非空")); - if (deQueue(q,e)==0) - printf("队空,不能出队\n"); - else - printf(" (4)出队一个元素%c\n",e); - printf(" (5)依次进队列元素d,e,f\n"); - if (!enQueue(q,'d')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'e')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'f')) printf("\t提示:队满,不能进队\n"); - printf(" (6)出队列序列:"); - while (!QueueEmpty(q)) - { deQueue(q,e); - printf("%c ",e); - } - printf("\n"); - printf(" (7)释放队列\n"); - DestroyQueue(q); -} \ No newline at end of file diff --git "a/2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/p59\351\241\265\345\256\236\351\252\2143.1.1\351\242\230\351\241\272\345\272\217\346\240\210.cpp" "b/2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/\345\256\236\351\252\2143.1.1\351\242\230\351\241\272\345\272\217\346\240\210.cpp" similarity index 99% rename from "2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/p59\351\241\265\345\256\236\351\252\2143.1.1\351\242\230\351\241\272\345\272\217\346\240\210.cpp" rename to "2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/\345\256\236\351\252\2143.1.1\351\242\230\351\241\272\345\272\217\346\240\210.cpp" index 871276cd8ac97d77ede75a51ea97f395ea39cf23..bf0921b45db15b042400babe55fefdec3c936bd4 100644 --- "a/2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/p59\351\241\265\345\256\236\351\252\2143.1.1\351\242\230\351\241\272\345\272\217\346\240\210.cpp" +++ "b/2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/\345\256\236\351\252\2143.1.1\351\242\230\351\241\272\345\272\217\346\240\210.cpp" @@ -18,7 +18,7 @@ void DestroyStack(SqStack *&s){ bool StackEmpty(SqStack *s){ return(s->top==-1); -} +} bool Push(SqStack *&s,ElemType e) { diff --git "a/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\270\200\347\253\240\347\254\254\344\270\211\351\242\230\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\270\200\347\253\240\347\254\254\344\270\211\351\242\230\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index 345c1940cc896e189af5e5720ae6bc34c2bdeff6..0000000000000000000000000000000000000000 --- "a/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\270\200\347\253\240\347\254\254\344\270\211\351\242\230\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include - int main() -{ - int a, b, c, i, k = 0, d; - scanf("%d %d", &a, &b); - for (c = a; c <= b; c++) -{ - d = 1; - for (i = 2; i <= sqrt(c); i++) - if (c % i == 0) { - d = 0; - break; - } - if (d == 1) - k++; - if (c == b) - printf("%d", k); - -} - -} \ No newline at end of file diff --git "a/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\270\211\347\253\240\347\254\254\344\270\200\351\242\230.cpp" "b/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\270\211\347\253\240\347\254\254\344\270\200\351\242\230.cpp" deleted file mode 100644 index 500891fbd2b2ba1fe0f1e26a5b8751c7412fd908..0000000000000000000000000000000000000000 --- "a/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\270\211\347\253\240\347\254\254\344\270\200\351\242\230.cpp" +++ /dev/null @@ -1,8 +0,0 @@ -//顺序表的定义 -#define MAXLEN 100 -typedef int DataType; -typedef struct -{ - DataType data[MAXLEN]; - int Length; -}SeqList; \ No newline at end of file diff --git "a/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\270\211\347\253\240\347\254\254\344\270\211\351\242\230.cpp" "b/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\270\211\347\253\240\347\254\254\344\270\211\351\242\230.cpp" deleted file mode 100644 index 6cae15e7153859ee59840257cbea2c9c65eded45..0000000000000000000000000000000000000000 --- "a/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\270\211\347\253\240\347\254\254\344\270\211\351\242\230.cpp" +++ /dev/null @@ -1,102 +0,0 @@ -#include "stdio.h" -#include "stdlib.h" - #define LEN 10 -typedef struct ring_buff{ - int array[LEN]; - int W; - int R; -}*ring; -struct ring_buff * fifo_init(void) -{ - struct ring_buff * p = NULL; - p = (struct ring_buff *)malloc(sizeof(struct ring_buff)); - if(p == NULL) - { - printf("fifo_init malloc error\n"); - return NULL; - } - p->W = 0; - p->R = 0; - return p; -} -int get_ring_buff_fullstate(struct ring_buff * p_ring_buff) -{ - if((p_ring_buff->W - p_ring_buff->R) == LEN) - { - return (1); - } - else - { - return (0); - } -} -int get_ring_buff_emptystate(struct ring_buff * p_ring_buff) -{ - if(p_ring_buff->W == p_ring_buff->R) - { - return (1); - } - else - { - return (0); - } -} -int ring_buff_insert(struct ring_buff * p_ring_buff,int data) -{ - if(p_ring_buff == NULL) - { - printf("p null\n"); - return (-1); - } - - if(get_ring_buff_fullstate(p_ring_buff) == 1) - { - printf("buff is full\n"); - return (-2); - } - p_ring_buff->array[p_ring_buff->W%LEN] = data; - p_ring_buff->W ++; - return (0); -} -int ring_buff_get(struct ring_buff * p_ring_buff) -{ - int data = 0; - if(p_ring_buff == NULL) - { - printf("p null\n"); - return (-1); - } - if(get_ring_buff_emptystate(p_ring_buff) == 1) - { - printf("buff is empty\n"); - return (-2); - } - data = p_ring_buff->array[p_ring_buff->R%LEN]; - p_ring_buff->R++; - return data; -} -int ring_buff_destory(struct ring_buff * p_ring_buff) -{ - if(p_ring_buff == NULL) - { - printf("p null\n"); - return (-1); - } - free(p_ring_buff); - return (0); -} -int main() -{ - int i = 0; - ring pt_ring_buff = fifo_init(); - for(i = 0;i<10;i++) - { - ring_buff_insert(pt_ring_buff,i); - } - for(i = 0;i<10;i++) - { - printf("%d ",ring_buff_get(pt_ring_buff)); - } - ring_buff_destory(pt_ring_buff); - return (1); -} \ No newline at end of file diff --git "a/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\270\211\347\253\240\347\254\254\345\205\253\351\242\230.cpp" "b/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\270\211\347\253\240\347\254\254\345\205\253\351\242\230.cpp" deleted file mode 100644 index 896cfe0a13d1334b25ba18abf69e88065e416ca5..0000000000000000000000000000000000000000 --- "a/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\270\211\347\253\240\347\254\254\345\205\253\351\242\230.cpp" +++ /dev/null @@ -1,119 +0,0 @@ -#include -#include -#include -using namespace std; -struct Queen -{ - int x; - int y; -}; -typedef struct SeqStack -{ - Queen* Base; - Queen* Top; - int stacksize; -}; -void InitStack(SeqStack& S,int N) -{ - S.Base = new Queen[N+1]; - if (!S.Base) exit(0); - S.Top = S.Base; - S.stacksize = N+1; -} -bool Check(SeqStack S,Queen *Q) -{ - Queen* p; - p = S.Base; - while (p < S.Top) - { - if (Q->x - p->x == abs(Q->y - p->y) || Q->y == p->y) - { - return false; - break; - } - p++; - } - return true; -} -void StackTraverse(SeqStack S) -{ - Queen* p; - p = S.Base; - while (p < S.Top) - { - cout << p->x << " " << p->y << endl; - p++; - } - cout << endl; -} -void Push(SeqStack& S, Queen* Q) -{ - S.Top->x = Q->x; - S.Top->y = Q->y; - S.Top++; -} -void Pop(SeqStack& S, Queen*& Q) -{ - --S.Top; - Q->x = S.Top->x; - Q->y = S.Top->y; -} -void Create(SeqStack &S,int N) -{ - bool flag; - Queen* p=new Queen; - int total = 0; - p->x = 1; - p->y = 1; - Push(S, p); - p->x = 2; - p->y = 1; - while (p->x <= N && p->y <= N) - { - while (p->y <= N) - { - flag = Check(S, p); - if (flag) break; - p->y++; - } - if (flag) - { - Push(S, p); - p->x++; - p->y = 1; - if (p->x > N) - { - StackTraverse(S); - total++; - Pop(S, p); - p->y++; - while (p->y > N && S.Base != S.Top) - { - Pop(S, p); - p->y++; - } - } - } - else - { - Pop(S, p); - p->y++; - while (p->y > N && S.Base != S.Top) - { - Pop(S, p); - p->y++; - } - } - } - cout <<"解个数:" << total; -} -int main() -{ - int N; - cin >> N; - SeqStack S; - InitStack(S, N); - cout << "皇后坐标:"< -#include -typedef char ElemType; -typedef struct LNode -{ - ElemType data; - struct LNode *next; -}LinkNode; -void InitList(LinkNode *&L)//初始化单链表 1) -{ - L=(LinkNode *)malloc(sizeof(LinkNode)); - L->next=NULL; -} -bool ListInsert(LinkNode *&L,int i,ElemType e)//插入数据元素 2) -{ - int j=0; - LinkNode *p=L,*s; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - s=(LinkNode *)malloc(sizeof(LinkNode)); - s->data=e; - s->next=p->next; - p->next=s; - return true; - } -} -void DispList(LinkNode *L)//输出线性表 3) 9) 11) -{ - LinkNode *p=L->next; - while(p!=NULL) - { - printf("%c",p->data); - p=p->next; - } - printf("\n"); -} -int ListLength(LinkNode *L)//求线性表的长度 4) -{ - int n=0; - LinkNode *p=L; - while (p->next!=NULL) - { - n++; - p=p->next; - } - return (n); -} -bool ListEmpty(LinkNode *L)//判断单链表是否为空5) -{ - return (L->next==NULL); -} -bool GetElem(LinkNode *L,int i,ElemType &e)//某个数据元素值6) -{ - int j=0; - LinkNode *p=L; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - e=p->data; - return true; - } -} -int LocateElem(LinkNode *L,ElemType e)//元素的位置7) -{ - int i=1; - LinkNode *p=L->next; - while(p!=NULL&&p->data!=e) - { - p=p->next; - i++; - } - if(p==NULL) - return (0); - else - return (i); -} -bool ListDelete(LinkNode *&L,int i,ElemType &e) -{ - int j=0; - LinkNode *p=L,*q; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - q=p->next; - if(q==NULL) - return false; - e=q->data; - p->next=q->next; - free(q); - return true; - } -} -void DestroyList(LinkNode *&L)//释放单链表12) -{ - LinkNode *pre=L,*p=L->next; - while(p!=NULL) - { - free(pre); - pre=p; - p=pre->next; - } - free(pre); -} \ No newline at end of file diff --git "a/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\272\214\347\253\240\347\254\254\345\205\255\351\242\230\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" "b/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\272\214\347\253\240\347\254\254\345\205\255\351\242\230\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" deleted file mode 100644 index afadc56f70c64cc430e8094248655c81c35ec09f..0000000000000000000000000000000000000000 --- "a/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\272\214\347\253\240\347\254\254\345\205\255\351\242\230\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include - -typedef char ElemType; - -typedef struct LNode { - ElemType data; - struct LNode *next; -} LinkNode; - -/** - * 尾插法建立单链表:裁判实现 - */ -void CreateListR(LinkNode *&L, ElemType a[], int n); - -/** - * 输出线性表: 每个数据后面一个空格:裁判实现 - */ -void DispList(LinkNode *L); - -/** - * 将L中所有数据结点按x进行划分。 - * 所有小于x的结点排在大于等于x的结点前面。 - * 必须保证两个分区中每个节点的初始相对位置。 - */ -void Split(LinkNode *&L, ElemType x); - -int main() -{ - int n; - scanf("%d", &n); - ElemType a[n]; - for (int i = 0; i < n; ++i) { - scanf("%d", &a[i]); - } - - LinkNode *L; - CreateListR(L, a, n); - printf("L:"); DispList(L); - ElemType x; - scanf("%d", &x); - printf("以%d进行划分\n", x); - Split(L, x); - printf("L:"); DispList(L); - return 0; -} \ No newline at end of file diff --git "a/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\272\214\347\253\240\347\254\254\345\215\201\351\242\230\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" "b/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\272\214\347\253\240\347\254\254\345\215\201\351\242\230\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" deleted file mode 100644 index a18a5e5ca166d58691c0bd87e2d8d98a1c628bf0..0000000000000000000000000000000000000000 --- "a/2209040021/\344\270\212\346\234\272\345\256\236\351\252\214\344\275\234\344\270\232/\347\254\254\344\272\214\347\253\240\347\254\254\345\215\201\351\242\230\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,114 +0,0 @@ -#include -#include - struct term { - float coe; - float exp; - struct term *next; -}; -struct term* allocate(size_t size) { - struct term* ptr = (struct term*) malloc (size); - if (ptr == NULL) { - fprintf(stderr, "内存分配失败"); - exit(EXIT_FAILURE); - } - return ptr; -} -struct term* createterm (float coe, float exp) { - struct term* newterm = allocate (sizeof (struct term)); - newterm->coe = coe; - newterm->exp = exp; - newterm->next = NULL; - return newterm; -} -void insertterm (struct term** head, struct term* newterm) { - if (*head == NULL || (*head)->exp < newterm->exp) { - newterm->next = *head; - *head = newterm; - } else { - struct term* current = *head; - while (current->next != NULL && current->next->exp > newterm->exp) { - current = current->next; - } - newterm->next = current->next; - current->next = newterm; - } -} -void print (struct term* head) { - struct term* current = head; - while (current != NULL) { - printf("%.2fX^%.2f", current->coe, current->exp); - current = current->next; - if (current != NULL) { - printf(" + "); - } - } - printf("\n"); -} -struct term* add (struct term* p1, struct term* p2) { - struct term* head = NULL; - while (p1 != NULL && p2 != NULL) { - if (p1->exp > p2->exp) { - insertterm(&head, createterm(p1->coe, p1->exp)); - p1 = p1->next; - } else if (p1->exp < p2->exp) { - insertterm(&head, createterm(p2->coe, p2->exp)); - p2 = p2->next; - } else { - float sum = p1->coe + p2->coe; - if (sum != 0) { - insertterm(&head, createterm(sum, p1->exp)); - } - p1 = p1->next; - p2 = p2->next; - } - } - while (p1 != NULL) { - insertterm(&head, createterm(p1->coe, p1->exp)); - p1 = p1->next; - } - while (p2 != NULL) { - insertterm(&head, createterm(p2->coe, p2->exp)); - p2 = p2->next; - } - return head; -} -void freelist(struct term* head) { - struct term* current = head; - while (current != NULL) { - struct term* temp = current; - current = current->next; - free(temp); - } -} - int main() { - struct term* p1 = NULL; - printf("请输入多项式1的项数:"); - int n1; - scanf("%d", &n1); - for (int i =0; i < n1; i++) { - float coe, exp; - printf("请输入第%d项的系数和指数(空格分开):",i+1); - scanf("%f %f",&coe, &exp); - insertterm(&p1, createterm(coe,exp)); - } - struct term* p2 = NULL; - printf("请输入多项式2的项数:"); - int n2; - scanf("%d", &n2); - for (int i =0; i < n2; i++) { - float coe, exp; - printf("请输入第%d项的系数和指数(空格分开):",i+1); - scanf("%f %f",&coe, &exp); - insertterm(&p2, createterm(coe,exp)); - } - struct term* result = add(p1,p2); - printf("多项式1:"); - print(p1); - printf("多项式2:"); - print(p2); - printf("相加结果:"); - print(result); - freelist(p1); - freelist(p2); - freelist(result); -} \ No newline at end of file diff --git "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\22077\351\241\265\347\254\25412\351\242\230.cpp" "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\22077\351\241\265\347\254\25412\351\242\230.cpp" deleted file mode 100644 index 37ea32d081d57cd6951ecc323f58a3fd895bf32d..0000000000000000000000000000000000000000 --- "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\22077\351\241\265\347\254\25412\351\242\230.cpp" +++ /dev/null @@ -1,24 +0,0 @@ -struct ListNode* reverseBetween(struct ListNode* head, int left, int right) { - if (NULL == head || NULL == head->next) { - return head; - } - struct ListNode *dumbNode = (struct ListNode*)malloc(sizeof(struct ListNode)); - dumbNode->val = -1; - dumbNode->next = head; - struct ListNode *cur = dumbNode; - struct ListNode *pre = dumbNode; - struct ListNode *next = NULL; - int i = 0; - for (i = 0; i < left - 1; i++) { - pre = pre->next; - } - cur = pre->next; - next = cur->next; - for (i = 0; i < right - left; i++) { - next = cur->next; - cur->next = next->next; - next->next = pre->next; - pre->next = next; - } - return dumbNode->next; -} \ No newline at end of file diff --git "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\22077\351\241\265\347\254\25415\351\242\230.cpp" "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\22077\351\241\265\347\254\25415\351\242\230.cpp" deleted file mode 100644 index 266159a84e4ee00bf29d5be448dd4d1c36606a7b..0000000000000000000000000000000000000000 --- "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\22077\351\241\265\347\254\25415\351\242\230.cpp" +++ /dev/null @@ -1,19 +0,0 @@ -struct ListNode* swapPairs(struct ListNode* head){ - if (NULL == head || NULL == head->next) { - return head; - } - struct ListNode dumbNode = { 0 }; - dumbNode.next = head; - struct ListNode *cur = &dumbNode; - struct ListNode *Node1 = NULL; - struct ListNode *Node2 = NULL; - while (cur->next && cur->next->next) { - Node1 = cur->next; - Node2 = Node1->next; - cur->next = Node2; - Node1->next = Node2->next; - Node2->next = Node1; - cur = Node1; - } - return dumbNode.next; -} \ No newline at end of file diff --git "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\22077\351\241\265\347\254\25423\351\242\230.cpp" "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\22077\351\241\265\347\254\25423\351\242\230.cpp" deleted file mode 100644 index 085ad948c164fcd0a76e09160249c52ad4a8383a..0000000000000000000000000000000000000000 --- "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\22077\351\241\265\347\254\25423\351\242\230.cpp" +++ /dev/null @@ -1,9 +0,0 @@ -class Solution { - public ListNode deleteDuplicates(ListNode head) { - if(head == null || head.next == null){ - return head; - } - head.next = deleteDuplicates(head.next); - return head.val == head.next.val?head.next:head; - } -} \ No newline at end of file diff --git "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\22077\351\241\265\347\254\254\344\271\235\351\242\230\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\22077\351\241\265\347\254\254\344\271\235\351\242\230\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" deleted file mode 100644 index d9634c228b7e10dea392c1ec9496da86d7030e36..0000000000000000000000000000000000000000 --- "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\22077\351\241\265\347\254\254\344\271\235\351\242\230\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" +++ /dev/null @@ -1,30 +0,0 @@ -struct ListNode* removeElements(struct ListNode* head, int val) -{ - struct ListNode* prev = head; - struct ListNode* cur = head; - while(cur) - { - - if(cur->val == val) - { - if(cur == head) - { - head = cur->next; - free(cur); - cur = head; - } - else - { - prev->next = cur->next; - free(cur); - cur = prev->next; - } - } - else - { - prev = cur; - cur = cur->next; - } - } - return head; -} \ No newline at end of file diff --git "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\347\250\21376\351\241\265\347\254\2545\351\242\230\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.cpp" "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\347\250\21376\351\241\265\347\254\2545\351\242\230\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.cpp" deleted file mode 100644 index e27d09250ad5eb24cfdb6469b34917901dbfcb32..0000000000000000000000000000000000000000 --- "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\347\250\21376\351\241\265\347\254\2545\351\242\230\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.cpp" +++ /dev/null @@ -1,38 +0,0 @@ -#include - int main() -{ - int nums1[] = { 1,2,3,0,0,0 }; - int nums2[] = { 2,5,6 }; - int m = 3; - int n = 3; - int i = (m + n)-1; - int end1 = m-1; - int end2 = n-1; - while (end1 >= 0 && end2 >= 0) - { - if (nums1[end1] > nums2[end2]) - { - nums1[i] = nums1[end1]; - i--; - end1--; - } - else - { - nums1[i] = nums2[end2]; - i--; - end2--; - } - } - while (end2 >= 0) - { - nums1[i] = nums2[end2]; - i--; - end2--; - } - i = 0; - for (i = 0; i < (m + n); i++) - { - printf("%d ", nums1[i]); - } - return 0; -} \ No newline at end of file diff --git "a/2209040024/\346\234\200\345\260\217\346\240\210.cpp" "b/2209040024/\346\234\200\345\260\217\346\240\210.cpp" deleted file mode 100644 index ad3094bb2502e2d6ade2c817fca31bb473592cd6..0000000000000000000000000000000000000000 --- "a/2209040024/\346\234\200\345\260\217\346\240\210.cpp" +++ /dev/null @@ -1,21 +0,0 @@ -class MinStack { -public: - MinStack() {} - void push(int x) { - s1.push(x); - if (s2.empty() || x <= s2.top()) s2.push(x); - } - void pop() { - if (s1.top() == s2.top()) s2.pop(); - s1.pop(); - } - int top() { - return s1.top(); - } - int getMin() { - return s2.top(); - } - -private: - stack s1, s2; -}; \ No newline at end of file diff --git "a/2209040024/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" "b/2209040024/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" deleted file mode 100644 index 93ce71ca174cff322e89e9fcdc9a59ff29006b1f..0000000000000000000000000000000000000000 --- "a/2209040024/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - bool isValid(string s) { - stack parentheses; - for (int i = 0; i < s.size(); ++i) { - if (s[i] == '(' || s[i] == '[' || s[i] == '{') parentheses.push(s[i]); - else { - if (parentheses.empty()) return false; - if (s[i] == ')' && parentheses.top() != '(') return false; - if (s[i] == ']' && parentheses.top() != '[') return false; - if (s[i] == '}' && parentheses.top() != '{') return false; - parentheses.pop(); - } - } - return parentheses.empty(); - } -} \ No newline at end of file diff --git "a/2209040024/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" "b/2209040024/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" deleted file mode 100644 index 867154a3f3cfa4534078efaed819d00e17a7ca5e..0000000000000000000000000000000000000000 --- "a/2209040024/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" +++ /dev/null @@ -1,32 +0,0 @@ -package com.sxt.test.java; - -public class Queue2Stack { - - Queue queue1 = new Queue(); - Queue queue2 = new Queue(); - - public void push(Object o) { - queue1.add(o); - } - - public Object pop() { - Object o = null; - while(queue1.length()>1) { - queue2.add(queue1.poll()); - } - if(queue1.length()==1) { - o = queue1.poll(); - while(queue2.length()>0) { - queue1.add(queue2.poll()); - } - } - - return o; - } - - public int length() { - return queue1.length(); - } - - -} \ No newline at end of file diff --git "a/2209040028/chapter-7/\345\256\236\347\216\260\344\272\214\345\217\211\346\240\221\347\232\204\345\220\204\347\247\215\350\277\220\347\256\227\347\232\204\345\237\272\346\234\254\347\256\227\346\263\225.cpp" "b/2209040028/chapter-7/\345\256\236\347\216\260\344\272\214\345\217\211\346\240\221\347\232\204\345\220\204\347\247\215\350\277\220\347\256\227\347\232\204\345\237\272\346\234\254\347\256\227\346\263\225.cpp" deleted file mode 100644 index 95d2482f4ce2e3855faea6c0ecd6f54ac94729f3..0000000000000000000000000000000000000000 --- "a/2209040028/chapter-7/\345\256\236\347\216\260\344\272\214\345\217\211\346\240\221\347\232\204\345\220\204\347\247\215\350\277\220\347\256\227\347\232\204\345\237\272\346\234\254\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,125 +0,0 @@ -#include -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct node -{ - ElemType data; - struct node *lchild; - struct node *rchild; - -}BTNode; -void CreateBTree(BTNode *&b,char *str) -{ - BTNode * St[MaxSize],*p; - int top=-1,k,j=0;char ch; - b=NULL; - ch=str[j]; - while(ch!='\0') - { - switch(ch) - { - case'(':top++;St[top]=p;k=1;break; - case')':top--;break; - case',':k=2;break; - - default:p=(BTNode *)malloc(sizeof(BTNode)); - p->data=ch;p->lchild=p->rchild=NULL; - if(b==NULL) - b=p; - else - { - switch(k) - { - case 1:St[top]->lchild=p;break; - case 2:St[top]->rchild=p;break; - } - } - } - j++;ch=str[j]; - } -} -void DestroyBTree(BTNode *&b) -{ - if(b!=NULL) - { - DestroyBTree(b->lchild); - DestroyBTree(b->rchild); - free(b); - } -} -BTNode *FindNode(BTNode *b,ElemType x) -{ - BTNode *p; - if(b==NULL) - return NULL; - else if(b->data==x) - return b; - else - { - p=FindNode(b->lchild,x); - if(p!=NULL) - return p; - else - return FindNode(b->rchild,x); - } -} -BTNode *LchildNode(BTNode *p) -{ - return p->lchild; - -} -BTNode *RchildNode(BTNode *p) -{ - return p->rchild; -} -int BTHeight(BTNode *b) -{ - int lchildh,rchildh; - if(b==NULL)return(0); - else - { - lchildh=BTHeight(b->lchild); - rchildh=BTHeight(b->rchild); - return(lchildh>rchildh)?(lchildh+1):(rchildh+1); - } -} -void DispBTree(BTNode *b) -{ - if(b!=NULL) - { - printf("%c",b->data); - if(b->lchild!=NULL||b->rchild!=NULL) - { - printf("("); - DispBTree(b->lchild); - if(b->rchild!=NULL)printf(","); - DispBTree(b->rchild); - printf(")"); - } - } -} -int main() -{ - BTNode *b,*p,*lp,*rp;; - printf("二叉树的基本运算如下:\n"); - printf(" (1)创建二叉树\n"); - CreateBTree(b,"A(B(D,E(H(J,k(L,M(,N))))),C(F,G(,I)))"); - printf(" (2)输出二叉树:");DispBTree(b);printf("\n"); - printf(" (3)H结点:"); - p=FindNode(b,'H'); - if(p!=NULL) - { lp= LchildNode(p); - if(lp!=NULL)printf("左孩子为%c",lp->data); - else printf("无左孩子"); - rp=RchildNode(p); - if(rp!=NULL) printf("右孩子为%c",rp->data); - else printf("无右孩子"); - - } - printf("\n"); - printf(" (4)二叉树b的高度:%d\n",BTHeight(b)); - printf(" (5)释放二叉树b\n"); - DestroyBTree(b); - return 1; -} \ No newline at end of file diff --git "a/2209040031/chap.1/lc1588\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" "b/2209040031/chap.1/lc1588\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" deleted file mode 100644 index dc90c5d9f5aa6a67a27acde7d7fa40b508837604..0000000000000000000000000000000000000000 --- "a/2209040031/chap.1/lc1588\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" +++ /dev/null @@ -1,13 +0,0 @@ -class Solution { -public: - int sumOddLengthSubarrays(vector& arr) { - int res = 0; - for(int i = 0; i < arr.size(); i ++){ - int left = i + 1, right = arr.size() - i, - left_even = (left + 1) / 2, right_even = (right + 1) / 2, - left_odd = left / 2, right_odd = right / 2; - res += (left_even * right_even + left_odd * right_odd) * arr[i]; - } - return res; - } -}; \ No newline at end of file diff --git "a/2209040031/chap.1/lc1\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2209040031/chap.1/lc1\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index b8eeca1f7ad8ff27fa50b0745c2adcdb795172fb..0000000000000000000000000000000000000000 --- "a/2209040031/chap.1/lc1\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - vector twoSum(vector& nums, int target) { - int i,j; - for(i=0;i -#include -#include - -clock_t start , stop; -double duration1,duration2;//秒 - -void add1(long n) -{ - start=clock(); - long sum=0; - sum=(1+n)*n/2; - stop=clock(); - printf("高斯法:从1到%ld的连续整数和为:%ld\n",n,sum);//输出不是计算过程,所以放在外面 - duration1 = ((double)(stop-start))/CLOCKS_PER_SEC; - printf("高斯法的用时是%f秒\n",duration1); -} - -void add2(long n) -{ - start=0; - stop=0; - start=clock(); - long sum=0; - for(int i=1;i<=n;i++) - { - sum=i+sum; - } - stop=clock(); - printf("累加法:从1到%ld的连续整数和为:%ld\n",n,sum); - duration2 = ((double)(stop-start))/CLOCKS_PER_SEC; - printf("累加法的用时是%f秒\n",duration2); -} - -int main() -{ - long n; - scanf("%ld",&n); - add1(n); - add2(n); - return 0; -} diff --git "a/2209040031/chap.1/\344\271\240\351\242\2303.cpp" "b/2209040031/chap.1/\344\271\240\351\242\2303.cpp" deleted file mode 100644 index 169fefe655af44f933abdbd0f247d6d000f2bec6..0000000000000000000000000000000000000000 --- "a/2209040031/chap.1/\344\271\240\351\242\2303.cpp" +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include -#include -bool prime1(long n) -{ - long i; - for(i=2;inext = removeElements(head->next,val); - return head->val == val ? head->next : head; - - } -}; \ No newline at end of file diff --git "a/2209040031/chap.2/lc24\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\347\273\223\347\202\271.cpp" "b/2209040031/chap.2/lc24\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\347\273\223\347\202\271.cpp" deleted file mode 100644 index 7d39648fec611f9bcf34701c2c921f09549542de..0000000000000000000000000000000000000000 --- "a/2209040031/chap.2/lc24\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\347\273\223\347\202\271.cpp" +++ /dev/null @@ -1,12 +0,0 @@ -class Solution { -public: - ListNode* swapPairs(ListNode* head) { - if (head == nullptr || head->next == nullptr) { - return head; - } - ListNode* newHead = head->next; - head->next = swapPairs(newHead->next); - newHead->next = head; - return newHead; - } -}; \ No newline at end of file diff --git "a/2209040031/chap.2/lc82\345\210\240\351\231\244\346\216\222\345\272\217\345\210\227\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240II.cpp" "b/2209040031/chap.2/lc82\345\210\240\351\231\244\346\216\222\345\272\217\345\210\227\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240II.cpp" deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git "a/2209040031/chap.2/lc88\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.cpp" "b/2209040031/chap.2/lc88\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.cpp" deleted file mode 100644 index c478edeeffcd34f74c46bbacf80b6e7da7f5aad5..0000000000000000000000000000000000000000 --- "a/2209040031/chap.2/lc88\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.cpp" +++ /dev/null @@ -1,9 +0,0 @@ -class Solution { -public: - void merge(vector& nums1, int m, vector& nums2, int n) { - for (int i = 0; i != n; ++i) { - nums1[m + i] = nums2[i]; - } - sort(nums1.begin(), nums1.end()); - } -}; \ No newline at end of file diff --git "a/2209040031/chap.2/lc92\347\277\273\350\275\254\351\223\276\350\241\250II.cpp" "b/2209040031/chap.2/lc92\347\277\273\350\275\254\351\223\276\350\241\250II.cpp" deleted file mode 100644 index 6cc92cf2cef26dc33f0f26799c462cb3609b1153..0000000000000000000000000000000000000000 --- "a/2209040031/chap.2/lc92\347\277\273\350\275\254\351\223\276\350\241\250II.cpp" +++ /dev/null @@ -1,20 +0,0 @@ -class Solution { -public: - ListNode* reverseBetween(ListNode* head, int left, int right) { - ListNode *dummyNode = new ListNode(-1); - dummyNode->next = head; - ListNode *pre = dummyNode; - for (int i = 0; i < left - 1; i++) { - pre = pre->next; - } - ListNode *cur = pre->next; - ListNode *next; - for (int i = 0; i < right - left; i++) { - next = cur->next; - cur->next = next->next; - next->next = pre->next; - pre->next = next; - } - return dummyNode->next; - } -}; \ No newline at end of file diff --git "a/2209040031/chap.2/\344\271\240\351\242\2302.cpp" "b/2209040031/chap.2/\344\271\240\351\242\2302.cpp" deleted file mode 100644 index 91cfd841ba9c0384e37dfc7fc31421a3a98df428..0000000000000000000000000000000000000000 --- "a/2209040031/chap.2/\344\271\240\351\242\2302.cpp" +++ /dev/null @@ -1,251 +0,0 @@ -#include -#include -typedef int ElemType; -//单链表的声明 -typedef struct LNode -{ - ElemType data;//存储元素的数据域 - struct LNode *next;//存储后继节点 -}LinkNode; -//建立单链表 -//1)头插法,每次插入表头,L,an,an-1,an-2``````a2,a1 -void CreateListF(LinkNode *&L,ElemType a[],int n) -{ - LinkNode *s; - L=(LinkNode *)malloc(sizeof(LinkNode));//给L分配内存用malloc - L->next=NULL;//初始化链表 - for(int i=0;idata=a[i];//将数组值给s - s->next=L->next;//插入操作 - L->next=s; - } -} -//2)尾插法 -void CreateListR(LinkNode *&L,int n,ElemType a[]) -{ - LinkNode *s,*r;//定义头指针和尾指针 - L=(LinkNode *)malloc(sizeof(LinkNode));//分配内存 - r=L; - for(int i=0;idata=a[i]; - r->next=s; - r=s; - } - r->next=NULL; -} -//初始化单链表 -void InitList(LinkNode *&L) -{ - L=(LinkNode *)malloc(sizeof(LinkNode)); - L->next=NULL; -} -//销毁单链表 -void DestroyList(LinkNode *&L) -{ - LinkNode *pre=L,*p=L->next; - while(p!=NULL) - { - free(pre); - pre=p; - p=pre->next; - } - free(pre); -} -//判断是否为空链表 -bool ListEmpty(LinkNode *L) -{ - return(L->next==NULL); -} -//求线性表的长度 -int ListLength(LinkNode *L) -{ - int n=0; - LinkNode *p=L; - while(p->next!=NULL) - { - n++; - p=p->next; - } - return(n); -} -//输出线性表 -void DisList(LinkNode *L) -{ - LinkNode *p=L->next; - while(p!=NULL) - { - printf("%d",p->data); - p=p->next; - } - printf("\n"); -} -//按序号求线性表的元素 -bool GetElem(LinkNode *L,int i,ElemType &e) -{ - int j=0; - LinkNode *p=L; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - e=p->data; - return true; -} -//按元素值查找 -int LocateList(LinkNode *L,ElemType e) -{ - int i=1; - LinkNode *p=L->next; - while(p!=NULL) - { - p=p->next; - i++; - } - if(p==NULL) - return(0); - else - return(i); -} -//插入数据元素 -bool ListInsert(LinkNode *&L,int i,ElemType e) -{ - int j=0; - LinkNode *p=L,*s; - if(i<=0) - return false; - while(jnext; - j++; - } - if(p==NULL) - return false; - else - { - s=(LinkNode *)malloc(sizeof(LinkNode)); - s->data=e; - s->next=p->next; - p->next=s; - return true; - } -} -//删除数据元素 -bool ListDelete(LinkNode *&L,int i,ElemType &e) -{ - int j=0; - LinkNode *p=L,*q; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - q=p->next; - if(q==NULL) - return false; - e=q->data; - p->next=q->next; - free(q); - return true; - } -} -//应用 -//将L分成两个单链表L1,L2,L1使用L的头节点 -void split(LinkNode *&L,LinkNode *&L1,LinkNode *&L2) -{ - LinkNode *p=L->next,*q,*r1; - L1=L;//将L的头结点给L1 - L2=(LinkNode *)malloc(sizeof(LinkNode));//创建L2的头结点 - L2->next=NULL;//初始化头结点 - while(p!=NULL) - { - r1->next=p;//L1采用尾插法 - r1=p; - p=p->next;//将数据后移一个指针,到b - q=p->next; - p->next=L2->next;//L2用头插法 - L2->next=p; - p=q; - } - r1->next=NULL; -} -//删除元素值最大的元素 -void delmaxnode(LinkNode *&L) -{ - LinkNode *p=L->next,*pre=L,*maxp=p,*maxpre=pre; - while(p!=NULL) - { - if(maxp->datadata)//如果maxp的值小于p,将p的值给maxp,pre的值给maxpre - { - maxp=p; - maxpre=pre; - } - pre=p;//将pre和p的值后移一位 - p=p->next; - } - maxpre->next=maxp->next;//进行删除操作 - free(maxp); -} -//递增排序 -void sort(LinkNode *&L) -{ - LinkNode *p,*pre ,*q; - p=L->next->next; - L->next->next=NULL; - while(p!=NULL) - { - q=p->next; - pre=L; - while(pre->next!=NULL && pre->next->datadata)//若pre的值小于p,则不用交换,将pre的位置后移一位 - pre=pre->next; - p->next=pre->next;//进行头插操作 - pre->next=p; - p=q; - } -} -int main() -{ - LinkNode *h; - ElemType e; - printf("1)初始化单链表\n"); - InitList(h); - printf("2)采用尾插法插入元素\n"); - ListInsert(h,1,'a'); - ListInsert(h,2,'b'); - ListInsert(h,3,'c'); - ListInsert(h,4,'d'); - ListInsert(h,5,'e'); - printf("3)输出单链表\n"); - DisList(h); - printf("4)单链表长度%d\n",ListLength(h)); - printf("5)单链表%s\n",(ListEmpty(h)?"空":"非空")); - GetElem(h,3,e); - printf("6)单链表第三个元素\n",e); - printf("7)元素a的位置%d\n",LocateList(h,'a')); - printf("8)在第四个元素位置上插入元素\n"); - ListInsert(h,4,'f'); - printf("9)输出单链表\n"); - DisList(h); - printf("10)删除第三个元素\n"); - ListDelete(h,3,e); - printf("11)输出单链表\n"); - DisList(h); - printf("12)释放单链表\n"); - DestroyList(h); - return 1; -} \ No newline at end of file diff --git "a/2209040031/chap.2/\344\271\240\351\242\2306.cpp" "b/2209040031/chap.2/\344\271\240\351\242\2306.cpp" deleted file mode 100644 index db9940a98affee4cbd188297a6beeecd505d46d4..0000000000000000000000000000000000000000 --- "a/2209040031/chap.2/\344\271\240\351\242\2306.cpp" +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include -typedef int ElemType; -typedef struct LNode -{ - ElemType data; - struct LNode *next; -}LinkNode; -void DivideList(LinkNode *&L) -{ - LinkNode *p,*pre,*q; - p=L->next->next=NULL; - while(p!=NULL) - { - q=p->next; - pre=L; - while(pre->next!=NULL && pre->next->data>p->data)//头插 - { - pre=pre->next; - p->next=pre->next; - pre->next=p; - p=q; - } - } -} -int main() -{ - LinkNode *L; - ElemType a[]=[2,3,6,8,9]; - int n=strlen(a); - DivideList(L); - -} \ No newline at end of file diff --git "a/2209040031/chap.3/lc155\346\234\200\345\260\217\346\240\210.cpp" "b/2209040031/chap.3/lc155\346\234\200\345\260\217\346\240\210.cpp" deleted file mode 100644 index 0702c78bf60e2efbf3a3c23171cd24bcc883c803..0000000000000000000000000000000000000000 --- "a/2209040031/chap.3/lc155\346\234\200\345\260\217\346\240\210.cpp" +++ /dev/null @@ -1,26 +0,0 @@ -class MinStack { - stack x_stack; - stack min_stack; -public: - MinStack() { - min_stack.push(INT_MAX); - } - - void push(int x) { - x_stack.push(x); - min_stack.push(min(min_stack.top(), x)); - } - - void pop() { - x_stack.pop(); - min_stack.pop(); - } - - int top() { - return x_stack.top(); - } - - int getMin() { - return min_stack.top(); - } -}; \ No newline at end of file diff --git "a/2209040031/chap.3/lc20\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" "b/2209040031/chap.3/lc20\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" deleted file mode 100644 index c171d4915c48a9589e66d0b2b0f9f6a781d66b90..0000000000000000000000000000000000000000 --- "a/2209040031/chap.3/lc20\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" +++ /dev/null @@ -1,29 +0,0 @@ -class Solution { -public: - bool isValid(string s) { - int n = s.size(); - if (n % 2 == 1) { - return false; - } - - unordered_map pairs = { - {')', '('}, - {']', '['}, - {'}', '{'} - }; - stack stk; - for (char ch: s) { - if (pairs.count(ch)) { - if (stk.empty() || stk.top() != pairs[ch]) { - return false; - } - stk.pop(); - } - else { - stk.push(ch); - } - } - return stk.empty(); - - } -}; \ No newline at end of file diff --git a/2209040033/chap_2/lc_203.cpp b/2209040033/chap_2/lc_203.cpp deleted file mode 100644 index 579d67a357024bf942d881b61a161af5e27b1769..0000000000000000000000000000000000000000 --- a/2209040033/chap_2/lc_203.cpp +++ /dev/null @@ -1,20 +0,0 @@ -class Solution { -public: - ListNode* removeElements(ListNode* head, int val) { - ListNode* pre = new ListNode(0, head); - ListNode* cur = pre; - while (cur->next) { - if (cur->next->val == val) { - ListNode* delNode = cur->next; - cur->next = delNode->next; - delete delNode; - } - else { - cur = cur->next; - } - } - cur = pre->next; - delete pre; - return cur; - } -}; \ No newline at end of file diff --git a/2209040033/chap_2/lc_24.cpp b/2209040033/chap_2/lc_24.cpp deleted file mode 100644 index 44b4e38e81624680670089c32ac39f989d42e96f..0000000000000000000000000000000000000000 --- a/2209040033/chap_2/lc_24.cpp +++ /dev/null @@ -1,21 +0,0 @@ - -class Solution { -public: - ListNode* swapPairs(ListNode* head) { - ListNode *dummyHead = new ListNode(0,head),*temp = dummyHead; - - while (temp->next != nullptr && temp->next->next != nullptr) - { - ListNode *node1 = temp->next,*node2 = temp->next->next; - - temp->next = node2, - node1->next = node2->next, - node2->next = node1, - temp = node1; - - } - ListNode* ans = dummyHead->next; - delete dummyHead; - return ans; - } -}; \ No newline at end of file diff --git a/2209040033/chap_2/lc_82.cpp b/2209040033/chap_2/lc_82.cpp deleted file mode 100644 index 7e8a04e6082e6878d569a28d0308b8e55564eb0b..0000000000000000000000000000000000000000 --- a/2209040033/chap_2/lc_82.cpp +++ /dev/null @@ -1,26 +0,0 @@ -class Solution { -public: - ListNode* deleteDuplicates(ListNode* head) { - ListNode* pre = new ListNode(0, head); // 伪头节点 - ListNode* last = pre; // 当前节点的上一个节点,初始为伪头节点 - ListNode* node = head; // 当前节点,初始为头节点 - int sameVal; // 用于记录每次搜索的节点值 - int count; // 统计节点值等于sameVal的节点数 - while(node != nullptr){ - sameVal = node->val; // 记录当前搜索的节点值 - count = 1; // 统计节点值 - while(node->next != nullptr && node->next->val == sameVal){ - // 找到当前搜索的节点值的最后一个节点 - node = node->next; - count++; - } - if(count == 1){ - last = node; // 这个节点值只有一个节点,不为重复节点,将作为下一轮搜索的last - }else{ - last->next = node->next; // 这个节点值有重复节点,删除这些重复节点,即last连接到当前节点的下一个,last不变 - } - node = node->next; // 更新node - } - return pre->next; - } -}; \ No newline at end of file diff --git a/2209040033/chap_2/lc_88.cpp b/2209040033/chap_2/lc_88.cpp deleted file mode 100644 index b451cc5a0b3de05693aa3bcd39ea0dab0861a569..0000000000000000000000000000000000000000 --- a/2209040033/chap_2/lc_88.cpp +++ /dev/null @@ -1,38 +0,0 @@ - -class Solution { -public: - void merge(vector& nums1, int m, vector& nums2, int n) { - //判断特殊情况节省运行时间 - if (nums2.empty()) - return; - //用 i , j , cur 三个指针 - // i 指向 nums1 中最后一个非零元素 - int i = m - 1; - // j 指向 nums2 中最后一个元素 - int j = n - 1; - // cur 指向 nums1 当前要填充的位置 - int cur = m + n - 1; - //同时遍历 nums1 和 nums2 - while (i >= 0 && j >= 0) { - //将较大的元素放到当前要填充的位置 - if (nums1[i] > nums2[j]) { - nums1[cur] = nums1[i]; - //只需要更新 i 指针,不需要更新 j - i--; - } else { - //将较大的元素放到当前要填充的位置 - nums1[cur] = nums2[j]; - //同理,只需要更新 j 指针 - j--; - } - //不管哪个大都要更新 cur 指针 - cur--; - } - //j大于等于0,说明 nums2 中还有剩余元素,然后将剩余的 nums2 中的元素放入 nums1 中 - while (j >= 0) { - nums1[cur] = nums2[j]; - cur--; - j--; - } - } -}; \ No newline at end of file diff --git a/2209040033/chap_2/lc_92.cpp b/2209040033/chap_2/lc_92.cpp deleted file mode 100644 index 7227a2f95cdfee9d38f6de5be2cefd4a50654ced..0000000000000000000000000000000000000000 --- a/2209040033/chap_2/lc_92.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode() : val(0), next(nullptr) {} - * ListNode(int x) : val(x), next(nullptr) {} - * ListNode(int x, ListNode *next) : val(x), next(next) {} - * }; - */ -class Solution { -public: - ListNode* reversepro(ListNode* begin , ListNode* end){ - if(begin->next == end){ - return begin; - } - ListNode* newhead = reversepro(begin->next , end); - begin->next->next = begin; - begin->next = nullptr; - return newhead; - } - ListNode* reverseBetween(ListNode* head, int left, int right) { - if(!head || !head->next || left == right){ - return head; - } - - - ListNode* dumbnode = new ListNode(0 , head); - ListNode* pre = dumbnode; - ListNode* end = dumbnode; - for(int i = 0 ; i < left-1 ; ++i){ - pre = pre->next; - } - for(int i = 0 ; i < right + 1 ; ++i){ - end = end->next; - } - - - ListNode* e = reversepro(pre->next , end); - pre->next->next = end; - pre->next = e; - return dumbnode->next; - } -}; \ No newline at end of file diff --git a/2209040034/chap3/lc-150.cpp b/2209040034/chap3/lc-150.cpp deleted file mode 100644 index 6df82007f644a640e2d778d487f89868b60b1ef1..0000000000000000000000000000000000000000 --- a/2209040034/chap3/lc-150.cpp +++ /dev/null @@ -1,34 +0,0 @@ -int evalRPN(char ** tokens, int tokensSize){ -int stk[tokensSize]; -int top=-1,i=0; -for(i;i='0'||strlen(p)>1) - { - stk[++top]=atoi(p); - } - else - { - int num2=stk[top]; - int num1=stk[--top]; - switch(p[0]) - { - case '+': - stk[top]=num1+num2; - break; - case '-': - stk[top]=num1-num2; - break; - case '*': - stk[top]=num1*num2; - break; - case '/': - stk[top]=num1/num2; - break; - } - } - -} -return stk[top]; -} \ No newline at end of file diff --git a/2209040034/chap3/lc-155.cpp b/2209040034/chap3/lc-155.cpp deleted file mode 100644 index 0bc17930b393a0239d23f806d49b42275330548c..0000000000000000000000000000000000000000 --- a/2209040034/chap3/lc-155.cpp +++ /dev/null @@ -1,62 +0,0 @@ -typedef struct MinStack -{ - int data; - struct MinStack *next; -} MinStack; - - -MinStack* minStackCreate() { - MinStack *new=(MinStack *)malloc(sizeof(MinStack)); - new->data=0; - new->next=NULL; - return new; -} - -void minStackPush(MinStack* obj, int val) { - MinStack *new=(MinStack *)malloc(sizeof(MinStack)); - new->data=val; - new->next=obj->next; - obj->next=(struct MinStack*)new; - -} - -void minStackPop(MinStack* obj) { - if(obj->next==NULL)//空 - return; - else if(obj->next!=NULL) - { - MinStack* frees=obj->next; - obj->next=frees->next; - free(frees); - } -} - -int minStackTop(MinStack* obj) { - return (obj->next->data); -} - -int minStackGetMin(MinStack* obj) { - MinStack *p=obj->next; - int min=p->data; - while(p!=NULL) - { - if(p->datadata; - } - p=p->next; - } - return min; -} - -void minStackFree(MinStack* obj) { - MinStack *o=obj->next; - while(o!=NULL) - { - - MinStack *p=o; - o=o->next; - free(p); - - } -} \ No newline at end of file diff --git a/2209040034/chap3/lc-20.cpp b/2209040034/chap3/lc-20.cpp deleted file mode 100644 index 48eb4e95eaa9b09cc5ca7a64670b4308e958f6d8..0000000000000000000000000000000000000000 --- a/2209040034/chap3/lc-20.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/*给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效*/ - -bool isValid(char * s){ - int num; - char a[100001],x;//模拟栈 - num=strlen(s); - if(num%2==1) - { - return false; - } - int top=0,i=0; - if(s[0]==')'||s[0]==']'||s[0]=='}') - return false; - while(s[i]!='\0') - { - if(s[i]=='('||s[i]=='{'||s[i]=='[') - { - if(s[i]=='(') - x=')'; - if(s[i]=='[') - x=']'; - if(s[i]=='{') - x='}'; - top++; - a[top]=x; - //top++;为什么这里先赋值再加就不行? - } - else - { - if(a[top]==s[i]) - top--; - else - return false; - } - i++; - } - if(top==0) - return true; - else - return false; -} \ No newline at end of file diff --git a/2209040034/chap3/lc-227.cpp b/2209040034/chap3/lc-227.cpp deleted file mode 100644 index dc321a7eba0eb187c9d44b0d0406a4032b260d99..0000000000000000000000000000000000000000 --- a/2209040034/chap3/lc-227.cpp +++ /dev/null @@ -1,42 +0,0 @@ -//基本计算器 -int calculate(char * s) -{ - int len=strlen(s); - char sign='+'; - int stack[len]; - memset(stack,0,sizeof(int)*len); - int top=-1; - int num=0; - for(int i=0;i -1) { - rst += stack[top--]; - } - return rst; -} \ No newline at end of file diff --git a/2209040035/chap 2/lc_203.cpp b/2209040035/chap 2/lc_203.cpp deleted file mode 100644 index 922dba82afd74d170eeb4dae0655b57507f73f1c..0000000000000000000000000000000000000000 --- a/2209040035/chap 2/lc_203.cpp +++ /dev/null @@ -1,22 +0,0 @@ -struct ListNode* removeElements(struct ListNode* head, int val){ - struct ListNode* node=head; - if(head==NULL) return head; - while(node->next!=NULL) - { - if(node->val==val) - { - head=node->next; - node=head; - } - else if(node->next->val==val) - { - node->next=node->next->next; - } - else node=node->next; - } - if(head->val==val) - { - head=NULL; - } - return head; -} \ No newline at end of file diff --git a/2209040035/chap 2/lc_24.cpp b/2209040035/chap 2/lc_24.cpp deleted file mode 100644 index 51223ae67c8d383b30b4afc7dc819ced36e04ea9..0000000000000000000000000000000000000000 --- a/2209040035/chap 2/lc_24.cpp +++ /dev/null @@ -1,9 +0,0 @@ -struct ListNode* swapPairs(struct ListNode* head) { - if (head == NULL || head->next == NULL) { - return head; - } - struct ListNode* newHead = head->next; - head->next = swapPairs(newHead->next); - newHead->next = head; - return newHead; -} \ No newline at end of file diff --git a/2209040035/chap 2/lc_82 b/2209040035/chap 2/lc_82 deleted file mode 100644 index 375b3643b0a2422d781f6919e676603ed92704a6..0000000000000000000000000000000000000000 --- a/2209040035/chap 2/lc_82 +++ /dev/null @@ -1,27 +0,0 @@ -struct ListNode* deleteDuplicates(struct ListNode* head) -{ - if(head==NULL||head->next==NULL) - return head; - struct ListNode* tmp=malloc(sizeof(struct ListNode)); - tmp->val=1; - tmp->next=head; - struct ListNode* q=tmp; - int x=0; - - -while(q->next!=NULL&&q->next->next!=NULL) - if(q->next->val==q->next->next->val) - { - x=q->next->val; - while(q->next!=NULL&&q->next->val==x) - { - q->next=q->next->next; - } - } - else - { - q=q->next; - } -} -return tmp->next; -} \ No newline at end of file diff --git a/2209040035/chap 2/lc_82.cpp b/2209040035/chap 2/lc_82.cpp deleted file mode 100644 index 375b3643b0a2422d781f6919e676603ed92704a6..0000000000000000000000000000000000000000 --- a/2209040035/chap 2/lc_82.cpp +++ /dev/null @@ -1,27 +0,0 @@ -struct ListNode* deleteDuplicates(struct ListNode* head) -{ - if(head==NULL||head->next==NULL) - return head; - struct ListNode* tmp=malloc(sizeof(struct ListNode)); - tmp->val=1; - tmp->next=head; - struct ListNode* q=tmp; - int x=0; - - -while(q->next!=NULL&&q->next->next!=NULL) - if(q->next->val==q->next->next->val) - { - x=q->next->val; - while(q->next!=NULL&&q->next->val==x) - { - q->next=q->next->next; - } - } - else - { - q=q->next; - } -} -return tmp->next; -} \ No newline at end of file diff --git a/2209040035/chap 2/lc_88.cpp b/2209040035/chap 2/lc_88.cpp deleted file mode 100644 index 12e1733e09a35d93534cb00c628c3e9266d2a2e1..0000000000000000000000000000000000000000 --- a/2209040035/chap 2/lc_88.cpp +++ /dev/null @@ -1,45 +0,0 @@ -void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){ -int a[m+n],t; -int i=0,j=0; -if(m==0) -{ - for(i=0;ia[j+1]) - { - t=a[j]; - a[j]=a[j+1]; - a[j+1]=t; - } - } - } - for(i=0;inext; - cur->next=pre; - pre=cur; - cur=next; - } -} -struct ListNode *reverseBetween(struct ListNode *head, int left, int right) { - struct ListNode *dummyNode = malloc(sizeof(struct ListNode)); - dummyNode->val=-1; - dummyNode->next=head; - - struct ListNode *pre=dummyNode; - for (int y=0;ynext; - } - struct ListNode *rightNode=pre; - for (int y=0;ynext; - } - struct ListNode *leftNode=pre->next; - struct ListNode *curr=rightNode->next; - - pre->next=NULL; - rightNode->next=NULL; - - reverseLinkedList(leftNode); - - pre->next=rightNode; - leftNode->next=curr; - return dummyNode->next; -} \ No newline at end of file diff --git "a/2209040037/chapter_3/Ic_150\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" "b/2209040037/chapter_3/Ic_150\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" deleted file mode 100644 index d050fd18047ec74e1b346e2eadef02104cc303f3..0000000000000000000000000000000000000000 --- "a/2209040037/chapter_3/Ic_150\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include -//#include -#include - -using namespace std; -char s[10]; -double f() -{ - scanf("%s",s); - switch(s[0]) - { - case '+':return f()+f(); - case '-':return f()-f(); - case '*':return f()*f(); - case '/':return f()/f(); - default:return atof(s); - } -} -int main() -{ - printf("%f\n",f()); - return 0; -} diff --git "a/2209040037/chapter_3/Ic_155\346\234\200\345\260\217\346\240\210.cpp" "b/2209040037/chapter_3/Ic_155\346\234\200\345\260\217\346\240\210.cpp" deleted file mode 100644 index dd0361c747de57e102f59202ebc50256c4b3d61c..0000000000000000000000000000000000000000 --- "a/2209040037/chapter_3/Ic_155\346\234\200\345\260\217\346\240\210.cpp" +++ /dev/null @@ -1,59 +0,0 @@ -#define MAX_SIZE 10001 - -typedef struct { - int* data; - int top; - int min; -} MinStack; - -/** initialize your data structure here. */ -MinStack* minStackCreate() { - MinStack* obj = (MinStack*)malloc(sizeof(MinStack)); - obj->data = (int*)malloc(sizeof(int) * MAX_SIZE); - obj->top = -1; - obj->min = INT_MAX; - return obj; -} - -void minStackPush(MinStack* obj, int val) { - if (obj->top >= MAX_SIZE) { - return; - } - obj->data[++(obj->top)] = val; - if (val < obj->min) { - obj->min = val; - } -} - -void minStackPop(MinStack* obj) { - if (obj->top < 0) { - return; - } - obj->top--; - if (obj->top >= 0) { - obj->min = obj->data[obj->top]; - } else { - obj->min = INT_MAX; - } - for (int i = obj->top; i >= 0; i--) { - if (obj->data[i] < obj->min) { - obj->min = obj->data[i]; - } - } -} - -int minStackTop(MinStack* obj) { - return obj->data[obj->top]; -} - -int minStackGetMin(MinStack* obj) { - if (obj->top < 0) { - return -1; - } - return obj->min; -} - -void minStackFree(MinStack* obj) { - free(obj); -} - diff --git "a/2209040037/chapter_3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2209040037/chapter_3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index 34b29be362268d0f1356761efec4b427daf4cd9a..0000000000000000000000000000000000000000 --- "a/2209040037/chapter_3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,94 +0,0 @@ -#include -#include -//ζ -#define MaxSize 50 -typedef char ElemType; -char w; -typedef struct -{ - ElemType data[MaxSize]; - int front,rear; -}SqQueue; -//(1)ʼζ -void InitQueue(SqQueue* &q) -{ - q=(SqQueue*)malloc(sizeof(SqQueue)); - q->front=q->rear=0; -} -//(2)νԪabc -bool enQueue(SqQueue* &q,ElemType e) -{ - if((q->rear+1)%MaxSize==q->front) - return false; - q->rear=(q->rear+1)%MaxSize; - q->data[q->rear]=e; - return true; -} -//(3)жϻζqǷǿ -bool QueueEmpty(SqQueue *q) -{ - return(q->front==q->rear); -} - -//(4)һԪأԪ -bool deQueue(SqQueue* &q,ElemType&e) -{ - if(q->front==q->rear) - return false; - q->front=(q->front+1)%MaxSize; - e=q->data[q->front]; - return true; -} -//(5)νԪdef - -//(6) - -//(7)ͷŶ -void DestroyQueue(SqQueue* &q) -{ - free(q); -} - - -// -int main() -{ - SqQueue *q; - printf("ζеĸֻ㷨 \n"); - - printf("(1)ʼζ \n"); - InitQueue(q); - - printf("(2)νԪabc \n"); - if(!enQueue(q,'a')) - printf("\tʾ:ܽ\n"); - if(!enQueue(q,'b')) - printf("\tʾ:ܽ\n"); - if(!enQueue(q,'c')) - printf("\tʾ:ܽ\n"); - - printf("(3)жϻζqǷǿ: %s\n",(QueueEmpty(q)?"":"ǿ")); - if(deQueue(q,w)==0) - printf("ӿ,ܳ\n"); - else - printf("(4)һԪأԪ: %c\n",w); - - printf("(5)νԪdef \n"); - if(!enQueue(q,'d')) - printf("\tʾ:ܽ\n"); - if(!enQueue(q,'e')) - printf("\tʾ:ܽ\n"); - if(!enQueue(q,'f')) - printf("\tʾ:ܽ\n"); - - printf("(6): \n"); - while(!QueueEmpty(q)) - { - deQueue(q,w); - printf("%c",w); - } - printf("\n"); - printf("(7)ͷŶ \n"); - DestroyQueue(q); - return 0; -} diff --git "a/2209040037/chapter_3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\347\232\204\351\227\256\351\242\230.cpp" "b/2209040037/chapter_3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\347\232\204\351\227\256\351\242\230.cpp" deleted file mode 100644 index f942a8fd9ea3340a68c7a2dcf706159c814e17f1..0000000000000000000000000000000000000000 --- "a/2209040037/chapter_3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\347\232\204\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include -#include - -bool place(int* paraSolution, int paraT) -{ - int j; - for(j = 1;j < paraT;j++) - { - if(abs(paraT-j) == abs(paraSolution[paraT]-paraSolution[j]) || paraSolution[paraT] == paraSolution[j]) - return 0; - } - return 1; -} - -void backtracking(int* paraSolution,int paraN,int paraT) -{ - int i; - if(paraT > paraN) - { - for(i = 1; i <= paraN; i++) - { - printf("%d ",paraSolution[i]); - } - printf("\r\n"); - } - else - { - for(i = 1;i <= paraN; i++) - { - paraSolution[paraT] = i; - if(place(paraSolution,paraT)) - { - backtracking(paraSolution,paraN,paraT+1); - } - } - } -} - -void nQueen(int paraN) -{ - int i; - int* solution = (int*)malloc((paraN + 1) * sizeof(int)); - for(i = 0;i <= paraN; i++) - { - solution[i] = 0; - } - backtracking(solution, paraN ,1); -} - -int main() -{ - nQueen(5); - return 1; -} diff --git "a/2209040037/chapter_3/Ic_225\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" "b/2209040039/chapter2/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" similarity index 95% rename from "2209040037/chapter_3/Ic_225\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" rename to "2209040039/chapter2/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" index 40b6c40c008b139f95df7c551bdfadb8d1a8ca5f..ea78b76d4c69507703c3a7c585173589858a9af7 100644 --- "a/2209040037/chapter_3/Ic_225\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" +++ "b/2209040039/chapter2/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" @@ -96,3 +96,4 @@ void myStackFree(MyStack *obj) { free(obj); obj = NULL; } + diff --git "a/2209040041/chapter_3/lc_150\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" "b/2209040039/chapter3/lc_150\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" similarity index 96% rename from "2209040041/chapter_3/lc_150\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" rename to "2209040039/chapter3/lc_150\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" index 9e2aead4d2dc42c93da22c8a19b9ac25d08bc782..6d8b35dcb83a588493a5679efee6c6d8db624016 100644 --- "a/2209040041/chapter_3/lc_150\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" +++ "b/2209040039/chapter3/lc_150\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" @@ -1,32 +1,32 @@ -bool isNumber(char* token) { - return strlen(token) > 1 || ('0' <= token[0] && token[0] <= '9'); -} - -int evalRPN(char** tokens, int tokensSize) { - int n = tokensSize; - int stk[n], top = 0; - for (int i = 0; i < n; i++) { - char* token = tokens[i]; - if (isNumber(token)) { - stk[top++] = atoi(token); - } else { - int num2 = stk[--top]; - int num1 = stk[--top]; - switch (token[0]) { - case '+': - stk[top++] = num1 + num2; - break; - case '-': - stk[top++] = num1 - num2; - break; - case '*': - stk[top++] = num1 * num2; - break; - case '/': - stk[top++] = num1 / num2; - break; - } - } - } - return stk[top - 1]; -} \ No newline at end of file +bool isNumber(char* token) { + return strlen(token) > 1 || ('0' <= token[0] && token[0] <= '9'); +} + +int evalRPN(char** tokens, int tokensSize) { + int n = tokensSize; + int stk[n], top = 0; + for (int i = 0; i < n; i++) { + char* token = tokens[i]; + if (isNumber(token)) { + stk[top++] = atoi(token); + } else { + int num2 = stk[--top]; + int num1 = stk[--top]; + switch (token[0]) { + case '+': + stk[top++] = num1 + num2; + break; + case '-': + stk[top++] = num1 - num2; + break; + case '*': + stk[top++] = num1 * num2; + break; + case '/': + stk[top++] = num1 / num2; + break; + } + } + } + return stk[top - 1]; +} diff --git "a/2209040037/chapter_3/Ic_20\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" "b/2209040039/chapter3/lc_20\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" similarity index 100% rename from "2209040037/chapter_3/Ic_20\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" rename to "2209040039/chapter3/lc_20\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" diff --git "a/2209040037/chapter_3/Ic_227\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" "b/2209040039/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\2502.cpp" similarity index 96% rename from "2209040037/chapter_3/Ic_227\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" rename to "2209040039/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\2502.cpp" index 81f8b14a7762e27d37a960eb62d63ddfdae11e7b..541e464748000b4711eeab0b52e416928e705105 100644 --- "a/2209040037/chapter_3/Ic_227\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" +++ "b/2209040039/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\2502.cpp" @@ -31,3 +31,4 @@ int calculate(char* s) { } return ret; } + diff --git "a/2209040041/chapter_3/lc_227\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" "b/2209040039/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\2502.cpp~HEAD" similarity index 95% rename from "2209040041/chapter_3/lc_227\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" rename to "2209040039/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\2502.cpp~HEAD" index 71d774026d1c426b9fce019f5c1a465fdbfb3440..541e464748000b4711eeab0b52e416928e705105 100644 --- "a/2209040041/chapter_3/lc_227\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" +++ "b/2209040039/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\2502.cpp~HEAD" @@ -1,33 +1,34 @@ -int calculate(char* s) { - int n = strlen(s); - int stk[n], top = 0; - char preSign = '+'; - int num = 0; - for (int i = 0; i < n; ++i) { - if (isdigit(s[i])) { - num = num * 10 + (int)(s[i] - '0'); - } - if (!isdigit(s[i]) && s[i] != ' ' || i == n - 1) { - switch (preSign) { - case '+': - stk[top++] = num; - break; - case '-': - stk[top++] = -num; - break; - case '*': - stk[top - 1] *= num; - break; - default: - stk[top - 1] /= num; - } - preSign = s[i]; - num = 0; - } - } - int ret = 0; - for (int i = 0; i < top; i++) { - ret += stk[i]; - } - return ret; -} \ No newline at end of file +int calculate(char* s) { + int n = strlen(s); + int stk[n], top = 0; + char preSign = '+'; + int num = 0; + for (int i = 0; i < n; ++i) { + if (isdigit(s[i])) { + num = num * 10 + (int)(s[i] - '0'); + } + if (!isdigit(s[i]) && s[i] != ' ' || i == n - 1) { + switch (preSign) { + case '+': + stk[top++] = num; + break; + case '-': + stk[top++] = -num; + break; + case '*': + stk[top - 1] *= num; + break; + default: + stk[top - 1] /= num; + } + preSign = s[i]; + num = 0; + } + } + int ret = 0; + for (int i = 0; i < top; i++) { + ret += stk[i]; + } + return ret; +} + diff --git "a/2209040070/chapter_3/lc_227\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" "b/2209040039/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\2502.cpp~master" similarity index 95% rename from "2209040070/chapter_3/lc_227\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" rename to "2209040039/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\2502.cpp~master" index 71d774026d1c426b9fce019f5c1a465fdbfb3440..541e464748000b4711eeab0b52e416928e705105 100644 --- "a/2209040070/chapter_3/lc_227\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" +++ "b/2209040039/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\2502.cpp~master" @@ -1,33 +1,34 @@ -int calculate(char* s) { - int n = strlen(s); - int stk[n], top = 0; - char preSign = '+'; - int num = 0; - for (int i = 0; i < n; ++i) { - if (isdigit(s[i])) { - num = num * 10 + (int)(s[i] - '0'); - } - if (!isdigit(s[i]) && s[i] != ' ' || i == n - 1) { - switch (preSign) { - case '+': - stk[top++] = num; - break; - case '-': - stk[top++] = -num; - break; - case '*': - stk[top - 1] *= num; - break; - default: - stk[top - 1] /= num; - } - preSign = s[i]; - num = 0; - } - } - int ret = 0; - for (int i = 0; i < top; i++) { - ret += stk[i]; - } - return ret; -} \ No newline at end of file +int calculate(char* s) { + int n = strlen(s); + int stk[n], top = 0; + char preSign = '+'; + int num = 0; + for (int i = 0; i < n; ++i) { + if (isdigit(s[i])) { + num = num * 10 + (int)(s[i] - '0'); + } + if (!isdigit(s[i]) && s[i] != ' ' || i == n - 1) { + switch (preSign) { + case '+': + stk[top++] = num; + break; + case '-': + stk[top++] = -num; + break; + case '*': + stk[top - 1] *= num; + break; + default: + stk[top - 1] /= num; + } + preSign = s[i]; + num = 0; + } + } + int ret = 0; + for (int i = 0; i < top; i++) { + ret += stk[i]; + } + return ret; +} + diff --git "a/2209040042/chapter3/\345\256\236\350\241\214\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2209040039/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" similarity index 99% rename from "2209040042/chapter3/\345\256\236\350\241\214\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" rename to "2209040039/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" index 6ae25210183d5280310368e2dbf5a362b5af7a23..281433fa4f7f36117f2868d81426224c4fe480d5 100644 --- "a/2209040042/chapter3/\345\256\236\350\241\214\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ "b/2209040039/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" @@ -69,4 +69,4 @@ int main() printf("\n"); printf(" (7)释放队列\n"); DestroyQueue(q); -} \ No newline at end of file +} diff --git "a/2209040070/chapter_3/\345\256\236\347\216\260\347\216\257\345\275\242\345\210\227\351\230\237\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2209040039/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp~HEAD" similarity index 43% rename from "2209040070/chapter_3/\345\256\236\347\216\260\347\216\257\345\275\242\345\210\227\351\230\237\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" rename to "2209040039/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp~HEAD" index a8af801e5923198055acb9541130294619d38678..07057eb979acfa59409195d73ef06227a1ae5c99 100644 --- "a/2209040070/chapter_3/\345\256\236\347\216\260\347\216\257\345\275\242\345\210\227\351\230\237\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ "b/2209040039/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp~HEAD" @@ -1,73 +1,72 @@ -#include -#include -#define MaxSize 5 - -typedef char ElemType; -typedef struct -{ - ElemType data[MaxSize]; - int front,rear; -} SqQueue; -void InitQueue(SqQueue *&q) -{ q=(SqQueue *)malloc (sizeof(SqQueue)); - q->front=q->rear=0; -} - -void DestroyQueue(SqQueue *&q) -{ - free(q); -} -bool QueueEmpty(SqQueue *q) -{ - return(q->front==q->rear); -} -bool enQueue(SqQueue *&q,ElemType e) -{ - if ((q->rear+1)%MaxSize==q->front) - return false; - q->rear=(q->rear+1)%MaxSize; - q->data[q->rear]=e; - return true; -} -bool deQueue(SqQueue *&q,ElemType &e) -{ - if (q->front==q->rear) - return false; - q->front=(q->front+1)%MaxSize; - e=q->data[q->front]; - return true; -} -extern void InitQueue(SqQueue *&q); -extern void DestroyQueue(SqQueue *&q); -extern bool QueueEmpty(SqQueue *q); -extern bool enQueue(SqQueue *&q,ElemType e); -extern bool deQueue(SqQueue *&q,ElemType &e); -int main() -{ - ElemType e; - SqQueue *q; - printf("环形队列基本运算如下:\n"); - printf(" (1)初始化队列q\n"); - InitQueue(q); - printf(" (2)依次进队列元素a,b,c\n"); - if (!enQueue(q,'a')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'b')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'c')) printf("\t提示:队满,不能进队\n"); - printf(" (3)队列为%s\n",(QueueEmpty(q)?"空":"非空")); - if (deQueue(q,e)==0) - printf("队空,不能出队\n"); - else - printf(" (4)出队一个元素%c\n",e); - printf(" (5)依次进队列元素d,e,f\n"); - if (!enQueue(q,'d')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'e')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'f')) printf("\t提示:队满,不能进队\n"); - printf(" (6)出队列序列:"); - while (!QueueEmpty(q)) - { deQueue(q,e); - printf("%c ",e); - } - printf("\n"); - printf(" (7)释放队列\n"); - DestroyQueue(q); -} \ No newline at end of file +#include +#include +#define MaxSize 5 +typedef char ElemType; +typedef struct +{ + ElemType data[MaxSize]; + int front,rear; //׺Ͷβָ +} SqQueue; +void InitQueue(SqQueue *&q) //ʼ +{ q=(SqQueue *)malloc (sizeof(SqQueue)); + q->front=q->rear=0; +} +void DestroyQueue(SqQueue *&q) //ٶ +{ + free(q); +} +bool QueueEmpty(SqQueue *q) //ж϶п +{ + return(q->front==q->rear); +} +bool enQueue(SqQueue *&q,ElemType e) // +{ + if ((q->rear+1)%MaxSize==q->front) // + return false; + q->rear=(q->rear+1)%MaxSize; + q->data[q->rear]=e; + return true; +} +bool deQueue(SqQueue *&q,ElemType &e) // +{ + if (q->front==q->rear) //ӿ + return false; + q->front=(q->front+1)%MaxSize; + e=q->data[q->front]; + return true; +} + +extern void InitQueue(SqQueue *&q); +extern void DestroyQueue(SqQueue *&q); +extern bool QueueEmpty(SqQueue *q); +extern bool enQueue(SqQueue *&q,ElemType e); +extern bool deQueue(SqQueue *&q,ElemType &e); +int main() +{ + ElemType e; + SqQueue *q; + printf("ζл:\n"); + printf(" (1)ʼq\n"); + InitQueue(q); + printf(" (2)νԪa,b,c\n"); + if (!enQueue(q,'a')) printf("\tʾ:,ܽ\n"); + if (!enQueue(q,'b')) printf("\tʾ:,ܽ\n"); + if (!enQueue(q,'c')) printf("\tʾ:,ܽ\n"); + printf(" (3)Ϊ%s\n",(QueueEmpty(q)?"":"ǿ")); + if (deQueue(q,e)==0) + printf("ӿ,ܳ\n"); + else + printf(" (4)һԪ%c\n",e); + printf(" (5)νԪd,e,f\n"); + if (!enQueue(q,'d')) printf("\tʾ:,ܽ\n"); + if (!enQueue(q,'e')) printf("\tʾ:,ܽ\n"); + if (!enQueue(q,'f')) printf("\tʾ:,ܽ\n"); + printf(" (6):"); + while (!QueueEmpty(q)) + { deQueue(q,e); + printf("%c ",e); + } + printf("\n"); + printf(" (7)ͷŶ\n"); + DestroyQueue(q); +} diff --git "a/2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\350\277\220\347\256\227.cpp" "b/2209040039/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp~master" similarity index 99% rename from "2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\350\277\220\347\256\227.cpp" rename to "2209040039/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp~master" index 4378f4fc0434e4e96b944c1ad0a14dc85bdebcd1..281433fa4f7f36117f2868d81426224c4fe480d5 100644 --- "a/2209040019/\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\350\277\220\347\256\227.cpp" +++ "b/2209040039/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp~master" @@ -35,6 +35,7 @@ bool deQueue(SqQueue *&q,ElemType &e) //出队 e=q->data[q->front]; return true; } + extern void InitQueue(SqQueue *&q); extern void DestroyQueue(SqQueue *&q); extern bool QueueEmpty(SqQueue *q); @@ -68,4 +69,4 @@ int main() printf("\n"); printf(" (7)释放队列\n"); DestroyQueue(q); -} \ No newline at end of file +} diff --git "a/2209040037/chapter_3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2209040039/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" similarity index 100% rename from "2209040037/chapter_3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" rename to "2209040039/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" diff --git "a/2209040070/chapter_3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2209040039/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp~HEAD" similarity index 65% rename from "2209040070/chapter_3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" rename to "2209040039/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp~HEAD" index 8c5f7171a11b8b84d9bb9c72130355c64dbe2f65..6bd0bd97df3f220fb7b8b7ea9732eca746b4c81d 100644 --- "a/2209040070/chapter_3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ "b/2209040039/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp~HEAD" @@ -1,73 +1,75 @@ -#include -#include -#define Maxsize 50 -typedef char ElemType; -char Z; -typedef struct -{ - ElemType data[Maxsize]; - int top; -}SqStack; - -void InitStack(SqStack*& s) -{ - s = (SqStack*)malloc(sizeof(SqStack)); - s->top = -1; -} -void DestroyStack(SqStack*& s) -{ - free(s); -} -bool StackEmpty(SqStack *s) -{ - return(s->top == -1); -} -bool Push(SqStack*& s, ElemType e) -{ - if (s->top == Maxsize - 1) - return false; - s->top++; - s->data[s->top] = e; - return true; -} -bool Pop(SqStack*& s, ElemType& e) -{ - if (s->top == -1) - return false; - e = s->data[s->top]; - s->top--; - return true; -} -bool GetTop(SqStack* s, ElemType& e) -{ - if (s->top == -1) - return false; - e = s->data[s->top]; - return true; -} -int main() -{ - SqStack* s; - printf("顺序栈的基本运算\n"); - printf("初始化栈\n"); - InitStack(s); - printf("判断栈s是否非空: %s\n", (StackEmpty(s) ? "空" : "非空")); - printf("依次进栈元素a,b,c,c,d,e\n"); - Push(s, 'a'); - Push(s, 'b'); - Push(s, 'c'); - Push(s, 'd'); - Push(s, 'e'); - printf("判断栈s是否非空: %s\n", (StackEmpty(s) ? "空" : "非空")); - printf("依次出栈"); - while (s->top!=-1) - { - Pop(s, Z); - printf("%c", Z); - } - printf("\n"); - printf("判断栈s是否非空: %s\n", (StackEmpty(s) ? "空" : "非空")); - printf("释放栈\n"); - DestroyStack(s); - -} \ No newline at end of file +#include +#include +#define Maxsize 50 +typedef char ElemType; +char Z; +typedef struct +{ + ElemType data[Maxsize]; + int top; +}SqStack;//˳ջ + +void InitStack(SqStack*& s) +{ + s = (SqStack*)malloc(sizeof(SqStack)); + s->top = -1; +}//ʼջ + +void DestroyStack(SqStack*& s) +{ + free(s); +}//ջ +bool StackEmpty(SqStack *s) +{ + return(s->top == -1); +} //жջǷΪ +bool Push(SqStack*& s, ElemType e) +{ + if (s->top == Maxsize - 1) + return false; + s->top++; + s->data[s->top] = e; + return true; +}//ջ +bool Pop(SqStack*& s, ElemType& e) +{ + if (s->top == -1) + return false; + e = s->data[s->top]; + s->top--; + return true; +}//ջ +bool GetTop(SqStack* s, ElemType& e) +{ + if (s->top == -1) + return false; + e = s->data[s->top]; + return true; +} //ȡջԪ + +int main() +{ + SqStack* s; + printf("˳ջĻ\n"); + printf("ʼջ\n"); + InitStack(s); + printf("жջsǷǿ: %s\n", (StackEmpty(s) ? "" : "ǿ")); + printf("νջԪa,b,c,c,d,e\n"); + Push(s, 'a'); + Push(s, 'b'); + Push(s, 'c'); + Push(s, 'd'); + Push(s, 'e'); + printf("жջsǷǿ: %s\n", (StackEmpty(s) ? "" : "ǿ")); + printf("γջ"); + while (s->top!=-1) + { + Pop(s, Z); + printf("%c", Z); + } + printf("\n"); + printf("жջsǷǿ: %s\n", (StackEmpty(s) ? "" : "ǿ")); + printf("ͷջ\n"); + DestroyStack(s); + +} diff --git "a/2209040039/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp~master" "b/2209040039/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp~master" new file mode 100644 index 0000000000000000000000000000000000000000..6bd0bd97df3f220fb7b8b7ea9732eca746b4c81d --- /dev/null +++ "b/2209040039/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp~master" @@ -0,0 +1,75 @@ +#include +#include +#define Maxsize 50 +typedef char ElemType; +char Z; +typedef struct +{ + ElemType data[Maxsize]; + int top; +}SqStack;//˳ջ + +void InitStack(SqStack*& s) +{ + s = (SqStack*)malloc(sizeof(SqStack)); + s->top = -1; +}//ʼջ + +void DestroyStack(SqStack*& s) +{ + free(s); +}//ջ +bool StackEmpty(SqStack *s) +{ + return(s->top == -1); +} //жջǷΪ +bool Push(SqStack*& s, ElemType e) +{ + if (s->top == Maxsize - 1) + return false; + s->top++; + s->data[s->top] = e; + return true; +}//ջ +bool Pop(SqStack*& s, ElemType& e) +{ + if (s->top == -1) + return false; + e = s->data[s->top]; + s->top--; + return true; +}//ջ +bool GetTop(SqStack* s, ElemType& e) +{ + if (s->top == -1) + return false; + e = s->data[s->top]; + return true; +} //ȡջԪ + +int main() +{ + SqStack* s; + printf("˳ջĻ\n"); + printf("ʼջ\n"); + InitStack(s); + printf("жջsǷǿ: %s\n", (StackEmpty(s) ? "" : "ǿ")); + printf("νջԪa,b,c,c,d,e\n"); + Push(s, 'a'); + Push(s, 'b'); + Push(s, 'c'); + Push(s, 'd'); + Push(s, 'e'); + printf("жջsǷǿ: %s\n", (StackEmpty(s) ? "" : "ǿ")); + printf("γջ"); + while (s->top!=-1) + { + Pop(s, Z); + printf("%c", Z); + } + printf("\n"); + printf("жջsǷǿ: %s\n", (StackEmpty(s) ? "" : "ǿ")); + printf("ͷջ\n"); + DestroyStack(s); + +} diff --git "a/2209040039/chapter3/\346\234\200\345\260\217\346\240\210.cpp" "b/2209040039/chapter3/\346\234\200\345\260\217\346\240\210.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..ea195c21ba11f5d8f0e044d4f967ef7a9db891e0 --- /dev/null +++ "b/2209040039/chapter3/\346\234\200\345\260\217\346\240\210.cpp" @@ -0,0 +1,58 @@ +typedef struct MinStack +{ + int data; + struct MinStack *next; +} MinStack; +MinStack* minStackCreate() +{ + MinStack * newStack = (MinStack *) malloc(sizeof(MinStack)); + newStack->data = 0; + newStack->next = NULL; + return newStack; +} +void minStackPush(MinStack* obj, int val) +{ + MinStack * newStack = (MinStack *) malloc(sizeof(MinStack)); + newStack->data = val; + newStack->next = obj->next; + obj->next = newStack; +} +void minStackPop(MinStack* obj) +{ + if(obj->next != NULL) + { + MinStack* freeStrack = obj->next ; + obj->next = freeStrack->next; + free(freeStrack); + } +} +int minStackTop(MinStack* obj) +{ + return obj->next->data; +} +int minStackGetMin(MinStack* obj) +{ + MinStack * it = obj->next; + int minValue = it->data; + while(it != NULL) + { + if( it->data < minValue ) + { + minValue = it->data; + } + it = it->next; + } + return minValue; + +} +void minStackFree(MinStack* obj) + { + MinStack * it = obj->next; + MinStack* freeStrack = obj->next ; + if(it != NULL) + { + it = it->next; + free(freeStrack); + freeStrack = it; + } +} diff --git "a/2209040041/chapter_3/lc_20\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" "b/2209040039/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" similarity index 95% rename from "2209040041/chapter_3/lc_20\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" rename to "2209040039/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" index a101958c754caad65e7c8311cbfec0a13221ce0a..a15e0e92776c57cad60f37e517094ef4c4e90514 100644 --- "a/2209040041/chapter_3/lc_20\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" +++ "b/2209040039/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" @@ -1,26 +1,26 @@ -char pairs(char a) { - if (a == '}') return '{'; - if (a == ']') return '['; - if (a == ')') return '('; - return 0; -} - -bool isValid(char* s) { - int n = strlen(s); - if (n % 2 == 1) { - return false; - } - int stk[n + 1], top = 0; - for (int i = 0; i < n; i++) { - char ch = pairs(s[i]); - if (ch) { - if (top == 0 || stk[top - 1] != ch) { - return false; - } - top--; - } else { - stk[top++] = s[i]; - } - } - return top == 0; -} \ No newline at end of file +char pairs(char a) { + if (a == '}') return '{'; + if (a == ']') return '['; + if (a == ')') return '('; + return 0; +} + +bool isValid(char* s) { + int n = strlen(s); + if (n % 2 == 1) { + return false; + } + int stk[n + 1], top = 0; + for (int i = 0; i < n; i++) { + char ch = pairs(s[i]); + if (ch) { + if (top == 0 || stk[top - 1] != ch) { + return false; + } + top--; + } else { + stk[top++] = s[i]; + } + } + return top == 0; +} diff --git "a/2209040037/chapter_3/Ic_232\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" "b/2209040039/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" similarity index 94% rename from "2209040037/chapter_3/Ic_232\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" rename to "2209040039/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" index 336407c37688b729b845f3aebffe1ef82a56b646..f86c20b96fbdf1b08f4eb62fbe181ccf8c715ad9 100644 --- "a/2209040037/chapter_3/Ic_232\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" +++ "b/2209040039/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" @@ -79,3 +79,4 @@ void myQueueFree(MyQueue* obj) { stackFree(obj->inStack); stackFree(obj->outStack); } + diff --git "a/2209040041/chapter_2/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" "b/2209040041/chapter_2/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" deleted file mode 100644 index 7533b804aaaefb07af5656fb05ccfdea55e4aebf..0000000000000000000000000000000000000000 --- "a/2209040041/chapter_2/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,221 +0,0 @@ -#include -#include -#include -#include -#include -typedef struct LNode* PtrToLNode; -typedef int ElementType; -struct LNode { - ElementType Data1, Data2; - PtrToLNode Next; -}; -typedef PtrToLNode List; -List makeEmpty() { - List L = (List)malloc(sizeof(struct LNode)); - L->Next = NULL; - return L; -} -int Length(List L) { - List p = L->Next; - int cut = 0; - while (p != NULL) { - cut++; - p = p->Next; - } - return cut; -} -void Findkth(List L, int k) { - List p = L->Next; - int cnt = 1; - while (p != NULL && cnt < k) { - p = p->Next; - cnt++; - } - if (cnt == k) { - printf("第%d个元素为:", k); - if (p->Data2 == 0)printf("%d\n", p->Data1); - else printf("%dX^%d\n", p->Data1, p->Data2); - } - else - printf("在链表中没有第%d个元素\n", k); -} -List Find(List L, ElementType x) { - List p = L->Next; - while (p && p->Data2 != x) p = p->Next; - if (p)return p; - else return NULL; -} -List Insert(List L, ElementType x, ElementType y, int i) { - List tmp = (List)malloc(sizeof(struct LNode)); - tmp->Data1 = x; tmp->Data2 = y; - int cnt = 0; - List p = L; - while (p && cnt < i - 1) { - p = p->Next; - cnt++; - } - if (p == NULL || cnt != i - 1) - printf("插入位置的参数错误\n!"); - else { - tmp->Next = p->Next; - p->Next = tmp; - } - return L; -} -List Delete(List L, int i) { - List tmp, p = L; - int cnt = 0; - while (p && cnt < i - 1) { - p = p->Next; - cnt++; - } - if (p == NULL || cnt != i - 1 || p->Next == NULL)printf("删除位置参数错误!\n"); - else { - tmp = p->Next; - p->Next = tmp->Next; - free(tmp); - } - return L; -} -void print(List L) { - if (L->Next == NULL) { - printf("0\n"); - return; - } - List p = L->Next; - if (p->Data1 != 0) printf("%d*X^%d", p->Data1, p->Data2); - else printf("0"); - p = L->Next->Next; - while (p != NULL) { - if (p->Data1 == 0) { - p = p->Next; - continue; - } - if (p->Data1 == 1) { - if (p->Data2 == 0) { - if (p->Data1 < 0) printf("%d", p->Data1); - else printf("+%d", p->Data1); - } - else if (p->Data2 == 1) { - if (p->Data1 < 0)printf("X"); - else printf("+X", p->Data1); - } - else { - if (p->Data1 < 0)printf("X^%d", p->Data1, p->Data2); - else printf("+X^%d", p->Data1, p->Data2); - } - } - else { - if (p->Data2 == 0) { - if (p->Data1 < 0) printf("%d", p->Data1); - else printf("+%d", p->Data1); - } - else if(p->Data2==1){ - if (p->Data1 < 0)printf("%d*X", p->Data1); - else printf("+%d*X", p->Data1); - } - else { - if (p->Data1 < 0)printf("%d*X^%d", p->Data1, p->Data2); - else printf("+%d*X^%d", p->Data1, p->Data2); - } - } - p = p->Next; - } - printf("\n"); -} -List List_sort(List L) { - if (L->Next == NULL)return L; - List p, q; - p = L->Next; - int i = 0; - for (p = L->Next; p!= NULL; p = p->Next) - for (q = p->Next; q!= NULL; q = q->Next) - ElementType t = p->Data2; - p->Data2 = q->Data2; - q->Data2 = t; - t = p->Data1; - p->Data1 = q->Data1; - q->Data1 = t; - } - return L; -} -List add(List a, List b) { - List p = b->Next; - while (p != NULL) { - List q = a->Next; - int flag = 0; - while (q != NULL) { - if (p->Data2 == q->Data2) { - q->Data1 += p->Data1; - flag = 1; - } - q = q->Next; - } - if (flag == 0)Insert(a, p->Data1, p->Data2, Length(a) + 1); - p = p->Next; - } - return a; -} -#include -List Multiplication(List a, List b) { - List c = makeEmpty(); - List p = a->Next; - List d = c; - while (p != NULL) { - List q = b->Next; - while (q != NULL) { - List s = (List)malloc(sizeof(struct LNode)); - s->Data1 = p->Data1 * q->Data1; - s->Data2 = p->Data2 + q->Data2; - d->Next = s; - s->Next = NULL; - d = s; - q = q->Next; - } - p = p->Next; - } - return c; -} -List new_List_sort(List L) { - List p = L->Next; - while (p != NULL) { - List q = p->Next; - while (q != NULL) { - if (p->Data2 == q->Data2) { - p->Data1 += q->Data1; - q->Data1 = 0; - } - q = q->Next; - } - p = p->Next; - } - return L; -} -int main() { - List a = makeEmpty(); - List b = makeEmpty(); - int na, nb; - printf("请分别输入多项式a与多项式b的非0系数的个数:\n"); - scanf("%d %d", &na, &nb); - printf("请输入多项式a的各项系数与指数:\n"); - ElementType x, y; - if (na != 0) { - while (na--) { - scanf("%d %d", &x, &y); - a = Insert(a, x, y, Length(a) + 1); - } - } - if (nb != 0) { - printf("请输入多项式b的各项系数与指数:\n"); - while (nb--) { - scanf("%d %d", &x, &y); - b = Insert(b, x, y, Length(b) + 1); - } - } - List c = Multiplication(a, b); - List_sort(c); - new_List_sort(c); - printf("两个多项式相乘后得到的结果为:\n"); - print(c); - return 0; -} diff --git "a/2209040041/chapter_2/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2209040041/chapter_2/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index b4be54b6dcae657556066b7d8f121adac27a3c9f..0000000000000000000000000000000000000000 --- "a/2209040041/chapter_2/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,171 +0,0 @@ -#include -#include -typedef char ElemType; -typedef struct LNode -{ - ElemType data; - struct LNode *next; -}LinkNode; -void CreateListF(LinkNode *&L,ElemType a[],int n) -{ - LinkNode *s; - L=(LinkNode*)malloc(sizeof(LinkNode)); - L->next=NULL; - for(int i=0;idata=a[i]; - s->next=L->next; - L->next=s; - } -} -void InitList(LinkNode *&L) -{ - L=(LinkNode*)malloc(sizeof(LinkNode)); - L->next=NULL; -} -void DestroyList(LinkNode *&L) -{ - LinkNode *pre=L,*p=pre->next; - while(p!=NULL) - { - free(pre); - pre=p; - p=pre->next; - } - free(pre); -} -bool ListEmpty(LinkNode *L) -{ - return(L->next==NULL); -} -int ListLength(LinkNode *L) -{ - int i=0; - LinkNode *p=L; - while(p->next!=NULL) - { - i++; - p=p->next; - } - return(i); -} -void DispList(LinkNode*L) -{ - LinkNode *p=L->next; - while(p!=NULL) - { - printf("%c ",p->data); - p=p->next; - } - printf("\n"); -} -bool GetElem(LinkNode *L,int i,ElemType &e) -{ - int j=0; - LinkNode *p=L; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - e=p->data; - return true; - } -} -int LocateElem(LinkNode*L,ElemType e) -{ - int i=1; - LinkNode*p=L->next; - while(p!=NULL&&p->data!=e) - { - p=p->next; - i++; - } - if(p==NULL) - return(0); - else - return(i); -} -bool ListInsert(LinkNode *&L,int i,ElemType e) -{ - int j=0; - LinkNode *p=L,*s; - if(i<=0)return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - s=(LinkNode*)malloc(sizeof(LinkNode)); - s->data=e; - s->next=p->next; - p->next=s; - return true; - } -} -bool ListDelete(LinkNode*&L,int i,ElemType &e) -{ - int j=0; - LinkNode*p=L,*q; - if(i<=0)return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - q=p->next; - if(q==NULL) - return false; - e=q->data; - p->next=q->next; - free(q); - return true; - } -} -int main() -{ - LinkNode *h; - ElemType e; - printf("单链表的基本运算如下:\n"); - printf("(1)初始化单链表h\n"); - InitList(h); - printf("(2)依次采用尾插法插入元素a,b,c,d,e\n"); - ListInsert(h,1,'a'); - ListInsert(h,2,'b'); - ListInsert(h,3,'c'); - ListInsert(h,4,'d'); - ListInsert(h,5,'e'); - printf("(3)输出单链表h:"); - DispList(h); - printf("(4)单链表h长度:%d\n",ListLength(h)); - printf("(5)单链表h为%s\n",(ListEmpty(h)?"空":"非空")); - GetElem(h,3,e); - printf("(6)单链表h的第3个元素:%c\n",e); - printf("(7)元素a的位置:%d\n",LocateElem(h,'a')); - printf("(8)在第4个元素位置上插入元素f\n"); - ListInsert(h,4,'f'); - printf("(9)输出单链表h:"); - DispList(h); - printf("(10)删除h的第3个元素\n"); - ListDelete(h,3,e); - printf("(11)输出单链表h:"); - DispList(h); - printf("(12)释放单链表h\n"); - DestroyList(h); - return 1; - } \ No newline at end of file diff --git "a/2209040041/chapter_2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" "b/2209040041/chapter_2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" deleted file mode 100644 index 4965e7a03148b95712119ce4fabc61dae9a4a4eb..0000000000000000000000000000000000000000 --- "a/2209040041/chapter_2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" +++ /dev/null @@ -1,126 +0,0 @@ -#include -#include -#include -typedef struct LinkNode -{ - int data; - struct LinkNode *next; -}LinkNode; -void InitList(LinkNode *&L) -{ - L=(LinkNode*)malloc(sizeof(LinkNode)); - L->next=NULL; -} -void CreatList(LinkNode* L,int a[],int n) -{ - LinkNode *s; - for(int i=0;idata=a[i]; - s->next=L->next; - L->next=s; - } -} -void DisList(LinkNode *L) -{ - LinkNode *p=L->next; - while(p!=NULL) - { - printf("%d ",p->data); - p=p->next; - } - printf("\n"); -} - -void sort(LinkNode* L,int num) -{ - LinkNode *p= L->next,*t = NULL, *h = NULL, *s = NULL; - L->next = NULL; - while (p) { - t = p; - p = p->next; - t->next = NULL; - if (!L->next) - L->next = t; - else if (t->data > num) - { - h = L; - while (h->next) h = h->next; - h->next = t; - } - else { - h = L->next; s = L; - while (h && h->data < num) s = h, h = h->next; - if (s == L) { - t->next = L->next; - L->next = t; - } - else { - t->next = s->next; - s->next = t; - } - } - } -} -int main() -{ - int n,e[100]; - int x; - LinkNode *L; - InitList(L); - - scanf("%d",&n); - for(int i=0;inext; - while(p!=NULL) - { - if(p->data==e) - { - break; - } - p=p->next; - } - if(p==NULL) - { - return false; - } - else - { - return true; - } -} -void fun(int a[],int n) -{ - int j=0,i=1; - int t; - for(i=1;i<=n-1;i++) - { - for(j=0;j<=n-1-i;j++) - { - if(a[j]>a[j+1]) - { - t=a[j]; - a[j]=a[j+1]; - a[j+1]=t; - } - } - } - for(int k=0;kdata = (int*)malloc(sizeof(int) * MAX_SIZE); - obj->top = -1; - obj->min = INT_MAX; - return obj; -} - -void minStackPush(MinStack* obj, int val) { - if (obj->top >= MAX_SIZE) { - return; - } - obj->data[++(obj->top)] = val; - if (val < obj->min) { - obj->min = val; - } -} - -void minStackPop(MinStack* obj) { - if (obj->top < 0) { - return; - } - obj->top--; - if (obj->top >= 0) { - obj->min = obj->data[obj->top]; - } else { - obj->min = INT_MAX; - } - for (int i = obj->top; i >= 0; i--) { - if (obj->data[i] < obj->min) { - obj->min = obj->data[i]; - } - } -} - -int minStackTop(MinStack* obj) { - return obj->data[obj->top]; -} - -int minStackGetMin(MinStack* obj) { - if (obj->top < 0) { - return -1; - } - return obj->min; -} - -void minStackFree(MinStack* obj) { - free(obj); -} \ No newline at end of file diff --git "a/2209040041/chapter_3/lc_225\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" "b/2209040041/chapter_3/lc_225\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" deleted file mode 100644 index ff0b261a685d22ec37bcda54d26485c1e67ba181..0000000000000000000000000000000000000000 --- "a/2209040041/chapter_3/lc_225\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" +++ /dev/null @@ -1,98 +0,0 @@ -#define LEN 20 -typedef struct queue { - int *data; - int head; - int rear; - int size; -} Queue; - -typedef struct { - Queue *queue1, *queue2; -} MyStack; - -Queue *initQueue(int k) { - Queue *obj = (Queue *)malloc(sizeof(Queue)); - obj->data = (int *)malloc(k * sizeof(int)); - obj->head = -1; - obj->rear = -1; - obj->size = k; - return obj; -} - -void enQueue(Queue *obj, int e) { - if (obj->head == -1) { - obj->head = 0; - } - obj->rear = (obj->rear + 1) % obj->size; - obj->data[obj->rear] = e; -} - -int deQueue(Queue *obj) { - int a = obj->data[obj->head]; - if (obj->head == obj->rear) { - obj->rear = -1; - obj->head = -1; - return a; - } - obj->head = (obj->head + 1) % obj->size; - return a; -} - -int isEmpty(Queue *obj) { - return obj->head == -1; -} - -MyStack *myStackCreate() { - MyStack *obj = (MyStack *)malloc(sizeof(MyStack)); - obj->queue1 = initQueue(LEN); - obj->queue2 = initQueue(LEN); - return obj; -} - -void myStackPush(MyStack *obj, int x) { - if (isEmpty(obj->queue1)) { - enQueue(obj->queue2, x); - } else { - enQueue(obj->queue1, x); - } -} - -int myStackPop(MyStack *obj) { - if (isEmpty(obj->queue1)) { - while (obj->queue2->head != obj->queue2->rear) { - enQueue(obj->queue1, deQueue(obj->queue2)); - } - return deQueue(obj->queue2); - } - while (obj->queue1->head != obj->queue1->rear) { - enQueue(obj->queue2, deQueue(obj->queue1)); - } - return deQueue(obj->queue1); -} - -int myStackTop(MyStack *obj) { - if (isEmpty(obj->queue1)) { - return obj->queue2->data[obj->queue2->rear]; - } - return obj->queue1->data[obj->queue1->rear]; -} - -bool myStackEmpty(MyStack *obj) { - if (obj->queue1->head == -1 && obj->queue2->head == -1) { - return true; - } - return false; -} - -void myStackFree(MyStack *obj) { - free(obj->queue1->data); - obj->queue1->data = NULL; - free(obj->queue1); - obj->queue1 = NULL; - free(obj->queue2->data); - obj->queue2->data = NULL; - free(obj->queue2); - obj->queue2 = NULL; - free(obj); - obj = NULL; -} \ No newline at end of file diff --git "a/2209040041/chapter_3/lc_232\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" "b/2209040041/chapter_3/lc_232\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" deleted file mode 100644 index d68e5e31481338917165a33e47a42a2353eee513..0000000000000000000000000000000000000000 --- "a/2209040041/chapter_3/lc_232\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" +++ /dev/null @@ -1,81 +0,0 @@ -typedef struct { - int* stk; - int stkSize; - int stkCapacity; -} Stack; - -Stack* stackCreate(int cpacity) { - Stack* ret = malloc(sizeof(Stack)); - ret->stk = malloc(sizeof(int) * cpacity); - ret->stkSize = 0; - ret->stkCapacity = cpacity; - return ret; -} - -void stackPush(Stack* obj, int x) { - obj->stk[obj->stkSize++] = x; -} - -void stackPop(Stack* obj) { - obj->stkSize--; -} - -int stackTop(Stack* obj) { - return obj->stk[obj->stkSize - 1]; -} - -bool stackEmpty(Stack* obj) { - return obj->stkSize == 0; -} - -void stackFree(Stack* obj) { - free(obj->stk); -} - -typedef struct { - Stack* inStack; - Stack* outStack; -} MyQueue; - -MyQueue* myQueueCreate() { - MyQueue* ret = malloc(sizeof(MyQueue)); - ret->inStack = stackCreate(100); - ret->outStack = stackCreate(100); - return ret; -} - -void in2out(MyQueue* obj) { - while (!stackEmpty(obj->inStack)) { - stackPush(obj->outStack, stackTop(obj->inStack)); - stackPop(obj->inStack); - } -} - -void myQueuePush(MyQueue* obj, int x) { - stackPush(obj->inStack, x); -} - -int myQueuePop(MyQueue* obj) { - if (stackEmpty(obj->outStack)) { - in2out(obj); - } - int x = stackTop(obj->outStack); - stackPop(obj->outStack); - return x; -} - -int myQueuePeek(MyQueue* obj) { - if (stackEmpty(obj->outStack)) { - in2out(obj); - } - return stackTop(obj->outStack); -} - -bool myQueueEmpty(MyQueue* obj) { - return stackEmpty(obj->inStack) && stackEmpty(obj->outStack); -} - -void myQueueFree(MyQueue* obj) { - stackFree(obj->inStack); - stackFree(obj->outStack); -} \ No newline at end of file diff --git "a/2209040042/chapter2/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" "b/2209040042/chapter2/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" deleted file mode 100644 index f72024d3ba4de12d452eb87d317b89c06a7dd003..0000000000000000000000000000000000000000 --- "a/2209040042/chapter2/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,225 +0,0 @@ -#include -#include -#define MAX 100 //多项式最多项数 -typedef struct -{ - double coef; //系数 - int exp; //指数 -}PolyArray; //存放多项式的数组类型 - -typedef struct pnode -{ - double coef; //系数 - int exp; //指数 - struct pnode* next; -}PolyNode; //声明多项式单链表结点类型 - -void CreatePolyR(PolyNode*& L, PolyArray a[], int n) //尾插法建表 -{ - PolyNode* s, * r; - int i; - L = (PolyNode*)malloc(sizeof(PolyNode)); - L->next = NULL; - r = L; //r始终指向尾结点,开始时指向头结点 - for (i = 0; i < n; i++) - { - s = (PolyNode*)malloc(sizeof(PolyNode)); //创建新结点 - s->coef = a[i].coef; - s->exp = a[i].exp; - r->next = s; //将结点s插入到结点r之后 - r = s; - } - r->next = NULL; //尾结点next域置为NULL -} - -void DispPoly(PolyNode* L) //输出多项式单链表 -{ - PolyNode* p = L->next; - while (p != NULL) - { - if (p->coef > 0) printf("+");//系数为正,用+连接 - if (p->exp == 0) printf("%g", p->coef); //指数为0,只输出系数 - else if (p->exp == 1) printf("%gx", p->coef); //指数为1 - else printf("%gx^%d", p->coef, p->exp); //指数不为1 - p = p->next; - } - printf("\n"); -} - -void DestoryPoly(PolyNode*& L) //销毁多项式单链表 -{ - PolyNode* pre = L, * p = pre->next; - while (p != NULL) - { - free(pre); - pre = p; - p = pre->next; - } - free(pre); -} - -void Sort(PolyNode*& L) //将多项式单链表按指数递减排序 -{ - PolyNode* p = L->next, * pre, * q; - if (p != NULL) //L有一个或以上的数据结点 - { - q = p->next; //q保存p结点的后继节点 - p->next = NULL; //构造只含一个数据结点的有序表 - p = q; - while (p != NULL) //扫描原L中余下的数据结点 - { - q = p->next; //q保存p结点的后继节点 - pre = L; - while (pre->next != NULL && pre->next->exp > p->exp) - pre = pre->next; //在有序表中找插入节点p的前驱结点pre - p->next = pre->next; //将结点p插入到结点pre之后 - pre->next = p; - p = q; //扫描原单链表余下的结点 - } - } -} - -void Add(PolyNode* ha, PolyNode* hb, PolyNode*& hc) //ha和hb相加得到hc -{ - PolyNode* pa = ha->next, * pb = hb->next, * s, * r; - double c; - hc = (PolyNode*)malloc(sizeof(PolyNode)); - r = hc; //r指向尾结点,初始时指向头结点 - while (pa != NULL && pb != NULL) //pa、pb均没有扫描完 - { - if (pa->exp > pb->exp) //将指数较大的pa结点复制到hc中 - { - s = (PolyNode*)malloc(sizeof(PolyNode)); - s->exp = pa->exp; - s->coef = pa->coef; - r->next = s; - r = s; - pa = pa->next; - } - else if(pa->exp < pb->exp) //将指数较大的pb结点复制到hc中 - { - s = (PolyNode*)malloc(sizeof(PolyNode)); - s->exp = pb->exp; - s->coef = pb->coef; - r->next = s; - r = s; - pb = pb->next; - } - else //pa、pb结点的指数相等时 - { - c = pa->coef + pb->coef; //求两个结点的系数和c - if (c != 0) //若系数和不为0时创建新结点 - { - s = (PolyNode*)malloc(sizeof(PolyNode)); - s->exp = pa->exp; - s->coef = c; - r->next = s; - r = s; - } - pa = pa->next; //pa后移一个结点 - pb = pb->next; //pb后移一个结点 - } - } - if (pb != NULL) pa = pb; //复制余下的结点 - while (pa != NULL) - { - s = (PolyNode*)malloc(sizeof(PolyNode)); - s->exp = pa->exp; - s->coef = pa->coef; - r->next = s; - r = s; - pa = pa->next; - } - r->next = NULL; //尾结点next设置为空 -} - -void Sub(PolyNode* ha, PolyNode* hb, PolyNode*& hd) //ha和hb相加得到hc -{ - PolyNode* pa = ha->next, * pb = hb->next, * s, * r; - double c; - hd = (PolyNode*)malloc(sizeof(PolyNode)); - r = hd; //r指向尾结点,初始时指向头结点 - while (pa != NULL && pb != NULL) //pa、pb均没有扫描完 - { - if (pa->exp > pb->exp) //将指数较大的pa结点复制到hc中 - { - s = (PolyNode*)malloc(sizeof(PolyNode)); - s->exp = pa->exp; - s->coef = pa->coef; - r->next = s; - r = s; - pa = pa->next; - } - else if (pa->exp < pb->exp) //将指数较大的pb结点复制到hc中 - { - s = (PolyNode*)malloc(sizeof(PolyNode)); - s->exp = pb->exp; - s->coef = 0-pb->coef; - r->next = s; - r = s; - pb = pb->next; - } - else //pa、pb结点的指数相等时 - { - c = pa->coef - pb->coef; //求两个结点的系数和c - if (c != 0) //若系数相减不为0时创建新结点 - { - s = (PolyNode*)malloc(sizeof(PolyNode)); - s->exp = pa->exp; - s->coef = c; - r->next = s; - r = s; - } - pa = pa->next; //pa后移一个结点 - pb = pb->next; //pb后移一个结点 - } - } - if (pb != NULL) pa = pb; //复制余下的结点 - while (pa != NULL) - { - s = (PolyNode*)malloc(sizeof(PolyNode)); - s->exp = pa->exp; - s->coef = pa->coef; - r->next = s; - r = s; - pa = pa->next; - } - r->next = NULL; //尾结点next设置为空 -} - -int main() -{ - PolyNode* ha, * hb, * hc, * hd; - PolyArray a[] = { {3,1},{2,0},{1,3},{4,4} }; - PolyArray b[] = { {3,4},{2,3},{1,0},{5,2},{ 3,6} }; - CreatePolyR(ha, a, 4); - CreatePolyR(hb, b, 5); - printf("原多项式A:"); - DispPoly(ha); - printf("\n"); - printf("原多项式B:"); - DispPoly(hb); - printf("\n"); - Sort(ha); - Sort(hb); - printf("有序多项式A:"); - DispPoly(ha); - printf("\n"); - printf("有序多项式B:"); - DispPoly(hb); - printf("\n"); - - Add(ha, hb, hc); - printf("多项式相加:"); - DispPoly(hc); - printf("\n"); - - Sub(ha, hb, hd); - printf("多项式相减:"); - DispPoly(hd); - printf("\n"); - /*DestoryPoly(ha); - DestoryPoly(hb); - DestoryPoly(hc);*/ - return 0; -} diff --git "a/2209040042/chapter2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\206.cpp" "b/2209040042/chapter2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\206.cpp" deleted file mode 100644 index 5dcbb272490177d1ea710adada76dadb6b3bd999..0000000000000000000000000000000000000000 --- "a/2209040042/chapter2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\206.cpp" +++ /dev/null @@ -1,95 +0,0 @@ -#include -#include -#include - -typedef int ElemType; -typedef struct LNode -{ - ElemType data; - struct LNode* next; -}LinkNode; - -bool DivideList(LinkNode*& L, ElemType x); //L为传入的链表头结点,x为传入的基准 -bool InitList(LinkNode*& L); //链表产生 -bool DispList(LinkNode* L); //链表输出 - -int main(int argc, const char* argv[]) -{ - LinkNode* L = NULL; - int x; - bool flag = false; - InitList(L); - printf("请输入基准。\n"); - scanf("%d", &x); - flag = DivideList(L, x); - if (flag) - printf("算法执行成功。\n"); - else - printf("算法执行失败。\n"); - DispList(L); - return 0; -} - -bool InitList(LinkNode*& L) //建立单链表 -{ - while (!L) { - L = (LinkNode*)malloc(sizeof(LNode)); - } - L->next = NULL; - int i, n; - LinkNode* p = NULL, * q = NULL; - q = L; - printf("请输入数据规模:\n"); - scanf("%d", &n); - printf("请输入数据:\n"); - for (i = 0; i < n; i++) { - while (!p) { - p = (LinkNode*)malloc(sizeof(LNode)); - } - scanf("%d", &p->data); - q->next = p; - q = p; - p = q->next = NULL; - } - return true; -} -bool DispList(LinkNode* L) //输出单链表 -{ - LinkNode* p = L->next; - while (p) { - printf("\t%d", p->data); - p = p->next; - } - printf("\n"); - return true; -} -bool DivideList(LinkNode*& L, ElemType x) //L为传入的链表头结点,x为传入的基准 -{ - if (L->next==NULL) { - printf("单链表不存在。\n"); - return false; - } - LinkNode* p = NULL, * q = NULL, * r = NULL; //p为工作指针,q为小于基准的数的头结点,r为尾指针 - p = L; - //直到申请成功,因为malloc可能申请失败 - while (!q) { - q = (LinkNode*)malloc(sizeof(LinkNode)); - } - r = q; //r指向小于基准的链表的尾巴 - //p从第一个有效结点开始扫描整个链表,直到p后无结点为空 - while (p->next) { - if (p->next->data < x) { - r->next = p->next; //将p接到尾巴后面 - r=r->next; //尾巴后移 - p->next= p->next->next; //将该结点从L中截取出去 - r->next = NULL; //将尾巴与原链表之间的关系切断 - } - else - p = p->next; //p后移 - } - //将小于基准的链表接到大于等于基准的链表的前面 - r->next = L->next; //小于基准的链表的尾巴与大于等于基准的链表相连 - L->next = q->next; //将整体接到头结点后 - free(q); //释放小于基准的链表的头结点 - return true; -} diff --git "a/2209040042/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" "b/2209040042/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" deleted file mode 100644 index d418d5f6202e9ec80a847a6569efbf456e3120e1..0000000000000000000000000000000000000000 --- "a/2209040042/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250\342\205\241.cpp" +++ /dev/null @@ -1,48 +0,0 @@ -class Solution { -public: - int calculate(string s) { - stack operands; //存储表达式中的数值 - stack operators; //存储表达式中的运算符 - int index = 0; - s.erase(remove(s.begin(), s.end(), ' '), s.end()); - while(index < s.size()){ - if(isdigit(s[index])){ - int digit = 0; - for(;index < s.size() && isdigit(s[index]);index++) - digit = 10 * digit + (s[index] - '0'); - operands.push(digit); - } else { - while(operators.size() && !priority(s[index],operators.top())) - calc(operands,operators); - operators.push(s[index]); - index++; - } - } - while(operators.size()) - calc(operands,operators); - return operands.top(); - } - - void calc(stack& operands,stack& operators){ - int result; - int rhs = operands.top();operands.pop();//右操作数 - int lhs = operands.top();operands.pop();//左操作数 - switch(operators.top()){ - case '+':result = lhs + rhs; break; - case '-':result = lhs - rhs; break; - case '*':result = lhs * rhs; break; - case '/':result = lhs / rhs; break; - default: ; - } - operators.pop(); //计算完成后,该运算符要弹栈 - operands.push(result); //将新计算出来的结果入栈 - return; - } - bool priority(int a,int b){ - if(a == '+' || a == '-') - return false; - else if(a=='*' || a=='/') - return (b=='+' || b == '-'); - return false; - } -}; \ No newline at end of file diff --git "a/2209040042/chapter3/\345\256\236\350\241\214\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2209040042/chapter3/\345\256\236\350\241\214\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index 665f022b380e9db4a961d075a4e5b5406ccb2a03..0000000000000000000000000000000000000000 --- "a/2209040042/chapter3/\345\256\236\350\241\214\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct -{ - ElemType data[MaxSize]; - int top; -} SqStack; -void InitStack(SqStack *&s) -{ - s=(SqStack *)malloc(sizeof(SqStack)); - s->top=-1; -} -void DestroyStack(SqStack *&s) -{ - free(s); -} -bool StackEmpty(SqStack *s) -{ - return (s->top == -1); -} -bool Push(SqStack *&s,ElemType e) -{ - if(s->top==MaxSize-1) - return false; - s->top++; - s->data[s->top]=e; - return true; -} -bool Pop(SqStack *&s,ElemType &e) -{ - if(s->top==MaxSize-1) - return false; - e=s->data[s->top]; - s->top--; - return true; -} -bool GetTop(SqStack *s,ElemType &e) -{ - if(s->top == -1) - return false; - e=s->data[s->top]; - return true; -} -int main() -{ - ElemType e; - SqStack *s; - printf("(1)初始化栈s\n"); - InitStack(s); - printf("(2)判断栈s是否非空:%s\n",(StackEmpty(s)? "是":"否")); - printf("(3)依次进栈元素a、b、c、d、e\n"); - Push(s,'a'); - Push(s,'b'); - Push(s,'c'); - Push(s,'d'); - Push(s,'e'); - printf("(4)判断栈s是否非空:%s\n",((StackEmpty(s))? "是":"否")); - printf("(5)输出出栈序列"); - while(!StackEmpty(s)) - { - Pop(s,e); - printf("%c ",e); - } - printf("\n"); - printf("(6)判断栈s是否非空:%s\n",((StackEmpty(s))? "是":"否")); - GetTop(s,e); - printf("(7)取栈顶元素:%c\n",e); - printf("(8)释放栈"); - DestroyStack(s); - return 0; -} diff --git "a/2209040042/chapter3/\346\234\200\345\260\217\346\240\210.cpp" "b/2209040042/chapter3/\346\234\200\345\260\217\346\240\210.cpp" deleted file mode 100644 index 257c507f5bb12bd04ac1cba314f74fc64eedaead..0000000000000000000000000000000000000000 --- "a/2209040042/chapter3/\346\234\200\345\260\217\346\240\210.cpp" +++ /dev/null @@ -1,41 +0,0 @@ -#include -class MinStack { -public: - stack st; - stack minStack; - - /** 构造函数清空栈容器 */ - MinStack() { - while(!st.empty()) { - st.pop(); - } - while(!minStack.empty()) { - minStack.pop(); - } - /* 初始化最小栈的栈顶元素为最大值为了防止top访问空指针报错 */ - minStack.push(INT_MAX); - } - - void push(int x) { - st.push(x); - /* 比较最小栈的栈顶的值和当前值val的大小,将最小值押入最小栈也就是记录了当前st栈的最小值为栈顶元素 */ - int minVal = std::min(minStack.top(), x); - /* 将最小值押入最小栈 */ - minStack.push(minVal); - } - - void pop() { - /* 弹出两个栈的栈顶元素 */ - st.pop(); - minStack.pop(); - } - - int top() { - return st.top(); - } - - int min() { - /* 取最小栈的栈顶元素就是此时st栈的最小值 */ - return minStack.top(); - } -}; diff --git "a/2209040042/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" "b/2209040042/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" deleted file mode 100644 index b6636c9254fd572b88e0b0fffde8339ae25a22e9..0000000000000000000000000000000000000000 --- "a/2209040042/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" +++ /dev/null @@ -1,42 +0,0 @@ -#include -let isValid = function(s) { - let stack = [], length = s.length; - if(length % 2) return false; - for(let item of s){ - switch(item){ - case "{": - case "[": - case "(": - stack.push(item); - break; - case "}": - if(stack.pop() !== "{") return false; - break; - case "]": - if(stack.pop() !== "[") return false; - break; - case ")": - if(stack.pop() !== "(") return false; - break; - } - } - return !stack.length; -}; - -// 解法二 -var isValid = function(s) { - s = s.split(''); - let sl = s.length; - if (sl % 2) return false; - let map = new Map([[')', '('], [']', '['], ['}', '{']]); - let stack = []; - for(let i of s){ - if (map.get(i)) { - if (stack[stack.length - 1] !== map.get(i)) return false; - else stack.pop(); - } else { - stack.push(i); - } - } - return !stack.length; -}; \ No newline at end of file diff --git "a/2209040042/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" "b/2209040042/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" deleted file mode 100644 index d68e5e31481338917165a33e47a42a2353eee513..0000000000000000000000000000000000000000 --- "a/2209040042/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" +++ /dev/null @@ -1,81 +0,0 @@ -typedef struct { - int* stk; - int stkSize; - int stkCapacity; -} Stack; - -Stack* stackCreate(int cpacity) { - Stack* ret = malloc(sizeof(Stack)); - ret->stk = malloc(sizeof(int) * cpacity); - ret->stkSize = 0; - ret->stkCapacity = cpacity; - return ret; -} - -void stackPush(Stack* obj, int x) { - obj->stk[obj->stkSize++] = x; -} - -void stackPop(Stack* obj) { - obj->stkSize--; -} - -int stackTop(Stack* obj) { - return obj->stk[obj->stkSize - 1]; -} - -bool stackEmpty(Stack* obj) { - return obj->stkSize == 0; -} - -void stackFree(Stack* obj) { - free(obj->stk); -} - -typedef struct { - Stack* inStack; - Stack* outStack; -} MyQueue; - -MyQueue* myQueueCreate() { - MyQueue* ret = malloc(sizeof(MyQueue)); - ret->inStack = stackCreate(100); - ret->outStack = stackCreate(100); - return ret; -} - -void in2out(MyQueue* obj) { - while (!stackEmpty(obj->inStack)) { - stackPush(obj->outStack, stackTop(obj->inStack)); - stackPop(obj->inStack); - } -} - -void myQueuePush(MyQueue* obj, int x) { - stackPush(obj->inStack, x); -} - -int myQueuePop(MyQueue* obj) { - if (stackEmpty(obj->outStack)) { - in2out(obj); - } - int x = stackTop(obj->outStack); - stackPop(obj->outStack); - return x; -} - -int myQueuePeek(MyQueue* obj) { - if (stackEmpty(obj->outStack)) { - in2out(obj); - } - return stackTop(obj->outStack); -} - -bool myQueueEmpty(MyQueue* obj) { - return stackEmpty(obj->inStack) && stackEmpty(obj->outStack); -} - -void myQueueFree(MyQueue* obj) { - stackFree(obj->inStack); - stackFree(obj->outStack); -} \ No newline at end of file diff --git "a/2209040042/chapter3/\347\224\250\346\240\210\350\247\243\345\206\263n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" "b/2209040042/chapter3/\347\224\250\346\240\210\350\247\243\345\206\263n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" deleted file mode 100644 index f4f9a9a07a1c68eaebaafc9da7ca3a1638306603..0000000000000000000000000000000000000000 --- "a/2209040042/chapter3/\347\224\250\346\240\210\350\247\243\345\206\263n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include -#include -#define MaxSize 100 -typedef struct -{ - int data[MaxSize]; - int top; -} StType; -int count=0; -int place(StType st,int i,int j) -{ - int a=0; - int k=1; - if (i==1) - { - a=1; - return a; - } - while (k<=i-1) - { - if ((st.data[k]==j)||(fabs(j-st.data[k])==fabs(i-k))) - { - a=0; - return a; - } - else - k++; - } - a=1; - return a; -} -void queen(int n) -{ - int i,j,k; - int find=0; - StType st; - st.top=0; - st.top++; - st.data[st.top]=1; - while (st.top>0) - { - i=st.top; - if (st.top==n) - { - printf("第%d个解:",++count); - for (k=1; k<=st.top; k++) - printf("(%d,%d) ",k,st.data[k]); - printf("\n"); - } - find=0; - for (j=1; j<=n; j++) - if (place(st,i+1,j)) - { - st.top++; - st.data[st.top]=j; - find=1; - break; - } - if (find==0) - { - while (st.top>0) - { - if (st.data[st.top]==n) - st.top--; - for (j=st.data[st.top]+1; j<=n; j++) - if (place(st,st.top,j)) - { - st.data[st.top]=j; - break; - } - if (j>n) - st.top--; - else - break; - } - } - } -} - -int main() -{ - int n; - printf(" 皇后问题(n<20) n="); - scanf("%d",&n); - printf(" %d皇后问题求解如下:\n",n); - queen(n); - printf("\n"); - return 0; -} \ No newline at end of file diff --git "a/2209040042/chapter3/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" "b/2209040042/chapter3/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" deleted file mode 100644 index 185d3d5ce15bf16bac231e7906c9a3d7499e94fe..0000000000000000000000000000000000000000 --- "a/2209040042/chapter3/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" +++ /dev/null @@ -1,99 +0,0 @@ - -#define LEN 20 -typedef struct queue { - int *data; - int head; - int rear; - int size; -} Queue; - -typedef struct { - Queue *queue1, *queue2; -} MyStack; - -Queue *initQueue(int k) { - Queue *obj = (Queue *)malloc(sizeof(Queue)); - obj->data = (int *)malloc(k * sizeof(int)); - obj->head = -1; - obj->rear = -1; - obj->size = k; - return obj; -} - -void enQueue(Queue *obj, int e) { - if (obj->head == -1) { - obj->head = 0; - } - obj->rear = (obj->rear + 1) % obj->size; - obj->data[obj->rear] = e; -} - -int deQueue(Queue *obj) { - int a = obj->data[obj->head]; - if (obj->head == obj->rear) { - obj->rear = -1; - obj->head = -1; - return a; - } - obj->head = (obj->head + 1) % obj->size; - return a; -} - -int isEmpty(Queue *obj) { - return obj->head == -1; -} - -MyStack *myStackCreate() { - MyStack *obj = (MyStack *)malloc(sizeof(MyStack)); - obj->queue1 = initQueue(LEN); - obj->queue2 = initQueue(LEN); - return obj; -} - -void myStackPush(MyStack *obj, int x) { - if (isEmpty(obj->queue1)) { - enQueue(obj->queue2, x); - } else { - enQueue(obj->queue1, x); - } -} - -int myStackPop(MyStack *obj) { - if (isEmpty(obj->queue1)) { - while (obj->queue2->head != obj->queue2->rear) { - enQueue(obj->queue1, deQueue(obj->queue2)); - } - return deQueue(obj->queue2); - } - while (obj->queue1->head != obj->queue1->rear) { - enQueue(obj->queue2, deQueue(obj->queue1)); - } - return deQueue(obj->queue1); -} - -int myStackTop(MyStack *obj) { - if (isEmpty(obj->queue1)) { - return obj->queue2->data[obj->queue2->rear]; - } - return obj->queue1->data[obj->queue1->rear]; -} - -bool myStackEmpty(MyStack *obj) { - if (obj->queue1->head == -1 && obj->queue2->head == -1) { - return true; - } - return false; -} - -void myStackFree(MyStack *obj) { - free(obj->queue1->data); - obj->queue1->data = NULL; - free(obj->queue1); - obj->queue1 = NULL; - free(obj->queue2->data); - obj->queue2->data = NULL; - free(obj->queue2); - obj->queue2 = NULL; - free(obj); - obj = NULL; -} \ No newline at end of file diff --git "a/2209040042/chapter3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" "b/2209040042/chapter3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" deleted file mode 100644 index f32d0f7f736bb9d6ad939603d840f0b9e4427e50..0000000000000000000000000000000000000000 --- "a/2209040042/chapter3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" +++ /dev/null @@ -1,64 +0,0 @@ -#include -int myCompare(char *p) -{ - if(strlen(p)>1) - { - return -1; - } - int res=-1; - for(int i=0;i<4;i++) - { - if(*p==set[i]) - { - res=i; - return res; - } - } - return res; -} - -int result(int x1,int x2,char k) -{ - if(k=='+') - { - return x2+x1; - } - else if(k=='-') - { - return x2-x1; - } - else if(k=='*') - { - return x2*x1; - } - else if(k=='/'&&x1!=0) - { - - return x2/x1; - } - else return -1; -} -int evalRPN(char ** tokens, int tokensSize){ - int* stack=malloc(sizeof(int)*tokensSize); - int top=0; - for(int i=0;i -#include -using namespace std; -void LPS(const string& pattern, int* lps) -{ - int n = pattern.length(); - int len = 0; - lps[0] = 0; - int i = 1; - while (i < n) { - if (pattern[i] == pattern[len]) - { - len++; - lps[i] = len; - i++; - } - else - { - if (len != 0) - { - len = lps[len - 1]; - } - else - { - lps[i] = 0; - i++; - } - } - } -} - -int KMP(const string& text, const string& pattern) -{ - int n = text.length(); - int m = pattern.length(); - int lps[m] = {0}; - LPS(pattern, lps); - int count = 0; - int i = 0, j = 0; - while (i < n) - { - if (text[i] == pattern[j]) - { - i++; - j++; - if (j == m) { - count++; - j = lps[j - 1]; - } - } - else - { - if (j > 0) - { - j = lps[j - 1]; - } - else - { - i++; - } - } - } - return count; -} - -int main() -{ - string text, pattern; - cin >> text >> pattern; - int result = KMP(text, pattern); - cout << result << endl; - return 0; -} \ No newline at end of file diff --git "a/2209040042/chapter4/\345\256\236\347\216\260\351\241\272\345\272\217\344\270\262\347\232\204\345\220\204\347\247\215\346\250\241\345\274\217\345\214\271\351\205\215\347\256\227\346\263\225.cpp" "b/2209040042/chapter4/\345\256\236\347\216\260\351\241\272\345\272\217\344\270\262\347\232\204\345\220\204\347\247\215\346\250\241\345\274\217\345\214\271\351\205\215\347\256\227\346\263\225.cpp" deleted file mode 100644 index 1edc914b509aa834b62bc1e56af59b120e8f47e5..0000000000000000000000000000000000000000 --- "a/2209040042/chapter4/\345\256\236\347\216\260\351\241\272\345\272\217\344\270\262\347\232\204\345\220\204\347\247\215\346\250\241\345\274\217\345\214\271\351\205\215\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,179 +0,0 @@ -#include -#define MAX_SIZE 100 -typedef struct -{ - char data[MAX_SIZE]; // 串中字符 - int length; // 串长 -}SqString; // 声明顺序串类型 -static void str_assign(SqString &s, char cstr[]) -{ - int i; - - for(i = 0; cstr[i] != '\0'; i++) - s.data[i] = cstr[i]; - s.length = i; -} -static void destroy_str(SqString &s) -{ - -} -static void disp_str(SqString s) -{ - int i; - - if(s.length > 0) - { - for(i = 0; i < s.length; i++) - printf("%c", s.data[i]); - printf("\n"); - } -} -static int Brute_Force(SqString s, SqString t) -{ - int i = 0, j = 0; - - while(i = t.length) - return (i - t.length); // 返回匹配的第一个字符的下标 - else - return -1; // 模式匹配不成功 -} -static void get_next(SqString t, int next[]) -{ - int j = 0, k = -1; - - next[0] = -1; - while(j < t.length - 1) - { - if(k == -1 || t.data[j] == t.data[k]) // k为-1或比较的字符相等时 - { - j++; - k++; - next[j] = k; - } - else - k = next[k]; - } -} -static void get_nextval(SqString t, int nextval[]) -{ - int j = 0, k = -1; - - nextval[0] = -1; - while(j < t.length) - { - if((k == -1) || (t.data[j] == t.data[k])) - { - j++; - k++; - if(t.data[j] != t.data[k]) - nextval[j] = k; - else - nextval[j] = nextval[k]; - } - else - k = nextval[k]; - } -} -static int KMP_index(SqString s, SqString t) -{ - int i = 0, j = 0, next[MAX_SIZE]; - - get_next(t, next); - while(j < s.length && j < t.length) - { - if(j == -1 || s.data[i] == t.data[j]) - { - // i,j各增1 - i++; - j++; - } - else - j = next[j]; // i不变,j后退 - } - if(j >= t.length) - return (i - t.length); // 返回匹配模式串中的首字符下标 - else - return -1; // 返回不匹配标志 -} - -static int KMP_index1(SqString s, SqString t) -{ - int i = 0, j = 0, nextval[MAX_SIZE]; - - get_nextval(t, nextval); - while(i < s.length && j < t.length) - { - if(j == -1 || s.data[i] == t.data[j]) - { - // i,j各增1 - i++; - j++; - } - else - j = nextval[j]; - } - - if(j >= t.length) - return (i - t.length); // 返回匹配模式串中的首字符下标 - else - return -1; // 返回不匹配标志 -} - -int main(int argc, char *argv[]) -{ - int j; - int next[MAX_SIZE]; - int nextval[MAX_SIZE]; - SqString s, t; - - str_assign(s, "abcabcdabcdeabcdefabcdefg"); - str_assign(t, "abcdeabcdefab"); - printf("串s:"); - disp_str(s); - printf("串t:"); - disp_str(t); - printf("简单匹配算法:\n"); - printf(" t在s中的位置=%d\n", Brute_Force(s, t)); - get_next(t, next); // 由模式串t求出next值 - get_nextval(t, nextval); // 由模式串t求出nextval的值 - printf(" j "); - for(j = 0; j < t.length; j++) - printf("%4d", j); - printf("\n"); - printf("t[j] "); - for(j = 0; j < t.length; j++) - printf("%4c", t.data[j]); - printf("\n"); - printf("next "); - for(j = 0; j < t.length; j++) - printf("%4d", next[j]); - printf("\n"); - - printf("nextval "); - for(j = 0; j < t.length; j++) - printf("%4d", nextval[j]); - printf("\n"); - - printf("KMP算法:\n"); - printf(" t在s中的位置=%d\n", KMP_index(s, t)); - printf("改进的KMP算法:\n"); - printf(" t在s中的位置=%d\n", KMP_index1(s, t)); - - destroy_str(s); - destroy_str(t); - return 0; -} \ No newline at end of file diff --git "a/2209040042/chapter4/\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" "b/2209040042/chapter4/\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" deleted file mode 100644 index 09b708d1c918c10f4c4b372f78ed75c77259f2b4..0000000000000000000000000000000000000000 --- "a/2209040042/chapter4/\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" +++ /dev/null @@ -1,30 +0,0 @@ -public class pro10_15_16 { - public static String getRepeatStr(String str){ - if(str==null || str.length()<1){ - return null; - } - int maxi=0,maxlen=0,len=0; - for(int i=1;imaxlen){ - maxlen=len; - maxi=j-len+1; - } - } - } - if(maxlen>0){ - System.out.println(maxlen); - return str.substring(maxi,maxlen); - } - return null; - } - public static void main(String[] args) { - String str="abcdabcdabcd"; - System.out.println(getRepeatStr(str)); - } -} \ No newline at end of file diff --git "a/2209040053/chap.2/exp2.2\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2209040053/chap.2/exp2.2\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index b0276b58a91f5a11af6d915e6f198ad2599eaf57..0000000000000000000000000000000000000000 --- "a/2209040053/chap.2/exp2.2\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,48 +0,0 @@ - -#include -#include -#include - -typedef char ElemType; - -int main() -{ - char a[] = {'x', 'b', 'c', 'd', 'e'}; - ListNode *L; - InitList(L); - CreateListR(L, x, sizeof(x) / sizeof(x[0])); - printf("单链表的初始元素:\n"); - OutList(L); - printf("链表的长度为:%d\n",ListLength(L)); - if (ListEmpty(L)) - printf("链表为空\n"); - else - printf("链表不为空\n"); - char elem; - int position=3; - if (GetElem(L, position, elem)) - printf("查找成功,链表第3个元素为:%c\n", elem); - else - printf("查找失败,链表中不存在第3个元素\n"); - - printf("元素'x'的位置为:%d\n", LocateElem(L, 'x')); - - if (ListInsert(L, 4, 'f')) - { - printf("插入成功,插入后的链表为:\n"); - OutList(L); - } - else - printf("插入失败,插入位置无效"); - char e; - if (DeleteList(L, 3, e)) - { - printf("成功删除,删除的元素为:%c\n", e); - printf("删除后的链表为:\n"); - OutList(L); - } - else - printf("删除失败,删除位置无效\n"); - DestoryList(L); - return 0; -} \ No newline at end of file diff --git "a/2209040053/chap.2/exp2.6\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" "b/2209040053/chap.2/exp2.6\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" deleted file mode 100644 index e50987fcce49af45a99924a6f8e30ecf50fa0637..0000000000000000000000000000000000000000 --- "a/2209040053/chap.2/exp2.6\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" +++ /dev/null @@ -1,128 +0,0 @@ -#include -#include -#include -typedef struct LinkNode -{ - int data; - struct LinkNode *next; -}LinkNode; -void InitList(LinkNode *&L) -{ - L=(LinkNode*)malloc(sizeof(LinkNode)); - L->next=NULL; -} -void CreatList(LinkNode* L,int a[],int n) -{ - LinkNode *s; - for(int i=0;idata=a[i]; - s->next=L->next; - L->next=s; - } -} -void DisList(LinkNode *L) -{ - LinkNode *p=L->next; - while(p!=NULL) - { - printf("%d ",p->data); - p=p->next; - } - printf("\n"); -} - -void sort(LinkNode* L,int num) -{ - LinkNode *p= L->next,*t = NULL, *h = NULL, *s = NULL; //以下修改 - L->next = NULL; - while (p) { - t = p; - p = p->next; - t->next = NULL; - if (!L->next) - L->next = t; - else if (t->data > num) - { - h = L; - while (h->next) h = h->next; - h->next = t; - } - else { - h = L->next; s = L; - while (h && h->data < num) s = h, h = h->next; - if (s == L) { - t->next = L->next; - L->next = t; - } - else { - t->next = s->next; - s->next = t; - } - } - } -} -int main() -{ - int n,e[100]; - int x; - LinkNode *L; - InitList(L); - - scanf("%d",&n); - for(int i=0;inext; - while(p!=NULL) - { - if(p->data==e) - { - break; - } - p=p->next; - } - if(p==NULL) - { - return false; - } - else - { - return true; - } -} -void fun(int a[],int n) -{ - int k=0,i=1; - int t; - for(i=1;i<=n-1;i++) - { - for(k=0;k<=n-1-i;k++) - { - if(a[k]>a[k+1]) - { - t=a[k]; - a[k]=a[k+1]; - a[k+1]=t; - } - } - } - for(int h=0;hnext == NULL || left == NULL) - { - return head; - } - - struct ListNode *dummyNode = malloc(sizeof(struct ListNode)); - dummyNode->val = -1; - dummyNode->next = head; - - struct ListNode *pre = dummyNode; - for (int i = 1; i < left; i++) - { - pre = pre->next; - } //Pre节点指向left的前一个节点 - - struct ListNode *cur = pre->next; - struct ListNode *nextNode; - for (int j = left; j < right; j++) - { - nextNode = cur->next; - cur->next = nextNode->next; - nextNode->next = pre->next; - pre->next = nextNode; - } - return dummyNode->next; - -} \ No newline at end of file diff --git "a/2209040053/chap.2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" "b/2209040053/chap.2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" deleted file mode 100644 index 88650f5c1aeea7639755569dd442987b2b66fe38..0000000000000000000000000000000000000000 --- "a/2209040053/chap.2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" +++ /dev/null @@ -1,25 +0,0 @@ - -class Solution { -public: - ListNode* deleteDuplicates(ListNode* head) { - if(!head ||!head->next){ - return head; - } - ListNode* temp = new ListNode(0,head); - ListNode* p = temp; - while(p->next && p->next->next){ - if(p->next->val == p->next->next->val){ - ListNode* q = p->next->next; - while(q && p->next->val == q->val){ - q = q->next; - } - if(!q) p->next = NULL; - else p->next = q; - }else{ - p=p->next; - } - } - return temp->next; - } -}; - diff --git "a/2209040054/chapter8/\344\275\234\344\270\2322.cpp" "b/2209040054/chapter8/\344\275\234\344\270\2322.cpp" deleted file mode 100644 index 3d8edec497f74c7a57ccfa368dbb0ec9c57642d8..0000000000000000000000000000000000000000 --- "a/2209040054/chapter8/\344\275\234\344\270\2322.cpp" +++ /dev/null @@ -1,133 +0,0 @@ -#include -#include - -struct TreeNode { - int val; - struct TreeNode *left; - struct TreeNode *right; -}; - -struct QueueNode { - struct TreeNode* treeNode; - struct QueueNode* next; -}; - -struct Queue { - struct QueueNode* front; - struct QueueNode* rear; -}; - -struct Queue* createQueue() { - struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue)); - queue->front = NULL; - queue->rear = NULL; - return queue; -} - -void enqueue(struct Queue* queue, struct TreeNode* treeNode) { - struct QueueNode* newQueueNode = (struct QueueNode*)malloc(sizeof(struct QueueNode)); - newQueueNode->treeNode = treeNode; - newQueueNode->next = NULL; - if (queue->rear == NULL) { - queue->front = newQueueNode; - queue->rear = newQueueNode; - } else { - queue->rear->next = newQueueNode; - queue->rear = newQueueNode; - } -} - -struct TreeNode* dequeue(struct Queue* queue) { - if (queue->front == NULL) { - return NULL; - } - struct QueueNode* temp = queue->front; - struct TreeNode* treeNode = temp->treeNode; - queue->front = queue->front->next; - if (queue->front == NULL) { - queue->rear = NULL; - } - free(temp); - return treeNode; -} - -int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes) { - *returnSize = 0; - if (root == NULL) { - return NULL; - } - struct Queue* queue = createQueue(); - enqueue(queue, root); - int** result = (int**)malloc(2000 * sizeof(int*)); - *returnColumnSizes = (int*)malloc(2000 * sizeof(int)); - - while (queue->front != NULL) { - int levelSize = queue->rear - queue->front + 1; - result[*returnSize] = (int*)malloc(levelSize * sizeof(int)); - (*returnColumnSizes)[*returnSize] = levelSize; - for (int i = 0; i < levelSize; i++) { - struct TreeNode* node = dequeue(queue); - result[*returnSize][i] = node->val; - if (node->left != NULL) { - enqueue(queue, node->left); - } - if (node->right != NULL) { - enqueue(queue, node->right); - } - } - (*returnSize)++; - } - - return result; -} - -int main() { - struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->val = 3; - root->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->left->val = 9; - root->left->left = NULL; - root->left->right = NULL; - root->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->right->val = 20; - root->right->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->right->left->val = 15; - root->right->left->left = NULL; - root->right->left->right = NULL; - root->right->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->right->right->val = 7; - root->right->right->left = NULL; - root->right->right->right = NULL; - - int returnSize; - int* returnColumnSizes; - int** result = levelOrder(root, &returnSize, &returnColumnSizes); - - printf("["); - for (int i = 0; i < returnSize; i++) { - printf("["); - for (int j = 0; j < returnColumnSizes[i]; j++) { - printf("%d", result[i][j]); - if (j != returnColumnSizes[i] - 1) { - printf(","); - } - } - printf("]"); - if (i != returnSize - 1) { - printf(","); - } - } - printf("]\n"); - - free(root->right->right); - free(root->right->left); - free(root->right); - free(root->left); - free(root); - for (int i = 0; i < returnSize; i++) { - free(result[i]); - } - free(result); - free(returnColumnSizes); - return 0; -} diff --git "a/2209040054/chapter8/\344\275\234\344\270\2323.cpp" "b/2209040054/chapter8/\344\275\234\344\270\2323.cpp" deleted file mode 100644 index c622c850e28c5380d372797e3e1229686333924e..0000000000000000000000000000000000000000 --- "a/2209040054/chapter8/\344\275\234\344\270\2323.cpp" +++ /dev/null @@ -1,70 +0,0 @@ -#include -#include - -struct TreeNode { - int val; - struct TreeNode *left; - struct TreeNode *right; -}; - -struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) { - if (root == NULL || root == p || root == q) { - return root; - } - struct TreeNode* left = lowestCommonAncestor(root->left, p, q); - struct TreeNode* right = lowestCommonAncestor(root->right, p, q); - if (left != NULL && right != NULL) { - return root; - } else if (left != NULL) { - return left; - } else { - return right; - } -} - -int main() { - struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->val = 3; - root->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->left->val = 5; - root->left->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->left->left->val = 6; - root->left->left->left = NULL; - root->left->left->right = NULL; - root->left->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->left->right->val = 2; - root->left->right->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->left->right->left->val = 7; - root->left->right->left->left = NULL; - root->left->right->left->right = NULL; - root->left->right->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->left->right->right->val = 4; - root->left->right->right->left = NULL; - root->left->right->right->right = NULL; - root->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->right->val = 1; - root->right->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->right->left->val = 0; - root->right->left->left = NULL; - root->right->left->right = NULL; - root->right->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->right->right->val = 8; - root->right->right->left = NULL; - root->right->right->right = NULL; - - struct TreeNode* p = root->left; - struct TreeNode* q = root->right; - struct TreeNode* result = lowestCommonAncestor(root, p, q); - printf("%d\n", result->val); // 输出 3 - - free(root->right->right); - free(root->right->left); - free(root->right); - free(root->left->right->right); - free(root->left->right->left); - free(root->left->right); - free(root->left->left); - free(root->left); - free(root); - return 0; -} \ No newline at end of file diff --git "a/2209040054/chapter8/\347\254\254\345\205\253\347\253\240\344\275\234\344\270\232.cpp" "b/2209040054/chapter8/\347\254\254\345\205\253\347\253\240\344\275\234\344\270\232.cpp" deleted file mode 100644 index 8bd507f20975f03c8435ae0f50cbea67dd3fb4a0..0000000000000000000000000000000000000000 --- "a/2209040054/chapter8/\347\254\254\345\205\253\347\253\240\344\275\234\344\270\232.cpp" +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include - -struct TreeNode { - int val; - struct TreeNode *left; - struct TreeNode *right; -}; - -void postorderTraversalHelper(struct TreeNode* root, int* result, int* returnSize) { - if (root == NULL) { - return; - } - - postorderTraversalHelper(root->left, result, returnSize); - postorderTraversalHelper(root->right, result, returnSize); - result[(*returnSize)++] = root->val; -} - -int* postorderTraversal(struct TreeNode* root, int* returnSize) { - *returnSize = 0; - int* result = (int*)malloc(100 * sizeof(int)); - postorderTraversalHelper(root, result, returnSize); - return result; -} - -int main() { - struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->val = 1; - root->left = NULL; - root->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->right->val = 2; - root->right->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - root->right->left->val = 3; - root->right->left->left = NULL; - root->right->left->right = NULL; - root->right->right = NULL; - - int returnSize; - int* result = postorderTraversal(root, &returnSize); - - printf("["); - for (int i = 0; i < returnSize; i++) { - printf("%d", result[i]); - if (i != returnSize - 1) { - printf(","); - } - } - printf("]\n"); - - free(root->right->left); - free(root->right); - free(root); - free(result); - return 0; -} diff --git "a/2209040056/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2209040056/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index 4b1bd0442bd544ff4dbb7d2c99db8a855e83ae4f..0000000000000000000000000000000000000000 --- "a/2209040056/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include -#include -bool prime1(long n) -{ - long j; - for(j=2;jnext) { - if (c->val == c->next->val) - { - c->next = c->next->next; - } - else - { - c = c->next; - } - } - return tou; - } -}; - diff --git "a/2209040056/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\2502.cpp" "b/2209040056/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\2502.cpp" deleted file mode 100644 index 29f11c672662d72250acc201def0d224971a05f1..0000000000000000000000000000000000000000 --- "a/2209040056/chapter3/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\2502.cpp" +++ /dev/null @@ -1,33 +0,0 @@ -int calculate(char* s) { - int n = strlen(s); - int stk[n], top = 0; - char preSign = '+'; - int num = 0; - for (int k = 0; k < n; ++k) { - if (isdigit(s[k])) { - num = num * 10 + (int)(s[k] - '0'); - } - if (!isdigit(s[k]) && s[k] != ' ' || k == n - 1) { - switch (preSign) { - case '+': - stk[top++] = num; - break; - case '-': - stk[top++] = -num; - break; - case '*': - stk[top - 1] *= num; - break; - default: - stk[top - 1] /= num; - } - preSign = s[k]; - num = 0; - } - } - int r = 0; - for (int k = 0; k < top; k++) { - r += stk[k]; - } - return r; -} diff --git a/2209040057/chapter_2/ex2-10.cpp b/2209040057/chapter_2/ex2-10.cpp deleted file mode 100644 index 94a027ea05b90582724dd86e748027ac6c425a79..0000000000000000000000000000000000000000 --- a/2209040057/chapter_2/ex2-10.cpp +++ /dev/null @@ -1,127 +0,0 @@ -#include -#include -typedef struct PolyNode* Polynomial; - -struct PolyNode { - int coefficient; - int expon; - Polynomial next; -}; - -void Attach(int v, int e, Polynomial* p) -{ - Polynomial temp = (Polynomial)malloc(sizeof(struct PolyNode)); - temp->coefficient = v; - temp->expon = e; - temp->next = NULL; - (*p)->next = temp; - (*p) = temp; -} -Polynomial ReadPoly() -{ - Polynomial p, r, temp; - int s, e, N; - p = (Polynomial)malloc(sizeof(struct PolyNode)); - p->next = NULL; - r = p; - scanf("%d", &N); - while (N--) - { - scanf("%d %d", &s, &e); - Attach(s, e, &r); - } - temp = p; - p = p->next; - free(temp); - return p; -} -Polynomial initPoly(Polynomial p1, Polynomial p2) -{ - Polynomial g, t, r; - g = (Polynomial)malloc(sizeof(struct PolyNode)); - g->next = NULL; - t = (Polynomial)malloc(sizeof(struct PolyNode)); - t = g2; - r = g; - while (t) - { - Attach((p1->coefficient) * (t->coefficient), g1->expon + t->expon, &r); - t = t->next; - } - return g; -} -Polynomial Mult(Polynomial p1, Polynomial p2) -{ - Polynomial p3 = initPoly(p1, p2); - Polynomial t1, t2, r, temp; - int c, e; - t1 = p1->next; - while (t1) - { - r = p3; - t2 = p2; - while (t2) - { - c = (t1->coefficient) * (t2->coefficient); - e = (t1->expon) + (t2->expon); - while ((rear->next) && (rear->next->expon > e)) - { - r = r->next; - } - if ((r->next) && (r->next->expon == e)) - { - if (r->next->coefficient + c) - { - r->next->coefficient += c; - } - else - { - temp = r->next; - r->next = temp->next; - free(temp); - } - } - else - { - temp = (Polynomial)malloc(sizeof(struct PolyNode)); - temp->coefficient = c; - temp->expon = e; - temp->next = r->next; - r->next = temp; - } - t2 = t2->next; - } - t1 = t1->next; - } - temp = p3; - p3 = p3->next; - free(temp); - return p3; -} -void PrintPoly(Polynomial p) -{ - Polynomial L; - L = p; - if (!L) - printf("0\n"); - while (L->next) - { - printf("%dx^%d + ", L->coefficient, Lptr->expon); - L = L->next; - } - printf("%dx^%d\n", L->coefficient, L->expon); -} -int main() -{ - Polynomial p1, p2, result; - p1 = ReadPoly(); - p2 = ReadPoly(); - result = Mult(p1, p2); - printf("多项式1为:\n"); - PrintPoly(p1); - printf("多项式2为:\n"); - PrintPoly(p2); - printf("两式相乘结果为:\n"); - PrintPoly(result); - return 0; -} \ No newline at end of file diff --git "a/2209040057/chapter_2/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" "b/2209040057/chapter_2/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" deleted file mode 100644 index 795477539fb786bcbed0bfff5535d92a08161e71..0000000000000000000000000000000000000000 --- "a/2209040057/chapter_2/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" +++ /dev/null @@ -1,22 +0,0 @@ - -struct ListNode* swapPairs(struct ListNode* head){ - typedef struct ListNode ListNode; - ListNode * fake; - fake=(ListNode*)malloc(sizeof(ListNode)); - fake->next=head; - ListNode * cur=fake; - while (cur->next!=NULL&&cur->next->next!=NULL){ - ListNode * temp11; - ListNode * temp22; - temp11=cur->next; - temp22=cur->next->next->next; - cur->next=cur->next->next; - cur->next->next=temp11; - temp11->next=temp22; - - cur=cur->next->next; - } - head=fake->next; - free(fake); - return(head); -} \ No newline at end of file diff --git "a/2209040057/chapter_2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" "b/2209040057/chapter_2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" deleted file mode 100644 index 8eff407291b424fbcfd8a56b92454ff21ebde351..0000000000000000000000000000000000000000 --- "a/2209040057/chapter_2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" +++ /dev/null @@ -1,22 +0,0 @@ -void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n) { - for(f=0;fnext; - cur->next =u; - u = cur; - cur = next; - } -} -struct ListNode *reverseBetween(struct ListNode *head, int left, int right) { - struct ListNode *dummyNode = malloc(sizeof(struct ListNode)); - dummyNode->val = -1; - dummyNode->next = head; - - struct ListNode *u = dummyNode; - for (int i = 0; i < left - 1; i++) { - u = u->next; - } - struct ListNode *rightNode =u; - for (int i = 0; i < right - left + 1; i++) { - rightNode = rightNode->next; - } - struct ListNode *leftNode = u->next; - struct ListNode *curr = rightNode->next; - u->next = NULL; - rightNode->next = NULL; - reverseLinkedList(leftNode); - u->next = rightNode; - leftNode->next = curr; - return dummyNode->next; -} \ No newline at end of file diff --git "a/2209040058/chat 2/Ic_203\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" "b/2209040058/chat 2/Ic_203\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" deleted file mode 100644 index fe1c26a61e223e1b0087e9947c4b3908913578b1..0000000000000000000000000000000000000000 --- "a/2209040058/chat 2/Ic_203\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * struct ListNode *next; - * }; - */ - - -struct ListNode* removeElements(struct ListNode* head, int val){ - struct ListNode* node=head; - if(head==NULL) return head; - while(node->next!=NULL) - { - if(node->val==val) - { - head=node->next; - node=head; - } - else if(node->next->val==val) - { - node->next=node->next->next; - } - else node=node->next; - } - if(head->val==val) - { - head=NULL; - } - return head; -} diff --git "a/2209040058/chat 2/Ic_\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" "b/2209040058/chat 2/Ic_\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" deleted file mode 100644 index 6c23099c671e152f7d72b038bb834577b047f826..0000000000000000000000000000000000000000 --- "a/2209040058/chat 2/Ic_\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" +++ /dev/null @@ -1,15 +0,0 @@ - class Solution { -public: - ListNode* swapPairs(ListNode* head) { - - if(head==nullptr||head->next==nullptr) - { - return head; - } - - ListNode* second=head->next; - head->next=swapPairs(second->next); - second->next=head; - return second; - } -}; \ No newline at end of file diff --git "a/2209040059/chapter2/\344\270\244\344\270\244\350\277\236\347\273\255\344\272\244\346\215\242\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271.cpp" "b/2209040059/chapter2/\344\270\244\344\270\244\350\277\236\347\273\255\344\272\244\346\215\242\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271.cpp" deleted file mode 100644 index 4a8390fa1156d62c37a2c6b6807497da22732116..0000000000000000000000000000000000000000 --- "a/2209040059/chapter2/\344\270\244\344\270\244\350\277\236\347\273\255\344\272\244\346\215\242\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { - public ListNode swapPairs(ListNode head) { - ListNode dummyHead = new ListNode(0); - dummyHead.next = head; - ListNode temp = dummyHead; - while (temp.next != null && temp.next.next != null) { - ListNode node1 = temp.next; - ListNode node2 = temp.next.next; - temp.next = node2; - node1.next = node2.next; - node2.next = node1; - temp = node1; - } - return dummyHead.next; - } -} \ No newline at end of file diff --git "a/2209040062/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2209040062/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index 269a96b7d42109dddb60aaf521e4f6738f7361ad..0000000000000000000000000000000000000000 --- "a/2209040062/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,30 +0,0 @@ -#include "sqqueue. cpp" -int main() -{ ElemType e; -SqQueue *q; -printf("环形队列基本运算如下:\n"); -printf(" (1)初始化队列 q\n"); -InitQueue(q); -printf(" (2)依次进队列元素a,b,c\n"); -if(!enQueue(q, 'a')) printf("\t提示:队满,不能进队\n"); -if(!enQueue(q,'b')) printf("\t 提示:队满,不能进队\n"); -if(!enQueue(q,'c')) printf("\t 提示:队满,不能进队\n"); -printf("(3)队列为%s\n",(QueueEmpty(q)?"空":"非空")); -if (deQueue(q,e) == 0) - printf("队空,不能出队\n"); -else - printf(" (4)出队一个元素子c\n",e); -printf(" (5)依次进队列元素 d,e,f\n"); -if(!enQueue(q,'d')) printf("\t 提示:队满,不能进队\n"); -if(!enQueue(q,'e')) printf("\t提示:队满,不能进队\n"); -if(!enQueue(q,'f')) printf("\t提示:队满,不能进队\n"); -printf(" (6)出队列序列:"); -while(!QueueEmpty(q)) -{ deQueue(q,e); -printf("&c ",e); -} -printf("\n"); -printf(" (7)释放队列\n"); -DestroyQueue(q); -return 1; -} \ No newline at end of file diff --git "a/2209040062/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2209040062/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index c221dd9a5f80f2a681b048da57e1f9b74637b1f6..0000000000000000000000000000000000000000 --- "a/2209040062/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,23 +0,0 @@ -int main() -{ - SqStack *s; - InitStack(s); - if(StackEmpty(s))printf("空\n"); - else printf("非空\n"); - ElemType a,b,c,d,e; - cin>>a>>b>>c>>d>>e; - Push(s,a); - Push(s,b); - Push(s,c); - Push(s,d); - Push(s,e); - if(StackEmpty(s))printf("空\n"); - else printf("非空\n"); - printf("栈的长度为%d\n",Length(s)); - PrintStack(s); - Print(s); - if(StackEmpty(s))printf("空\n"); - else printf("非空\n"); - DestroyStack(s); - return 0; -} \ No newline at end of file diff --git "a/2209040062/chapter3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" "b/2209040062/chapter3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" deleted file mode 100644 index 4b6dcdc98f9946f2462fa8d9f4e631d226e842a2..0000000000000000000000000000000000000000 --- "a/2209040062/chapter3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,141 +0,0 @@ -#include -#include - -typedef struct Queen{ - int x; - int y; -}Queen; - -typedef struct ListNode{ - Queen q; - struct ListNode *Next; - struct ListNode *Last; -}ListNode; - -typedef struct List { - struct ListNode *header; - struct ListNode *trailer; - int _size; -}List; - -void CreateList(List *l); -void InsertList(List *l,Queen e); -Queen PopList(List *l); -void InsertBefore(int i,Queen e,List *l); - -void CreateList(List *l){ - l->_size=0; - l->header= (ListNode*)malloc(sizeof(ListNode)); - l->trailer=(ListNode*)malloc(sizeof(ListNode)); - l->header->Next=l->trailer; - l->header->Last=NULL; - l->trailer->Last=l->header; - l->trailer->Next=NULL; -} - -void InsertList(List *l,Queen e){ - ListNode *np=(ListNode*)malloc(sizeof(ListNode)); - np->q.x=e.x; - np->q.y=e.y; - np->Next=l->trailer; - np->Last=l->trailer->Last; - l->trailer->Last->Next=np; - l->trailer->Last=np; - l->_size++; -} - -void InsertBefore(int i,Queen e,List *l){ - int j; - ListNode *p=l->header->Next; - for(j=0;jNext; - } - ListNode *np=(ListNode*)malloc(sizeof(ListNode)); - np->q.x=e.x; - np->q.y=e.y; - np->Next=p; - np->Last=p->Last; - p->Last->Next=np; - p->Last=np; - l->_size++; -} - -void PushList(List *l,Queen e){ - ListNode *np=(ListNode*)malloc(sizeof(ListNode)); - np->q.x=e.x; - np->q.y=e.y; - np->Last=l->header; - np->Next=l->header->Next; - np->Next->Last=np; - l->header->Next=np; - l->_size++; -} - -Queen PopList(List *l){ - ListNode *now=l->header->Next; - Queen old=now->q; - now->Next->Last=now->Last; - l->header->Next=now->Next; - free(now); - l->_size--; - return old; -} - -int placeQ(int N){ - int solution_n = 0; - int i; - List solution; - CreateList(&solution); - Queen q; - q.x=0; - q.y=0; - int *xarray=(int*)calloc(N,sizeof(int)); - int *yarray=(int*)calloc(N,sizeof(int)); - int *sumarray=(int*)calloc(2*N,sizeof(int)); - int *diffarray=(int*)calloc(2*N,sizeof(int)); - for(i=0;i q.y) { - PushList(&solution, q); - xarray[q.x] = 1; - yarray[q.y] = 1; - sumarray[q.x + q.y] = 1; - diffarray[q.x - q.y + N] = 1; - if (N <= solution._size) { - solution_n++; - } - q.x++; - q.y = 0; - } - } - } - while((q.x>0)||(q.y -#include -#define MAXSIZE 1600 - -typedef struct { - int *data; // 头指针 - int top; // 栈顶元素 -} MinStack; - -/** initialize your data structure here. */ - -MinStack* minStackCreate() { - MinStack *obj=(MinStack *)malloc(sizeof(MinStack)); // 开辟头指针空间 - obj->data=(int *)malloc(MAXSIZE*sizeof(int)); // 开辟数组空间 - obj->top=-1; // 栈顶下标。-1位特殊值,表示在第一个 - return obj; // 返回该指针 -} - -void minStackPush(MinStack* obj, int x) { - if(obj->top==MAXSIZE-1){ - // 如果数组满了,就什么也不做 - }else if(obj->top==-1){ // 判断是否是第一次入栈 - obj->top++; // 让top+1,使其为0,数组第一个下标,可以与上一行合并为obj->data[++obj->top]=x; - obj->data[obj->top]=x; // 将元素存入数组 - obj->top++; // 再将top+1,可以与下一行合并为obj->data[++obj->top]=x; - obj->data[obj->top]=x; // 因为栈为空,所以最小值肯定是第一个元素 - }else{ - int ymin=obj->data[obj->top++]; // 若不是第一次存入,拿出当前栈的最小值 - obj->data[obj->top]=x; // 将元素入栈 - if(ymindata[++obj->top]=ymin; // 判断最小值 - else obj->data[++obj->top]=x; - } -} - -void minStackPop(MinStack* obj) { - if(obj->top==-1){ - }else{ - obj->top--; - obj->top--; // 将top下标-2 - } -} - -int minStackTop(MinStack* obj) { - if(obj->top==-1){ - return 0; - } - return obj->data[obj->top-1]; // 栈顶元素 - -} - -int minStackGetMin(MinStack* obj) { - return obj->data[obj->top]; // 获取最小值 -} - -void minStackFree(MinStack* obj) { - /* - * 清空栈 - */ - free(obj->data); - obj->data=NULL; - free(obj); - obj=NULL; -} - diff --git "a/2209040064/chapter1/\346\261\2021~N\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" "b/2209040064/chapter1/\346\261\2021~N\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" deleted file mode 100644 index b3b09eda97ed9f848991a3b7b9b5df2b0e90129c..0000000000000000000000000000000000000000 --- "a/2209040064/chapter1/\346\261\2021~N\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" +++ /dev/null @@ -1,44 +0,0 @@ -#include -#include -#include -long add1(long n) -{ - long i, sum = 0; - - for(i = 1; i <= n; i++) - sum += i; - - return sum; -} - -void add_time1(long n) -{ - long sum; - clock_t t; - - t = clock(); - sum = add1(n); - t = clock() - t; - - printf("\n"); - printf("结果:1~%d之和:%ld\n", n, sum); - printf("用时:%lf秒\n",((float)t)/CLOCKS_PER_SEC); -} -int main(void) -{ - int n; - - printf("n(大于1000000):"); - scanf("%d", &n); - if(n < 1000000) - return 0; - - add_time1(n); - add_time2(n); - - return 0; -} - - - - diff --git "a/2209040064/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2209040064/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index 101924332c958b1091618716b9df7001d7a6ca06..0000000000000000000000000000000000000000 --- "a/2209040064/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -#include -bool prime1(long n) -{ - long i; - for(i=2;i tokens; - int num = 0; - char sign = '+'; - - for (; index < s.size() + 1; ++index) { - char c = s[index]; - if (isdigit(c)) - num = num * 10 + (c - '0'); - if (c == '(') { - index++; - num = calc(s, index); - continue; - } - if (!isdigit(c) && c != ' ') { - int temp; - switch (sign) { - case '+': - tokens.push(num); - break; - case '-': - tokens.push(-num); - break; - case '*': - temp = tokens.top(); - tokens.pop(); - tokens.push(temp * num); - break; - default: - temp = tokens.top(); - tokens.pop(); - tokens.push(temp / num); - } - num = 0; - sign = c; - } - if (c == ')') - break; - } - int result = 0; - while (!tokens.empty()) { - result += tokens.top(); - tokens.pop(); - } - return result; - } -}; \ No newline at end of file diff --git "a/2209040066/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227\347\256\227\346\263\225.cpp" "b/2209040066/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227\347\256\227\346\263\225.cpp" deleted file mode 100644 index 0d2429b21c3db0dfd7850246d58234d415a1887a..0000000000000000000000000000000000000000 --- "a/2209040066/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include -#define MaxSise 100 -typedef char ElemType; -typedef struct -{ - ElemType data[MaxSise]; - int top; -}SqStack; -void InitStack(SqStack *&s) -{ - s=(SqStack *)malloc(sizeof(SqStack)); - s->top=-1; -} -void DestroyStack(SqStack *&s) -{ - free(s); -} -bool stackEmpty(SqStack *s) -{ - return(s->top==-1); - -} -bool push(SqStack *&s,ElemType e) -{ - if(s->top==MaxSise-1) - return false; - s->top++; - s->data[s->top]=e; - return true; -} -bool Pop(SqStack *&s,ElemType &e) -{ - if(s->top==-1) - return false; - e=s->data[s->top]; - s->top--; - return true; -} -bool GetTop(SqStack *s,ElemType &e) -{ - if(s->top==-1) - return false; - e=s->data[s->top]; - return true; -} \ No newline at end of file diff --git "a/2209040066/chapter3/\346\234\200\345\260\217\346\240\210.cpp" "b/2209040066/chapter3/\346\234\200\345\260\217\346\240\210.cpp" deleted file mode 100644 index d55e3b2b4aad9d6944bb6aa390b8a532043db774..0000000000000000000000000000000000000000 --- "a/2209040066/chapter3/\346\234\200\345\260\217\346\240\210.cpp" +++ /dev/null @@ -1,33 +0,0 @@ -class MinStack { - - public Stack aux; - public Stack main; - public MinStack() { - aux = new Stack<>(); - main = new Stack<>(); - aux.push(Integer.MAX_VALUE); - - } - - public void push(int val) { - main.push(val); - if (val < aux.peek()){ - aux.push(val); - }else{ - aux.push(aux.peek()); - } - } - - public void pop() { - main.pop(); - aux.pop(); - } - - public int top() { - return main.peek(); - } - - public int getMin() { - return aux.peek(); - } -} \ No newline at end of file diff --git "a/2209040066/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" "b/2209040066/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" deleted file mode 100644 index 95b3d002eebc44a3fb8167a7f41558b9c1e37ac7..0000000000000000000000000000000000000000 --- "a/2209040066/chapter3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include -using namespace std; - -bool isValid(string s) { - stack st; - unordered_map map = {{')', '('}, {']', '['}, {'}', '{'}}; - - for (char c : s) { - if (map.count(c)) { - if (st.empty() || st.top() != map[c]) { - return false; - } - st.pop(); - } else { - st.push(c); - } - } - - return st.empty(); -} - -int main() { - string s = "{()[()]}"; - - if (isValid(s)) { - cout << "Valid" << endl; - } else { - cout << "Invalid" << endl; - } - - return 0; -} \ No newline at end of file diff --git "a/2209040066/chapter3/\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2209040066/chapter3/\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" deleted file mode 100644 index bfad273f7c54b5ab5c0c45a54aaa04c0e230b0cd..0000000000000000000000000000000000000000 --- "a/2209040066/chapter3/\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,69 +0,0 @@ -#include -#include -#define MaxSise 100 -typedef char ElemType; -typedef struct -{ - ElemType data[MaxSise]; - int front,rear; -}SqQueue; -void InitQueue(SqQueue *&q) -{ - q=(SqQueue *)malloc(sizeof(SqQueue)); - q->front=q->rear=0; -} -void DestroyQueue(SqQueue *&q) -{ - free(q); -} -bool QueueEmpty(SqQueue *q) -{ - return(q->front==q->rear); -} -bool enQueue(SqQueue *&q,ElemType e) -{ - if((q->rear+1)%MaxSise==q->front) - return false; - q->rear=(q->rear+1)%MaxSise; - q->data[q->rear]=e; - return true; -} -bool deQueue(SqQueue *&q,ElemType &e) -{ - if(q->front==q->rear) - return false; - q->front=(q->front+1)%MaxSise; - e=q->data[q->front]; - return true; -} -int main() -{ - ElemType e; - SqQueue *q; - printf("环形队列基本运算如下:\n"); - printf("(1)初始化队列 q\n"); - InitQueue(q); - printf("(2)依次进队列元素 a,b,c\n"); - if(!enQueue(q,'a'))printf("\t提示:队满不能进队\n"); - if(!enQueue(q,'b'))printf("\t提示:队满不能进队\n"); - if(!enQueue(q,'c'))printf("\t提示:队满不能进队\n"); - printf("(3)队列为%s\n",(QueueEmpty(q)?"空":"非空")); - if(deQueue(q,e)==0) - printf("队空,不能出队\n"); - else - printf("(4)出队一个元素%c\n",e); - printf("(5)依次进队列元素 d,e,f\n"); - if(!enQueue(q,'d'))printf("\t提示:队满不能进队\n"); - if(!enQueue(q,'e'))printf("\t提示:队满不能进队\n"); - if(!enQueue(q,'f'))printf("\t提示:队满不能进队\n"); - printf("(6)出队列序列:"); - while(!QueueEmpty(q)) - { - deQueue(q,e); - printf("%c",e); - } - printf("\n"); - printf("(7)释放队列\n"); - DestroyQueue(q); - return 1; -} \ No newline at end of file diff --git "a/2209040066/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" "b/2209040066/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" deleted file mode 100644 index cf12777457deea2045911cfb918f182611d0091d..0000000000000000000000000000000000000000 --- "a/2209040066/chapter3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" +++ /dev/null @@ -1,127 +0,0 @@ -typedef int Datatype; -typedef struct stack { - Datatype* arr; - int top; - int capacity; -}stack; -//1.检测栈是否为空,如果为空返回非零结果,如果不为空返回0 -int StackEmpty(stack* ps) { - assert(ps); - return ps->top == 0; -} -//2.检测栈满扩容 -void Cheakstack(stack* ps) { - if (ps->capacity == ps->top) { - int newcapacity = (ps->capacity) * 2; - Datatype* newstack = (Datatype*)malloc(sizeof(stack) * newcapacity); - for (int i = 0; i < ps->top; i++) { - newstack[i] = ps->arr[i]; - } - free(ps->arr); - ps->arr = newstack; - ps->capacity = newcapacity; - } -} -// 3..初始化栈 -void StackInit(stack* ps) { - assert(ps); - int N = 5; - ps->arr = (Datatype*)malloc(sizeof(Datatype) * N); - ps->capacity = N; - ps->top = 0; -} -//4.入栈 -void StackPush(stack* ps, Datatype data) { - assert(ps); - Cheakstack(ps); - ps->arr[ps->top] = data; - ps->top++; -} -//5.出栈 -void StackPop(stack* ps) { - assert(ps); - if (StackEmpty(ps)) { - return; - } - ps->top--; -} -//6.获取栈顶元素 -Datatype* StackTop(stack* ps) { - assert(ps); - if (StackEmpty(ps)) { - return NULL; - } - return ps->arr[ps->top - 1]; -} -//7. 获取栈中有效元素个数 -int StackSize(stack* ps) { - assert(ps); - return ps->top; -} -// 8.销毁栈 -void StackDestroy(stack* ps) { - assert(ps); - if (ps->arr) { - free(ps->arr); - ps->capacity = 0; - ps->top =0; - } -} - -typedef struct { - //俩个栈 - stack s1; - stack s2; -} MyQueue; - - -MyQueue* myQueueCreate() { - MyQueue* mq=(MyQueue*)malloc(sizeof(MyQueue));//申请空间 - if(NULL==mq){//检测参数 - return NULL; - } - //初始化栈 - StackInit(&mq->s1); - StackInit(&mq->s2); - return mq; -} - -void myQueuePush(MyQueue* obj, int x) { - StackPush(&obj->s1,x); -} - -int myQueuePop(MyQueue* obj) { - //出队列:把非空 的栈里的元素搬移到空的 - if(StackEmpty(&obj->s2)){ - while(!StackEmpty(&obj->s1)){//所有元素搬移到s2 - StackPush(&obj->s2,StackTop(&obj->s1)); - StackPop(&obj->s1); - } - } - int ret=StackTop(&obj->s2);//保存出队元素 - StackPop(&obj->s2);//出队 - return ret; -} - -int myQueuePeek(MyQueue* obj) { - if(StackEmpty(&obj->s2)){ - while(!StackEmpty(&obj->s1)){//所有元素搬移到s2 - StackPush(&obj->s2,StackTop(&obj->s1)); - StackPop(&obj->s1); - } - } - return StackTop(&obj->s2); -} - -bool myQueueEmpty(MyQueue* obj) { - return StackEmpty(&obj->s1)&&StackEmpty(&obj->s2); - -} - -void myQueueFree(MyQueue* obj) { - if(obj!=NULL){//先销毁栈 - StackDestroy(&obj->s1); - StackDestroy(&obj->s2); - free(obj); - } -} \ No newline at end of file diff --git "a/2209040066/chapter3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" "b/2209040066/chapter3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" deleted file mode 100644 index 8d70fafad70b2650c367d74f1434f71de3664f77..0000000000000000000000000000000000000000 --- "a/2209040066/chapter3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,76 +0,0 @@ -#include -#include -#define MaxSise 100 -typedef struct -{ - int col[MaxSise]; - int top; -}StackType; -void dispasolution(StackType St) -{ - static int count=0; - printf("第%d个解:",++count); - for(int i=1;i20) - printf("n的值太大\n"); - else{ - printf("%d皇后问题求解如下:\n",n); - queue(n); - } - return 1; -} \ No newline at end of file diff --git "a/2209040066/chapter3/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" "b/2209040066/chapter3/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" deleted file mode 100644 index 67759e9680c57a31edfbd4f5de6e70fae74759be..0000000000000000000000000000000000000000 --- "a/2209040066/chapter3/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" +++ /dev/null @@ -1,178 +0,0 @@ -#include -#include -#include -#include -typedef int QDataType; -typedef struct QueueNode -{ - QDataType data; - struct QueueNode* next; -}QNode; -typedef struct Queue -{ - QNode* head; - QNode* tail; - int size; -}Queue; -void QueueInit(Queue* pq); -void QueueDestroy(Queue* pq); -void QueuePush(Queue* pq, QDataType x); -void QueuePop(Queue* pq); -QDataType QueueFront(Queue* pq); -QDataType QueueBack(Queue* pq); -bool QueueEmpty(Queue* pq); -int QueueSize(Queue* pq); -void QueueInit(Queue* pq) -{ - assert(pq); - pq->head = NULL; - pq->tail = NULL; - pq->size = 0; -} -void QueueDestroy(Queue* pq) -{ - assert(pq); - QNode* cur = pq->head; - while (cur) - { - QNode* del = cur; - cur = del->next; - free(del); - } - pq->head = NULL; - pq->tail = NULL; - pq->size = 0; -} -void QueuePush(Queue* pq, QDataType x) -{ - assert(pq); - QNode* newnode = (QNode*)malloc(sizeof(QNode)); - if (newnode == NULL) - { - perror("malloc fail"); - exit(-1); - } - newnode->data = x; - newnode->next = NULL; - if (pq->head== NULL) - { - pq->head =pq->tail= newnode; - } - else - { - pq->tail->next = newnode; - pq->tail = newnode; - } - pq->size++; -} -void QueuePop(Queue* pq) -{ - assert(pq); - assert(!QueueEmpty(pq)); - if (pq->head->next == NULL) - { - free(pq->head); - pq->head =pq->tail= NULL; - } - else - { - QNode* cur = pq->head->next; - free(pq->head); - pq->head = cur; - } - pq->size--; -} -QDataType QueueFront(Queue* pq) -{ - assert(pq); - assert(!QueueEmpty(pq)); - - return pq->head->data; -} - -QDataType QueueBack(Queue* pq) -{ - assert(pq); - assert(!QueueEmpty(pq)); - - return pq->tail->data; -} -bool QueueEmpty(Queue* pq) -{ - assert(pq); - return pq->head==NULL&& pq->tail==NULL; -} -int QueueSize(Queue* pq) -{ - assert(pq); - return pq->size; -} -typedef struct { - Queue q1; - Queue q2; - -} MyStack; - -//正文开始 -MyStack* myStackCreate() { - MyStack*obj=(MyStack*)malloc(sizeof(MyStack)); - QueueInit(&obj->q1); - QueueInit(&obj->q2); - return obj; - - -} - -void myStackPush(MyStack* obj, int x) -{ - if(!QueueEmpty(&obj->q1)) - { - QueuePush(&obj->q1,x); - } - else - QueuePush(&obj->q2,x); - -} - -int myStackPop(MyStack* obj) { - Queue*empty=&obj->q1; - Queue*noempty=&obj->q2; - if(!QueueEmpty(&obj->q1)) - { - empty=&obj->q2; - noempty=&obj->q1; - - } - - while(QueueSize(noempty)>1) - { - QueuePush(empty,QueueFront(noempty)); - QueuePop(noempty); - } - int top=QueueFront(noempty); - QueuePop(noempty); - return top; -} - -int myStackTop(MyStack* obj) { - if(!QueueEmpty(&obj->q1)) - { - return QueueBack(&obj->q1); - } - else - { - return QueueBack(&obj->q2); - } - -} - -bool myStackEmpty(MyStack* obj) { - - return QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2); -} - -void myStackFree(MyStack* obj) { - QueueDestroy(&obj->q1); - QueueDestroy(&obj->q2); - free(obj); -} \ No newline at end of file diff --git "a/2209040066/chapter3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" "b/2209040066/chapter3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" deleted file mode 100644 index 18dfc00236ba468e772aa3d73867cd2457884309..0000000000000000000000000000000000000000 --- "a/2209040066/chapter3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include -#include - -using namespace std; - -int evaluateReversePolishExpression(const string& expression) { - stack operands; - - for (const char& c : expression) { - if (isdigit(c)) { - operands.push(c - '0'); - } else if (c == '+' || c == '-' || c == '*' || c == '/') { - int operand2 = operands.top(); - operands.pop(); - int operand1 = operands.top(); - operands.pop(); - - int result; - switch (c) { - case '+': - result = operand1 + operand2; - break; - case '-': - result = operand1 - operand2; - break; - case '*': - result = operand1 * operand2; - break; - case '/': - result = operand1 / operand2; - break; - } - - operands.push(result); - } - } - - return operands.top(); -} - -int main() { - string expression; - cout << "Enter a reverse Polish expression: "; - getline(cin, expression); - - int result = evaluateReversePolishExpression(expression); - cout << "The result is: " << result << endl; - - return 0; -} \ No newline at end of file diff --git "a/2209040066/chapter4/\344\270\200\344\270\252 \344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" "b/2209040066/chapter4/\344\270\200\344\270\252 \344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" deleted file mode 100644 index d8b609009541200c64fe30ea54cbf5833c90714b..0000000000000000000000000000000000000000 --- "a/2209040066/chapter4/\344\270\200\344\270\252 \344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" +++ /dev/null @@ -1,162 +0,0 @@ -#include -#include -#define MaxSise 100 -typedef struct -{ - char data[MaxSise]; - int length; -}SqString; -void StrAssign(SqString &s,char cstr[]) -{ - for(int i=0;cstr[i]!='\0';i++) - s.data[i]=cstr[i]; - s.length=i; -} -void DestroyStr(SqString &s) -{ } -void StrCopy(SqString &s,SqString t) -{ - for(int i=0;is.length||j<0||i+j-1>s.length) - return str; - for(k=i-1;ks1.length+1) - return str; - for(j=0;js.length||i+j>s.length+1) - return str; - for(k=0;ks.length||i+j-1>s.length) - return str; - for(k=0;k0) - { - for(int i=0;ilength) - { - index=i; - length=length1; - } - j+=length1; - } - else j++; - } - i++; - } - subs=(SqString *)malloc(sizeof(SqString)); - sub->length=length; - for(i=0;idata[i]=s.data[index+i]; - return subs; -} -int main() -{ - char str[MaxSise]; - SqString s,*subs; - printf("输入串:"); - gets(str); - StrAssign(s,str); - subs=Maxsubstr(s); - printf("求最长重复子串:"); - printf("原串:"); - DispStr(s); - printf("最长重复子串:"); - DispStr(*str); - DestroyStr(s);free(subs); - return 1; -} \ No newline at end of file diff --git "a/2209040066/chapter4/\345\210\251\347\224\250KMP\347\256\227\346\263\225\346\261\202\345\255\220\344\270\262\345\234\250\344\270\273\344\270\262\344\270\255\344\270\215\351\207\215\345\217\240\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" "b/2209040066/chapter4/\345\210\251\347\224\250KMP\347\256\227\346\263\225\346\261\202\345\255\220\344\270\262\345\234\250\344\270\273\344\270\262\344\270\255\344\270\215\351\207\215\345\217\240\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" deleted file mode 100644 index 67d83c47ce546fc39648f0acc1fe15ffb672eeaa..0000000000000000000000000000000000000000 --- "a/2209040066/chapter4/\345\210\251\347\224\250KMP\347\256\227\346\263\225\346\261\202\345\255\220\344\270\262\345\234\250\344\270\273\344\270\262\344\270\255\344\270\215\351\207\215\345\217\240\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" +++ /dev/null @@ -1,188 +0,0 @@ -#include -#define MaxSise 100 -typedef struct -{ - char data[MaxSise]; - int length; -}SqString; -void StrAssign(SqString &s,char cstr[]) -{ - for(int i=0;cstr[i]!='\0';i++) - s.data[i]=cstr[i]; - s.length=i; -} -void DestroyStr(SqString &s) -{ } -void StrCopy(SqString &s,SqString t) -{ - for(int i=0;is.length||j<0||i+j-1>s.length) - return str; - for(k=i-1;ks1.length+1) - return str; - for(j=0;js.length||i+j>s.length+1) - return str; - for(k=0;ks.length||i+j-1>s.length) - return str; - for(k=0;k0) - { - for(int i=0;i -#define MaxSise 100 -typedef struct -{ - char data[MaxSise]; - int length; -}SqString; -void StrAssign(SqString &s,char cstr[]) -{ - for(int i=0;cstr[i]!='\0';i++) - s.data[i]=cstr[i]; - s.length=i; -} -void DestroyStr(SqString &s) -{ } -void StrCopy(SqString &s,SqString t) -{ - for(int i=0;is.length||j<0||i+j-1>s.length) - return str; - for(k=i-1;ks1.length+1) - return str; - for(j=0;js.length||i+j>s.length+1) - return str; - for(k=0;ks.length||i+j-1>s.length) - return str; - for(k=0;k0) - { - for(int i=0;i=t.length) - return(i-t.length); - else - return(-1); -} -void GetNext(SqString t,int next[]) -{ - int j,k; - j=0;k=-1;next[0]=-1; - while(j=t.length) - return(i-t.length); - else - return(-1); -} -void GetNextval(SqString t,int nextval[]) -{ - int j=0,k=-1; - nextval[0]=-1; - while(j=t.length) - return(i-t.length); - else - return(-1); -} -int main() -{ - int j; - int next[MaxSise],nextval[MaxSise]; - SqString s,t; - StrAssign(s,"abcabcdabcdeabcdefabcdefg"); - StrAssign(t,"abcdeabcdefab"), - printf("串s:");DispStr(s); - printf("串t:");DispStr(t); - printf("简单匹配算法:\n"); - printf("t在s中的位置=%d\n",Index(s,t)); - GetNext(t,next); - GetNextval(t,nextval); - printf("j"); - for(j=0;j -#include -#include -#include -#include -using namespace std; -string longestCommonPrefix(vector& strs) { - if (strs.empty()) return ""; - string res = strs[0];//取向量容器中的第一个字符串 - for (const string& s : strs) - { - for (int i = 0; i < res.size(); i++) - { - if (s[i] != res[i]) - { - //substr用法: s.substr(i,j)表示从下标为i的位置开始截取j位。 - res = res.substr(0, i); - break; - } - } - } - printf("公共前缀为:%s\n", res.c_str()); - return res; -} -string longestCommonPrefix2(vector& strs) { - if (strs.size() == 0) - { - printf("不存在公共前缀"); - return ""; - } - char c; - string res = strs[0]; - for (int i = 0; i < strs[0].size(); i++) { - c = strs[0][i]; - for (int j = 1; j < strs.size(); j++) { - if (i == strs[j].size() || c != strs[j][i]) - { - res = strs[0].substr(0, i); - printf("公共前缀为:%s\n", res.c_str()); - return strs[0].substr(0, i); - } - - } - } - return strs[0]; -} - - -int main() -{ - vector vec;//定义个一个字符串容器 - vec.push_back("abcdzhangsan");//把字符串"..."压进容器 - vec.push_back("abcdlisi"); - vec.push_back("abcde"); - printf("输入为:\n"); - for (int i = 0; i < vec.size(); i++) { - cout << vec[i] << endl;//打印容器的内容 - } - longestCommonPrefix(vec); - vector vec2; - vec2.push_back("flzhangsan"); - vec2.push_back("fllisi"); - vec2.push_back("flwangwu"); - printf("输入为:\n"); - for (int i = 0; i < vec2.size(); i++) { - cout << vec2[i] << endl; - } - longestCommonPrefix2(vec2); - return 0; -} \ No newline at end of file diff --git "a/2209040066/chapter4/\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.cpp" "b/2209040066/chapter4/\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.cpp" deleted file mode 100644 index 87089e0dd5516daeff69eba7e9de2cbbc7879af6..0000000000000000000000000000000000000000 --- "a/2209040066/chapter4/\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.cpp" +++ /dev/null @@ -1,21 +0,0 @@ -class Solution { -public: - bool repeatedSubstringPattern(string s) { - int n = s.size(); - for (int i = 1; i * 2 <= n; ++i) { - if (n % i == 0) { - bool match = true; - for (int j = i; j < n; ++j) { - if (s[j] != s[j - i]) { - match = false; - break; - } - } - if (match) { - return true; - } - } - } - return false; - } -}; \ No newline at end of file diff --git "a/2209040066/chapter4/\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" "b/2209040066/chapter4/\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" deleted file mode 100644 index eb2144470aec040c1d3bf1a9858ba9f6dc245e4c..0000000000000000000000000000000000000000 --- "a/2209040066/chapter4/\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -using namespace std; - -bool isPalindrome(string s) { - int left = 0; - int right = s.length() - 1; - - while (left < right) { - while (left < right && !isalnum(s[left])) { - left++; - } - while (left < right && !isalnum(s[right])) { - right--; - } - - if (tolower(s[left]) != tolower(s[right])) { - return false; - } - - left++; - right--; - } - - return true; -} - -int main() { - string s = "A man, a plan, a canal: Panama"; - bool result = isPalindrome(s); - if (result) { - cout << "The string is a palindrome." << endl; - } else { - cout << "The string is not a palindrome." << endl; - } - - return 0; -} \ No newline at end of file diff --git "a/2209040067/chap1\345\256\236\351\252\214\351\242\230/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2209040067/chap1\345\256\236\351\252\214\351\242\230/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index 60a057db7d42a9ce56c7617d65f08f622bd3cf79..0000000000000000000000000000000000000000 --- "a/2209040067/chap1\345\256\236\351\252\214\351\242\230/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,58 +0,0 @@ -#include -#include -#include - -bool prime1(long n) -{ - long i; - for(i=2;i -#include // clock_t, clock, CLOCKS_PER_SEC -#include - -//方法1:老实累加 -long add1(long n) -{ - long i, sum = 0; - for (i = 1; i <= n; i++) - sum += i; - return(sum); -} - - -void AddTime1(long n) /* 采用方法1的耗时统计 */ -{ - clock_t t = clock(); - long sum = add1(n); - t = clock() - t; - printf("方法1:\n"); - printf(" 结果:1~%d之和:%ld\n", n, sum); - printf(" 用时:%lf秒\n", ((float)t) / CLOCKS_PER_SEC); -} - - -//方法2:用公式 -long add2(long n) /* 方法2:求1+2+...+n */ -{ - return(n * (n + 1) / 2); -} - - -void AddTime2(long n) /* 采用方法2的耗时统计 */ -{ - clock_t t = clock(); - long sum = add2(n); - t = clock() - t; - printf("方法2:\n"); - printf(" 结果:1~%d之和:%ld\n", n, sum); - printf(" 用时:%lf秒\n", ((float)t) / CLOCKS_PER_SEC); -} - -int main() -{ - int n; - printf("n(大于1000000):"); - scanf("%d", &n); - if (n < 1000000) - return(0); - AddTime1(n); - AddTime2(n); - return(1); -} \ No newline at end of file diff --git "a/2209040067/chap2/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\273\223\347\202\271.cpp" "b/2209040067/chap2/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\273\223\347\202\271.cpp" deleted file mode 100644 index d9cf7735355fed0a9f1756fc2f5cb68a0baac543..0000000000000000000000000000000000000000 --- "a/2209040067/chap2/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\273\223\347\202\271.cpp" +++ /dev/null @@ -1,57 +0,0 @@ -#include - -using namespace std; - -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - -ListNode* swapPairs(ListNode* head) { - if (head == NULL || head->next == NULL) { - return head; - } - ListNode *dummy = new ListNode(0); - dummy->next = head; - ListNode *curr = dummy; - while (curr->next != NULL && curr->next->next != NULL) { - ListNode *first = curr->next; - ListNode *second = curr->next->next; - first->next = second->next; - second->next = first; - curr->next = second; - curr = curr->next->next; - } - ListNode *result = dummy->next; - delete dummy; - return result; -} - -void printList(ListNode* head) { - ListNode* curr = head; - while (curr != NULL) { - cout << curr->val << " "; - curr = curr->next; - } - cout << endl; -} - -int main() { - ListNode* head = new ListNode(1); - head->next = new ListNode(2); - head->next->next = new ListNode(3); - head->next->next->next = new ListNode(4); - head->next->next->next->next = new ListNode(5); - head->next->next->next->next->next = new ListNode(6); - - cout << "Original list: "; - printList(head); - - head = swapPairs(head); - - cout << "List after swapping pairs: "; - printList(head); - - return 0; -} \ No newline at end of file diff --git "a/2209040067/chap2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" "b/2209040067/chap2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" deleted file mode 100644 index 0046ad7f8f8412e2624f9c44253f368ac3021ce6..0000000000000000000000000000000000000000 --- "a/2209040067/chap2/\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" +++ /dev/null @@ -1,44 +0,0 @@ -#include - -using namespace std; - -struct TreeNode* -{ - int val; - TreeNode* next; - TreeNode*(int x): val(x), next(nullptr) - { - }; -}; -struct TreeNode* deleteDuplicates(struct TreeNode* head){ - - int val = 0; - struct TreeNode* q; - struct TreeNode* p; - - if(NULL == head) - { - return NULL; - } - - val = head->val; - p = head; - q = head->next; - while(NULL != q) - { - if(val == q->val) - { - p->next = q->next; - free(q); - q = p->next; - } - else - { - val = q->val; - q = q->next; - p = p->next; - } - } - - return head; -} \ No newline at end of file diff --git "a/2209040067/chap2/\345\220\210\345\271\266.cpp" "b/2209040067/chap2/\345\220\210\345\271\266.cpp" deleted file mode 100644 index fcd4c86b746acc950b039c15473803acd4bd4b5f..0000000000000000000000000000000000000000 --- "a/2209040067/chap2/\345\220\210\345\271\266.cpp" +++ /dev/null @@ -1,41 +0,0 @@ -#include -int main() -{ - int char a[],n,char b[],m; - for(n=0;n<10000;n++) - { - scanf("%d",a[n]); - - } - for(m=0;m<10000;m++) - { - scanf("%d",b[m]); - } - merge(a[n],b[m]); - return 0; - } - - void merge(int *nums1,int nums1Size,int m,int nums2,int nums2Size,int n) - { - int*tmp=(int*)malloc(sizeof(int)*(m+n)); - int i1=0,i2=0; - int i=0; - while(i1nums[i2]) - tmp[i++]=nums2[i2++]; - else - tmp[i++]=nums1[i1++]; - } - while(i2 -#include -#include - - - - -//定义每一个数据的节点 -typedef struct my_node{ - char data; - struct my_node *next; -}my_node,*p_node; - -//定义队列 -typedef struct{ - p_node front,rear; -}link_queue; - - -void init_queue(link_queue *p_queue) -{ - p_queue->front = p_queue->rear = (p_node)malloc(sizeof(my_node)); - if(!p_queue->front) - exit(0); - p_queue->front->next = NULL; -} - -void insert_queue(link_queue *p_queue,char data) -{ - p_node add_node; - add_node = (p_node)malloc(sizeof(my_node)); - add_node->data = data; - add_node->next = NULL; - p_queue->rear->next = add_node; - p_queue->rear = add_node; -} - -void delete_queue(link_queue *p_queue,char *data) -{ - p_node delete_node ; - delete_node = p_queue->front->next; - *data = delete_node->data; - p_queue->front->next = delete_node->next; - if(p_queue->rear == delete_node) - { - p_queue->rear = delete_node->next; - } - free(delete_node); -} -void removeElement(*head *val){ - // 为空 - if (head == null) - return null; - // 当不为空 且链表值等于给定值时 - while (head != null && head.val == val) { - // 位移一位 - head = head.next; - } - - ListNode node = head; - - while (node.next != null) { - if (node.next.val == val) - node.next = node.next.next; - else - node = node.next; - } - return head; - } -} - -int main(){ - link_queue *p_queue; - p_queue = (link_queue *)malloc(sizeof(link_queue ); //如果编译问题 加 - init_queue(p_queue); - - for(int i=0;i<5;i++) - { - insert_queue(p_queue,('a'+i)); - removeElement(p_queue,('a'+i)); - } - - for(int i=0;i<5;i++) - { - char *my_data = (char*)malloc(sizeof(char)); - delete_queue(p_queue,my_data); - printf("%c\\n",*my_data); - - } - return 0; - -} \ No newline at end of file diff --git "a/2209040067/chap2/\347\277\273\350\275\254\351\223\276\350\241\250.cpp" "b/2209040067/chap2/\347\277\273\350\275\254\351\223\276\350\241\250.cpp" deleted file mode 100644 index 58cb76c57160729ab4394310b26735293d9c48ec..0000000000000000000000000000000000000000 --- "a/2209040067/chap2/\347\277\273\350\275\254\351\223\276\350\241\250.cpp" +++ /dev/null @@ -1,54 +0,0 @@ -#include - -using namespace std; - -struct TreeNode -{ - int val; - TreeNode* next; - TreeNode(int x): val(x), next(nullptr) - { - }; -}; - -TreeNode* reverseList(TreeNode* head) -{ - if(head == nullptr) - { - return nullptr; - } - if(head->next == nullptr) - { - return head; - } - - TreeNode* pFront = nullptr; - TreeNode* pCur = head; - TreeNode* pBack = head->next; - - while(pCur->next) - { - pCur->next = pFront; - pFront = pCur; - pCur = pBack; - pBack = pBack->next; - } - pCur->next = pFront; - return pCur; -} - -int main() -{ - TreeNode a = TreeNode(10); - TreeNode b = TreeNode(12); - TreeNode c = TreeNode(13); - - TreeNode* head = &a; - a.next = &b; - b.next = &c; - - TreeNode* ptr = reverseList(head); - cout << ptr->val << endl; - - return 0; -} \ No newline at end of file diff --git "a/2209040067/chap3/chap3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" "b/2209040067/chap3/chap3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" deleted file mode 100644 index cf12777457deea2045911cfb918f182611d0091d..0000000000000000000000000000000000000000 --- "a/2209040067/chap3/chap3/\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.cpp" +++ /dev/null @@ -1,127 +0,0 @@ -typedef int Datatype; -typedef struct stack { - Datatype* arr; - int top; - int capacity; -}stack; -//1.检测栈是否为空,如果为空返回非零结果,如果不为空返回0 -int StackEmpty(stack* ps) { - assert(ps); - return ps->top == 0; -} -//2.检测栈满扩容 -void Cheakstack(stack* ps) { - if (ps->capacity == ps->top) { - int newcapacity = (ps->capacity) * 2; - Datatype* newstack = (Datatype*)malloc(sizeof(stack) * newcapacity); - for (int i = 0; i < ps->top; i++) { - newstack[i] = ps->arr[i]; - } - free(ps->arr); - ps->arr = newstack; - ps->capacity = newcapacity; - } -} -// 3..初始化栈 -void StackInit(stack* ps) { - assert(ps); - int N = 5; - ps->arr = (Datatype*)malloc(sizeof(Datatype) * N); - ps->capacity = N; - ps->top = 0; -} -//4.入栈 -void StackPush(stack* ps, Datatype data) { - assert(ps); - Cheakstack(ps); - ps->arr[ps->top] = data; - ps->top++; -} -//5.出栈 -void StackPop(stack* ps) { - assert(ps); - if (StackEmpty(ps)) { - return; - } - ps->top--; -} -//6.获取栈顶元素 -Datatype* StackTop(stack* ps) { - assert(ps); - if (StackEmpty(ps)) { - return NULL; - } - return ps->arr[ps->top - 1]; -} -//7. 获取栈中有效元素个数 -int StackSize(stack* ps) { - assert(ps); - return ps->top; -} -// 8.销毁栈 -void StackDestroy(stack* ps) { - assert(ps); - if (ps->arr) { - free(ps->arr); - ps->capacity = 0; - ps->top =0; - } -} - -typedef struct { - //俩个栈 - stack s1; - stack s2; -} MyQueue; - - -MyQueue* myQueueCreate() { - MyQueue* mq=(MyQueue*)malloc(sizeof(MyQueue));//申请空间 - if(NULL==mq){//检测参数 - return NULL; - } - //初始化栈 - StackInit(&mq->s1); - StackInit(&mq->s2); - return mq; -} - -void myQueuePush(MyQueue* obj, int x) { - StackPush(&obj->s1,x); -} - -int myQueuePop(MyQueue* obj) { - //出队列:把非空 的栈里的元素搬移到空的 - if(StackEmpty(&obj->s2)){ - while(!StackEmpty(&obj->s1)){//所有元素搬移到s2 - StackPush(&obj->s2,StackTop(&obj->s1)); - StackPop(&obj->s1); - } - } - int ret=StackTop(&obj->s2);//保存出队元素 - StackPop(&obj->s2);//出队 - return ret; -} - -int myQueuePeek(MyQueue* obj) { - if(StackEmpty(&obj->s2)){ - while(!StackEmpty(&obj->s1)){//所有元素搬移到s2 - StackPush(&obj->s2,StackTop(&obj->s1)); - StackPop(&obj->s1); - } - } - return StackTop(&obj->s2); -} - -bool myQueueEmpty(MyQueue* obj) { - return StackEmpty(&obj->s1)&&StackEmpty(&obj->s2); - -} - -void myQueueFree(MyQueue* obj) { - if(obj!=NULL){//先销毁栈 - StackDestroy(&obj->s1); - StackDestroy(&obj->s2); - free(obj); - } -} \ No newline at end of file diff --git "a/2209040067/chap3/chap3\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227\347\256\227\346\263\225.cpp" "b/2209040067/chap3/chap3\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227\347\256\227\346\263\225.cpp" deleted file mode 100644 index 0d2429b21c3db0dfd7850246d58234d415a1887a..0000000000000000000000000000000000000000 --- "a/2209040067/chap3/chap3\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include -#define MaxSise 100 -typedef char ElemType; -typedef struct -{ - ElemType data[MaxSise]; - int top; -}SqStack; -void InitStack(SqStack *&s) -{ - s=(SqStack *)malloc(sizeof(SqStack)); - s->top=-1; -} -void DestroyStack(SqStack *&s) -{ - free(s); -} -bool stackEmpty(SqStack *s) -{ - return(s->top==-1); - -} -bool push(SqStack *&s,ElemType e) -{ - if(s->top==MaxSise-1) - return false; - s->top++; - s->data[s->top]=e; - return true; -} -bool Pop(SqStack *&s,ElemType &e) -{ - if(s->top==-1) - return false; - e=s->data[s->top]; - s->top--; - return true; -} -bool GetTop(SqStack *s,ElemType &e) -{ - if(s->top==-1) - return false; - e=s->data[s->top]; - return true; -} \ No newline at end of file diff --git "a/2209040067/chap3/chap3\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2209040067/chap3/chap3\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" deleted file mode 100644 index bfad273f7c54b5ab5c0c45a54aaa04c0e230b0cd..0000000000000000000000000000000000000000 --- "a/2209040067/chap3/chap3\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,69 +0,0 @@ -#include -#include -#define MaxSise 100 -typedef char ElemType; -typedef struct -{ - ElemType data[MaxSise]; - int front,rear; -}SqQueue; -void InitQueue(SqQueue *&q) -{ - q=(SqQueue *)malloc(sizeof(SqQueue)); - q->front=q->rear=0; -} -void DestroyQueue(SqQueue *&q) -{ - free(q); -} -bool QueueEmpty(SqQueue *q) -{ - return(q->front==q->rear); -} -bool enQueue(SqQueue *&q,ElemType e) -{ - if((q->rear+1)%MaxSise==q->front) - return false; - q->rear=(q->rear+1)%MaxSise; - q->data[q->rear]=e; - return true; -} -bool deQueue(SqQueue *&q,ElemType &e) -{ - if(q->front==q->rear) - return false; - q->front=(q->front+1)%MaxSise; - e=q->data[q->front]; - return true; -} -int main() -{ - ElemType e; - SqQueue *q; - printf("环形队列基本运算如下:\n"); - printf("(1)初始化队列 q\n"); - InitQueue(q); - printf("(2)依次进队列元素 a,b,c\n"); - if(!enQueue(q,'a'))printf("\t提示:队满不能进队\n"); - if(!enQueue(q,'b'))printf("\t提示:队满不能进队\n"); - if(!enQueue(q,'c'))printf("\t提示:队满不能进队\n"); - printf("(3)队列为%s\n",(QueueEmpty(q)?"空":"非空")); - if(deQueue(q,e)==0) - printf("队空,不能出队\n"); - else - printf("(4)出队一个元素%c\n",e); - printf("(5)依次进队列元素 d,e,f\n"); - if(!enQueue(q,'d'))printf("\t提示:队满不能进队\n"); - if(!enQueue(q,'e'))printf("\t提示:队满不能进队\n"); - if(!enQueue(q,'f'))printf("\t提示:队满不能进队\n"); - printf("(6)出队列序列:"); - while(!QueueEmpty(q)) - { - deQueue(q,e); - printf("%c",e); - } - printf("\n"); - printf("(7)释放队列\n"); - DestroyQueue(q); - return 1; -} \ No newline at end of file diff --git "a/2209040067/chap3/chap3\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" "b/2209040067/chap3/chap3\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" deleted file mode 100644 index 8d70fafad70b2650c367d74f1434f71de3664f77..0000000000000000000000000000000000000000 --- "a/2209040067/chap3/chap3\344\270\212\346\234\272\345\256\236\351\252\214\351\242\230/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,76 +0,0 @@ -#include -#include -#define MaxSise 100 -typedef struct -{ - int col[MaxSise]; - int top; -}StackType; -void dispasolution(StackType St) -{ - static int count=0; - printf("第%d个解:",++count); - for(int i=1;i20) - printf("n的值太大\n"); - else{ - printf("%d皇后问题求解如下:\n",n); - queue(n); - } - return 1; -} \ No newline at end of file diff --git "a/2209040067/chap3/\347\254\254\344\270\211\347\253\240/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250.cpp" "b/2209040067/chap3/\347\254\254\344\270\211\347\253\240/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250.cpp" deleted file mode 100644 index be6a02e82ec35f96aab0835830ae6a76acfc3ae5..0000000000000000000000000000000000000000 --- "a/2209040067/chap3/\347\254\254\344\270\211\347\253\240/\345\237\272\346\234\254\350\256\241\347\256\227\345\231\250.cpp" +++ /dev/null @@ -1,54 +0,0 @@ -class Solution { -public: - int calculate(string str) { - int index = 0; - return calc(str, index); - } - - int calc(string &s, int &index) { - stack tokens; - int num = 0; - char sign = '+'; - - for (; index < s.size() + 1; ++index) { - char c = s[index]; - if (isdigit(c)) - num = num * 10 + (c - '0'); - if (c == '(') { - index++; - num = calc(s, index); - continue; - } - if (!isdigit(c) && c != ' ') { - int temp; - switch (sign) { - case '+': - tokens.push(num); - break; - case '-': - tokens.push(-num); - break; - case '*': - temp = tokens.top(); - tokens.pop(); - tokens.push(temp * num); - break; - default: - temp = tokens.top(); - tokens.pop(); - tokens.push(temp / num); - } - num = 0; - sign = c; - } - if (c == ')') - break; - } - int result = 0; - while (!tokens.empty()) { - result += tokens.top(); - tokens.pop(); - } - return result; - } -}; \ No newline at end of file diff --git "a/2209040067/chap3/\347\254\254\344\270\211\347\253\240/\346\234\200\345\260\217\346\240\210.cpp" "b/2209040067/chap3/\347\254\254\344\270\211\347\253\240/\346\234\200\345\260\217\346\240\210.cpp" deleted file mode 100644 index d55e3b2b4aad9d6944bb6aa390b8a532043db774..0000000000000000000000000000000000000000 --- "a/2209040067/chap3/\347\254\254\344\270\211\347\253\240/\346\234\200\345\260\217\346\240\210.cpp" +++ /dev/null @@ -1,33 +0,0 @@ -class MinStack { - - public Stack aux; - public Stack main; - public MinStack() { - aux = new Stack<>(); - main = new Stack<>(); - aux.push(Integer.MAX_VALUE); - - } - - public void push(int val) { - main.push(val); - if (val < aux.peek()){ - aux.push(val); - }else{ - aux.push(aux.peek()); - } - } - - public void pop() { - main.pop(); - aux.pop(); - } - - public int top() { - return main.peek(); - } - - public int getMin() { - return aux.peek(); - } -} \ No newline at end of file diff --git "a/2209040067/chap3/\347\254\254\344\270\211\347\253\240/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" "b/2209040067/chap3/\347\254\254\344\270\211\347\253\240/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" deleted file mode 100644 index 95b3d002eebc44a3fb8167a7f41558b9c1e37ac7..0000000000000000000000000000000000000000 --- "a/2209040067/chap3/\347\254\254\344\270\211\347\253\240/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include -using namespace std; - -bool isValid(string s) { - stack st; - unordered_map map = {{')', '('}, {']', '['}, {'}', '{'}}; - - for (char c : s) { - if (map.count(c)) { - if (st.empty() || st.top() != map[c]) { - return false; - } - st.pop(); - } else { - st.push(c); - } - } - - return st.empty(); -} - -int main() { - string s = "{()[()]}"; - - if (isValid(s)) { - cout << "Valid" << endl; - } else { - cout << "Invalid" << endl; - } - - return 0; -} \ No newline at end of file diff --git "a/2209040067/chap3/\347\254\254\344\270\211\347\253\240/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" "b/2209040067/chap3/\347\254\254\344\270\211\347\253\240/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" deleted file mode 100644 index 67759e9680c57a31edfbd4f5de6e70fae74759be..0000000000000000000000000000000000000000 --- "a/2209040067/chap3/\347\254\254\344\270\211\347\253\240/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" +++ /dev/null @@ -1,178 +0,0 @@ -#include -#include -#include -#include -typedef int QDataType; -typedef struct QueueNode -{ - QDataType data; - struct QueueNode* next; -}QNode; -typedef struct Queue -{ - QNode* head; - QNode* tail; - int size; -}Queue; -void QueueInit(Queue* pq); -void QueueDestroy(Queue* pq); -void QueuePush(Queue* pq, QDataType x); -void QueuePop(Queue* pq); -QDataType QueueFront(Queue* pq); -QDataType QueueBack(Queue* pq); -bool QueueEmpty(Queue* pq); -int QueueSize(Queue* pq); -void QueueInit(Queue* pq) -{ - assert(pq); - pq->head = NULL; - pq->tail = NULL; - pq->size = 0; -} -void QueueDestroy(Queue* pq) -{ - assert(pq); - QNode* cur = pq->head; - while (cur) - { - QNode* del = cur; - cur = del->next; - free(del); - } - pq->head = NULL; - pq->tail = NULL; - pq->size = 0; -} -void QueuePush(Queue* pq, QDataType x) -{ - assert(pq); - QNode* newnode = (QNode*)malloc(sizeof(QNode)); - if (newnode == NULL) - { - perror("malloc fail"); - exit(-1); - } - newnode->data = x; - newnode->next = NULL; - if (pq->head== NULL) - { - pq->head =pq->tail= newnode; - } - else - { - pq->tail->next = newnode; - pq->tail = newnode; - } - pq->size++; -} -void QueuePop(Queue* pq) -{ - assert(pq); - assert(!QueueEmpty(pq)); - if (pq->head->next == NULL) - { - free(pq->head); - pq->head =pq->tail= NULL; - } - else - { - QNode* cur = pq->head->next; - free(pq->head); - pq->head = cur; - } - pq->size--; -} -QDataType QueueFront(Queue* pq) -{ - assert(pq); - assert(!QueueEmpty(pq)); - - return pq->head->data; -} - -QDataType QueueBack(Queue* pq) -{ - assert(pq); - assert(!QueueEmpty(pq)); - - return pq->tail->data; -} -bool QueueEmpty(Queue* pq) -{ - assert(pq); - return pq->head==NULL&& pq->tail==NULL; -} -int QueueSize(Queue* pq) -{ - assert(pq); - return pq->size; -} -typedef struct { - Queue q1; - Queue q2; - -} MyStack; - -//正文开始 -MyStack* myStackCreate() { - MyStack*obj=(MyStack*)malloc(sizeof(MyStack)); - QueueInit(&obj->q1); - QueueInit(&obj->q2); - return obj; - - -} - -void myStackPush(MyStack* obj, int x) -{ - if(!QueueEmpty(&obj->q1)) - { - QueuePush(&obj->q1,x); - } - else - QueuePush(&obj->q2,x); - -} - -int myStackPop(MyStack* obj) { - Queue*empty=&obj->q1; - Queue*noempty=&obj->q2; - if(!QueueEmpty(&obj->q1)) - { - empty=&obj->q2; - noempty=&obj->q1; - - } - - while(QueueSize(noempty)>1) - { - QueuePush(empty,QueueFront(noempty)); - QueuePop(noempty); - } - int top=QueueFront(noempty); - QueuePop(noempty); - return top; -} - -int myStackTop(MyStack* obj) { - if(!QueueEmpty(&obj->q1)) - { - return QueueBack(&obj->q1); - } - else - { - return QueueBack(&obj->q2); - } - -} - -bool myStackEmpty(MyStack* obj) { - - return QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2); -} - -void myStackFree(MyStack* obj) { - QueueDestroy(&obj->q1); - QueueDestroy(&obj->q2); - free(obj); -} \ No newline at end of file diff --git "a/2209040067/chap3/\347\254\254\344\270\211\347\253\240/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" "b/2209040067/chap3/\347\254\254\344\270\211\347\253\240/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" deleted file mode 100644 index 18dfc00236ba468e772aa3d73867cd2457884309..0000000000000000000000000000000000000000 --- "a/2209040067/chap3/\347\254\254\344\270\211\347\253\240/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include -#include - -using namespace std; - -int evaluateReversePolishExpression(const string& expression) { - stack operands; - - for (const char& c : expression) { - if (isdigit(c)) { - operands.push(c - '0'); - } else if (c == '+' || c == '-' || c == '*' || c == '/') { - int operand2 = operands.top(); - operands.pop(); - int operand1 = operands.top(); - operands.pop(); - - int result; - switch (c) { - case '+': - result = operand1 + operand2; - break; - case '-': - result = operand1 - operand2; - break; - case '*': - result = operand1 * operand2; - break; - case '/': - result = operand1 / operand2; - break; - } - - operands.push(result); - } - } - - return operands.top(); -} - -int main() { - string expression; - cout << "Enter a reverse Polish expression: "; - getline(cin, expression); - - int result = evaluateReversePolishExpression(expression); - cout << "The result is: " << result << endl; - - return 0; -} \ No newline at end of file diff --git "a/2209040067/chap4/chap4\345\237\272\347\241\200\351\242\230/\345\256\236\347\216\260strStr( ).cpp" "b/2209040067/chap4/chap4\345\237\272\347\241\200\351\242\230/\345\256\236\347\216\260strStr( ).cpp" deleted file mode 100644 index 0bc0fcd0965641b7b41f10536dceb9573147a24e..0000000000000000000000000000000000000000 --- "a/2209040067/chap4/chap4\345\237\272\347\241\200\351\242\230/\345\256\236\347\216\260strStr( ).cpp" +++ /dev/null @@ -1,20 +0,0 @@ -class Solution { - int ne[50000 + 10]; -public: - int strStr(string haystack, string needle) { - if(needle.length() == 0) return 0; - ne[0] = -1; - for(int i = 1,j = -1;i < needle.length();i++){ - while(j != -1 && needle[i] != needle[j + 1]) j = ne[j]; - if(needle[i] == needle[j + 1]) j ++; - ne[i] = j; - } - - for(int i = 0,j = -1;i < haystack.length();i++){ - while(j != -1 && haystack[i] != needle[j + 1]) j = ne[j]; - if(haystack[i] == needle[j + 1]) j++; - if(j == needle.length() - 1) return i - j; - } - return -1; - } -}; \ No newline at end of file diff --git "a/2209040067/chap4/chap4\345\237\272\347\241\200\351\242\230/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.cpp" "b/2209040067/chap4/chap4\345\237\272\347\241\200\351\242\230/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.cpp" deleted file mode 100644 index 39e889a2912fee00be71fe387f580a1db76beb17..0000000000000000000000000000000000000000 --- "a/2209040067/chap4/chap4\345\237\272\347\241\200\351\242\230/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.cpp" +++ /dev/null @@ -1,70 +0,0 @@ -#include -#include -#include -#include -#include -using namespace std; -string longestCommonPrefix(vector& strs) { - if (strs.empty()) return ""; - string res = strs[0];//取向量容器中的第一个字符串 - for (const string& s : strs) - { - for (int i = 0; i < res.size(); i++) - { - if (s[i] != res[i]) - { - //substr用法: s.substr(i,j)表示从下标为i的位置开始截取j位。 - res = res.substr(0, i); - break; - } - } - } - printf("公共前缀为:%s\n", res.c_str()); - return res; -} -string longestCommonPrefix2(vector& strs) { - if (strs.size() == 0) - { - printf("不存在公共前缀"); - return ""; - } - char c; - string res = strs[0]; - for (int i = 0; i < strs[0].size(); i++) { - c = strs[0][i]; - for (int j = 1; j < strs.size(); j++) { - if (i == strs[j].size() || c != strs[j][i]) - { - res = strs[0].substr(0, i); - printf("公共前缀为:%s\n", res.c_str()); - return strs[0].substr(0, i); - } - - } - } - return strs[0]; -} - - -int main() -{ - vector vec;//定义个一个字符串容器 - vec.push_back("abcdzhangsan");//把字符串"..."压进容器 - vec.push_back("abcdlisi"); - vec.push_back("abcde"); - printf("输入为:\n"); - for (int i = 0; i < vec.size(); i++) { - cout << vec[i] << endl;//打印容器的内容 - } - longestCommonPrefix(vec); - vector vec2; - vec2.push_back("flzhangsan"); - vec2.push_back("fllisi"); - vec2.push_back("flwangwu"); - printf("输入为:\n"); - for (int i = 0; i < vec2.size(); i++) { - cout << vec2[i] << endl; - } - longestCommonPrefix2(vec2); - return 0; -} \ No newline at end of file diff --git "a/2209040067/chap4/chap4\345\237\272\347\241\200\351\242\230/\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.cpp" "b/2209040067/chap4/chap4\345\237\272\347\241\200\351\242\230/\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.cpp" deleted file mode 100644 index 87089e0dd5516daeff69eba7e9de2cbbc7879af6..0000000000000000000000000000000000000000 --- "a/2209040067/chap4/chap4\345\237\272\347\241\200\351\242\230/\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.cpp" +++ /dev/null @@ -1,21 +0,0 @@ -class Solution { -public: - bool repeatedSubstringPattern(string s) { - int n = s.size(); - for (int i = 1; i * 2 <= n; ++i) { - if (n % i == 0) { - bool match = true; - for (int j = i; j < n; ++j) { - if (s[j] != s[j - i]) { - match = false; - break; - } - } - if (match) { - return true; - } - } - } - return false; - } -}; \ No newline at end of file diff --git "a/2209040067/chap4/chap4\345\237\272\347\241\200\351\242\230/\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" "b/2209040067/chap4/chap4\345\237\272\347\241\200\351\242\230/\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" deleted file mode 100644 index eb2144470aec040c1d3bf1a9858ba9f6dc245e4c..0000000000000000000000000000000000000000 --- "a/2209040067/chap4/chap4\345\237\272\347\241\200\351\242\230/\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.cpp" +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -using namespace std; - -bool isPalindrome(string s) { - int left = 0; - int right = s.length() - 1; - - while (left < right) { - while (left < right && !isalnum(s[left])) { - left++; - } - while (left < right && !isalnum(s[right])) { - right--; - } - - if (tolower(s[left]) != tolower(s[right])) { - return false; - } - - left++; - right--; - } - - return true; -} - -int main() { - string s = "A man, a plan, a canal: Panama"; - bool result = isPalindrome(s); - if (result) { - cout << "The string is a palindrome." << endl; - } else { - cout << "The string is not a palindrome." << endl; - } - - return 0; -} \ No newline at end of file diff --git "a/2209040067/chapter2\344\270\212\346\234\272\345\256\236\351\252\214/\345\256\236\351\252\2143.cpp" "b/2209040067/chapter2\344\270\212\346\234\272\345\256\236\351\252\214/\345\256\236\351\252\2143.cpp" deleted file mode 100644 index 0fd997638bee7b3544c4d93eb2649d9982c93392..0000000000000000000000000000000000000000 --- "a/2209040067/chapter2\344\270\212\346\234\272\345\256\236\351\252\214/\345\256\236\351\252\2143.cpp" +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include -bool prime1(long n) -{ - long i; - for (i=2;i -#include -#include -long add1(long n) -{ - long i,sum=0; - for(i=1;i -int Sumarr(int* arr, int len) -{ - int res = 0; - for (int i = 0; i < len; i++) - { - for (int j = 1; j <=len- i ; j += 2) - { - for (int m = i; m < i + j; m++) - { - res = res + arr[m]; - } - } - } - return res; -} - -int main() -{ - int arr[]={10,11,12}; - printf("%d\n",Sumarr(arr,sizeof(arr)/sizeof(arr[0]))); - return 0; -} diff --git "a/2209040068/chapter7\357\274\210\347\254\254\344\270\203\347\253\240\344\275\234\344\270\232\357\274\211/\346\225\231\346\235\220\347\254\25411\351\242\230\357\274\232\346\261\202\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.cpp" "b/2209040068/chapter7\357\274\210\347\254\254\344\270\203\347\253\240\344\275\234\344\270\232\357\274\211/\346\225\231\346\235\220\347\254\25411\351\242\230\357\274\232\346\261\202\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.cpp" deleted file mode 100644 index d6827a5817ed6e85d1ddb940909b0cd9e33d1c66..0000000000000000000000000000000000000000 --- "a/2209040068/chapter7\357\274\210\347\254\254\344\270\203\347\253\240\344\275\234\344\270\232\357\274\211/\346\225\231\346\235\220\347\254\25411\351\242\230\357\274\232\346\261\202\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.cpp" +++ /dev/null @@ -1,4 +0,0 @@ -int maxDepth(struct TreeNode* root) { - if (root == NULL) return 0; - return fmax(maxDepth(root->left), maxDepth(root->right)) + 1; -} \ No newline at end of file diff --git "a/2209040069/chap1/lc1588_\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\227\346\225\260\347\273\204\345\222\214.cpp" "b/2209040069/chap1/lc1588_\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\227\346\225\260\347\273\204\345\222\214.cpp" deleted file mode 100644 index 15c0981135046f8583ac7c48c6fc09e4203856ca..0000000000000000000000000000000000000000 --- "a/2209040069/chap1/lc1588_\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\227\346\225\260\347\273\204\345\222\214.cpp" +++ /dev/null @@ -1,12 +0,0 @@ -int sob(int* arr, int arrSize) { - int sum = 0; - for (int start = 0; start < arrSize; start++) { - for (int length = 1; start + length <= arrSize; length += 2) { - int end = start + length - 1; - for (int i = start; i <= end; i++) { - sum += arr[i]; - } - } - } - return sum; -} \ No newline at end of file diff --git "a/2209040070/chapter_3/lc-232\347\224\250\346\240\210\345\256\236\347\216\260\345\210\227\351\230\237.cpp" "b/2209040070/chapter_3/lc-232\347\224\250\346\240\210\345\256\236\347\216\260\345\210\227\351\230\237.cpp" deleted file mode 100644 index d68e5e31481338917165a33e47a42a2353eee513..0000000000000000000000000000000000000000 --- "a/2209040070/chapter_3/lc-232\347\224\250\346\240\210\345\256\236\347\216\260\345\210\227\351\230\237.cpp" +++ /dev/null @@ -1,81 +0,0 @@ -typedef struct { - int* stk; - int stkSize; - int stkCapacity; -} Stack; - -Stack* stackCreate(int cpacity) { - Stack* ret = malloc(sizeof(Stack)); - ret->stk = malloc(sizeof(int) * cpacity); - ret->stkSize = 0; - ret->stkCapacity = cpacity; - return ret; -} - -void stackPush(Stack* obj, int x) { - obj->stk[obj->stkSize++] = x; -} - -void stackPop(Stack* obj) { - obj->stkSize--; -} - -int stackTop(Stack* obj) { - return obj->stk[obj->stkSize - 1]; -} - -bool stackEmpty(Stack* obj) { - return obj->stkSize == 0; -} - -void stackFree(Stack* obj) { - free(obj->stk); -} - -typedef struct { - Stack* inStack; - Stack* outStack; -} MyQueue; - -MyQueue* myQueueCreate() { - MyQueue* ret = malloc(sizeof(MyQueue)); - ret->inStack = stackCreate(100); - ret->outStack = stackCreate(100); - return ret; -} - -void in2out(MyQueue* obj) { - while (!stackEmpty(obj->inStack)) { - stackPush(obj->outStack, stackTop(obj->inStack)); - stackPop(obj->inStack); - } -} - -void myQueuePush(MyQueue* obj, int x) { - stackPush(obj->inStack, x); -} - -int myQueuePop(MyQueue* obj) { - if (stackEmpty(obj->outStack)) { - in2out(obj); - } - int x = stackTop(obj->outStack); - stackPop(obj->outStack); - return x; -} - -int myQueuePeek(MyQueue* obj) { - if (stackEmpty(obj->outStack)) { - in2out(obj); - } - return stackTop(obj->outStack); -} - -bool myQueueEmpty(MyQueue* obj) { - return stackEmpty(obj->inStack) && stackEmpty(obj->outStack); -} - -void myQueueFree(MyQueue* obj) { - stackFree(obj->inStack); - stackFree(obj->outStack); -} \ No newline at end of file diff --git "a/2209040070/chapter_3/lc_150\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" "b/2209040070/chapter_3/lc_150\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" deleted file mode 100644 index e10b488a9d06cc13cc887e526a0ed728c1d6d104..0000000000000000000000000000000000000000 --- "a/2209040070/chapter_3/lc_150\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" +++ /dev/null @@ -1,32 +0,0 @@ -bool isNumber(char* token) { - return strlen(token) > 1 || ('0' <= token[0] && token[0] <= '9'); -} - -int evalRPN(char** tokens, int tokensSize) { - int n = tokensSize; - int stk[n], top = 0; - for (int n = 0; n < n; n++) { - char* token = tokens[n]; - if (isNumber(token)) { - stk[top++] = atoi(token); - } else { - int num2 = stk[--top]; - int num1 = stk[--top]; - switch (token[0]) { - case '+': - stk[top++] = num1 + num2; - break; - case '-': - stk[top++] = num1 - num2; - break; - case '*': - stk[top++] = num1 * num2; - break; - case '/': - stk[top++] = num1 / num2; - break; - } - } - } - return stk[top - 1]; -} \ No newline at end of file diff --git "a/2209040070/chapter_3/lc_155\346\234\200\345\260\217\346\240\210.cpp" "b/2209040070/chapter_3/lc_155\346\234\200\345\260\217\346\240\210.cpp" deleted file mode 100644 index 5eb2c150093c2027103624a4800c4f9f77ea0cbc..0000000000000000000000000000000000000000 --- "a/2209040070/chapter_3/lc_155\346\234\200\345\260\217\346\240\210.cpp" +++ /dev/null @@ -1,57 +0,0 @@ -#define MAX_SIZE 10001 - -typedef struct { - int* data; - int top; - int min; -} MinStack; - -MinStack* minStackCreate() { - MinStack* obj = (MinStack*)malloc(sizeof(MinStack)); - obj->data = (int*)malloc(sizeof(int) * MAX_SIZE); - obj->top = -1; - obj->min = INT_MAX; - return obj; -} - -void minStackPush(MinStack* obj, int val) { - if (obj->top >= MAX_SIZE) { - return; - } - obj->data[++(obj->top)] = val; - if (val < obj->min) { - obj->min = val; - } -} - -void minStackPop(MinStack* obj) { - if (obj->top < 0) { - return; - } - obj->top--; - if (obj->top >= 0) { - obj->min = obj->data[obj->top]; - } else { - obj->min = INT_MAX; - } - for (int j = obj->top; j >= 0; j--) { - if (obj->data[j] < obj->min) { - obj->min = obj->data[j]; - } - } -} - -int minStackTop(MinStack* obj) { - return obj->data[obj->top]; -} - -int minStackGetMin(MinStack* obj) { - if (obj->top < 0) { - return -1; - } - return obj->min; -} - -void minStackFree(MinStack* obj) { - free(obj); -} diff --git "a/2209040070/chapter_3/lc_20\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" "b/2209040070/chapter_3/lc_20\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" deleted file mode 100644 index 5ff88534adf88c6d6b4a92b82040934a2e3fc6e6..0000000000000000000000000000000000000000 --- "a/2209040070/chapter_3/lc_20\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" +++ /dev/null @@ -1,26 +0,0 @@ -char pairs(char a) { - if (a == '}') return '{'; - if (a == ']') return '['; - if (a == ')') return '('; - return 0; -} - -bool isValid(char* s) { - int n = strlen(s); - if (n % 2 == 1) { - return false; - } - int stk[n + 1], top = 0; - for (int b = 0; b < n; b++) { - char ch = pairs(s[b]); - if (ch) { - if (top == 0 || stk[top - 1] != ch) { - return false; - } - top--; - } else { - stk[top++] = s[b]; - } - } - return top == 0; -} \ No newline at end of file diff --git "a/2209040070/chapter_3/lc_225\347\224\250\345\210\227\351\230\237\345\256\236\347\216\260\346\240\210.cpp" "b/2209040070/chapter_3/lc_225\347\224\250\345\210\227\351\230\237\345\256\236\347\216\260\346\240\210.cpp" deleted file mode 100644 index ff0b261a685d22ec37bcda54d26485c1e67ba181..0000000000000000000000000000000000000000 --- "a/2209040070/chapter_3/lc_225\347\224\250\345\210\227\351\230\237\345\256\236\347\216\260\346\240\210.cpp" +++ /dev/null @@ -1,98 +0,0 @@ -#define LEN 20 -typedef struct queue { - int *data; - int head; - int rear; - int size; -} Queue; - -typedef struct { - Queue *queue1, *queue2; -} MyStack; - -Queue *initQueue(int k) { - Queue *obj = (Queue *)malloc(sizeof(Queue)); - obj->data = (int *)malloc(k * sizeof(int)); - obj->head = -1; - obj->rear = -1; - obj->size = k; - return obj; -} - -void enQueue(Queue *obj, int e) { - if (obj->head == -1) { - obj->head = 0; - } - obj->rear = (obj->rear + 1) % obj->size; - obj->data[obj->rear] = e; -} - -int deQueue(Queue *obj) { - int a = obj->data[obj->head]; - if (obj->head == obj->rear) { - obj->rear = -1; - obj->head = -1; - return a; - } - obj->head = (obj->head + 1) % obj->size; - return a; -} - -int isEmpty(Queue *obj) { - return obj->head == -1; -} - -MyStack *myStackCreate() { - MyStack *obj = (MyStack *)malloc(sizeof(MyStack)); - obj->queue1 = initQueue(LEN); - obj->queue2 = initQueue(LEN); - return obj; -} - -void myStackPush(MyStack *obj, int x) { - if (isEmpty(obj->queue1)) { - enQueue(obj->queue2, x); - } else { - enQueue(obj->queue1, x); - } -} - -int myStackPop(MyStack *obj) { - if (isEmpty(obj->queue1)) { - while (obj->queue2->head != obj->queue2->rear) { - enQueue(obj->queue1, deQueue(obj->queue2)); - } - return deQueue(obj->queue2); - } - while (obj->queue1->head != obj->queue1->rear) { - enQueue(obj->queue2, deQueue(obj->queue1)); - } - return deQueue(obj->queue1); -} - -int myStackTop(MyStack *obj) { - if (isEmpty(obj->queue1)) { - return obj->queue2->data[obj->queue2->rear]; - } - return obj->queue1->data[obj->queue1->rear]; -} - -bool myStackEmpty(MyStack *obj) { - if (obj->queue1->head == -1 && obj->queue2->head == -1) { - return true; - } - return false; -} - -void myStackFree(MyStack *obj) { - free(obj->queue1->data); - obj->queue1->data = NULL; - free(obj->queue1); - obj->queue1 = NULL; - free(obj->queue2->data); - obj->queue2->data = NULL; - free(obj->queue2); - obj->queue2 = NULL; - free(obj); - obj = NULL; -} \ No newline at end of file diff --git "a/2209040070/chapter_3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" "b/2209040070/chapter_3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" deleted file mode 100644 index 9c19bb19ecee8dd85796f7031c519f337cc6ba2c..0000000000000000000000000000000000000000 --- "a/2209040070/chapter_3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include -#include -#define MaxSize 100 -typedef struct -{ - int data[MaxSize]; - int top; -} StType; - -int count=0; -int place(StType st,int i,int j) -{ - int a=0; - int k=1; - if (i==1) - { - a=1; - return a; - } - while (k<=i-1) - { - if ((st.data[k]==j)||(fabs(j-st.data[k])==fabs(i-k))) - { - a=0; - return a; - } - else - k++; - } - a=1; - return a; -} -void queen(int n) -{ - int i,j,k; - int find=0; - StType st; - st.top=0; - st.top++; - st.data[st.top]=1; - while (st.top>0) - { - i=st.top; - if (st.top==n) - { - printf("第%d个解:",++count); - for (k=1; k<=st.top; k++) - printf("(%d,%d) ",k,st.data[k]); - printf("\n"); - } - find=0; - for (j=1; j<=n; j++) - if (place(st,i+1,j)) - { - st.top++; - st.data[st.top]=j; - find=1; - break; - } - if (find==0) - { - while (st.top>0) - { - if (st.data[st.top]==n) - st.top--; - for (j=st.data[st.top]+1; j<=n; j++) - if (place(st,st.top,j)) - { - st.data[st.top]=j; - break; - } - if (j>n) - st.top--; - else - break; - } - } - } -} -int main() -{ - int n; - printf(" 皇后问题(n<20) n="); - scanf("%d",&n); - printf(" %d皇后问题求解如下:\n",n); - queen(n); - printf("\n"); - return 0; -} \ No newline at end of file diff --git "a/2209040071/chapter1,2/1~n\344\271\213\345\222\214.cpp" "b/2209040071/chapter1,2/1~n\344\271\213\345\222\214.cpp" deleted file mode 100644 index a028ca4aff69ce72a6270e04bc19a8d53908d3db..0000000000000000000000000000000000000000 --- "a/2209040071/chapter1,2/1~n\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,14 +0,0 @@ -int* twoSum(int* nums, int numsSize, int target, int* returnSize) { - for (int i = 0; i < numsSize; ++i) { - for (int j = i + 1; j < numsSize; ++j) { - if (nums[i] + nums[j] == target) { - int* ret = malloc(sizeof(int) * 2); - ret[0] = i, ret[1] = j; - *returnSize = 2; - return ret; - } - } - } - *returnSize = 0; - return NULL; -} \ No newline at end of file diff --git "a/2209040071/chapter1,2/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2209040071/chapter1,2/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index a028ca4aff69ce72a6270e04bc19a8d53908d3db..0000000000000000000000000000000000000000 --- "a/2209040071/chapter1,2/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,14 +0,0 @@ -int* twoSum(int* nums, int numsSize, int target, int* returnSize) { - for (int i = 0; i < numsSize; ++i) { - for (int j = i + 1; j < numsSize; ++j) { - if (nums[i] + nums[j] == target) { - int* ret = malloc(sizeof(int) * 2); - ret[0] = i, ret[1] = j; - *returnSize = 2; - return ret; - } - } - } - *returnSize = 0; - return NULL; -} \ No newline at end of file diff --git "a/2209040071/chapter1,2/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.cpp" "b/2209040071/chapter1,2/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.cpp" deleted file mode 100644 index e6b8c2d733a12344f47e5eef83771eb3307a48ed..0000000000000000000000000000000000000000 --- "a/2209040071/chapter1,2/\345\220\210\345\271\2662\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.cpp" +++ /dev/null @@ -1,11 +0,0 @@ -int cmp(int *i,int *j) -{ - return*i-*j; -} -void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){ - for(int k=0;k!=n;k++) - { - nums1[m+k]=nums2[k]; - } - qsort(nums1,nums1Size,sizeof(int),cmp); -} \ No newline at end of file diff --git "a/2209040071/chapter1,2/\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" "b/2209040071/chapter1,2/\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" deleted file mode 100644 index c511198f5fc12ad3a496729ca6afd8c219bca9f9..0000000000000000000000000000000000000000 --- "a/2209040071/chapter1,2/\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" +++ /dev/null @@ -1,25 +0,0 @@ -#include -int arrSum(int* arr, int start, int end){ - int b, res = 0; - for(b = start; b < start + end; b++) - { - res += arr[b]; - } - - return res; -} -int sumOddLengthSubarrays(int* arr, int arrSize){ - int b = 0; - int d = 0; - int k = 0; - int ans = 0; - - for(b = 1; b <= arrSize; b += 2){ - for(d = 0; d < b; d += b){ - for(k = d; k < arrSize - b + 1;k++){ - ans += arrSum(arr, k ,b); - } - } - } - return ans; -} \ No newline at end of file diff --git "a/2209040071/chapter1,2/\347\247\273\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\345\205\203\347\264\240.cpp" "b/2209040071/chapter1,2/\347\247\273\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\345\205\203\347\264\240.cpp" deleted file mode 100644 index a8ca13472188ad70f78b1b60906f05ee9a6f4597..0000000000000000000000000000000000000000 --- "a/2209040071/chapter1,2/\347\247\273\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\345\205\203\347\264\240.cpp" +++ /dev/null @@ -1,25 +0,0 @@ -struct ListNode* removeElements(struct ListNode* head, int val){ - while (NULL != head && head->val == val) - { - head = head->next; - } - - - // - struct ListNode* curr=head; - struct ListNode* prev=NULL; - while(curr!=NULL) - { - if(curr->val!=val) - { - prev=curr; - } - else - { - prev->next=curr->next; - } - curr=curr->next; - } - return head; - -} \ No newline at end of file diff --git "a/2209040071/chapter1,2/\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2209040071/chapter1,2/\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index f1c80d87acbfc4b605ff7b28addcd1639a45c7d2..0000000000000000000000000000000000000000 --- "a/2209040071/chapter1,2/\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include -#include -bool prime1(long n) -{ - long b; - for(b=2;bnext; - cur->next = pre; - pre = cur; - cur = next; - } -} -struct ListNode* reverseBetween(struct ListNode* head, int left, int right){ - // 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论 - struct ListNode *dummyNode = malloc(sizeof(struct ListNode)); - dummyNode->val = -1; - dummyNode->next = head; - struct ListNode *pre = dummyNode; - // 第 1 步:从虚拟头节点走 left - 1 步,来到 left 节点的前一个节点 - // 建议写在 for 循环里,语义清晰 - for (int i = 0; i < left - 1; i++) { - pre = pre->next; - } - // 第 2 步:从 pre 再走 right - left + 1 步,来到 right 节点 - struct ListNode *rightNode = pre; - for (int i = 0; i < right - left + 1; i++) { - rightNode = rightNode->next; - } - // 第 3 步:切断出一个子链表(截取链表) - struct ListNode *leftNode = pre->next; - struct ListNode *curr = rightNode->next; - // 注意:切断链接 - pre->next = NULL; - rightNode->next = NULL; - // 第 4 步:同第 206 题,反转链表的子区间 - reverseLinkedList(leftNode); - // 第 5 步:接回到原来的链表中 - pre->next = rightNode; - leftNode->next = curr; - return dummyNode->next; -} diff --git "a/2209040071/chapter1,2/ \344\270\244\344\270\244\347\233\270\344\272\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" "b/2209040071/chapter2/ \344\270\244\344\270\244\347\233\270\344\272\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" similarity index 100% rename from "2209040071/chapter1,2/ \344\270\244\344\270\244\347\233\270\344\272\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" rename to "2209040071/chapter2/ \344\270\244\344\270\244\347\233\270\344\272\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" diff --git "a/2209040071/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2209040071/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index 269a96b7d42109dddb60aaf521e4f6738f7361ad..0000000000000000000000000000000000000000 --- "a/2209040071/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,30 +0,0 @@ -#include "sqqueue. cpp" -int main() -{ ElemType e; -SqQueue *q; -printf("环形队列基本运算如下:\n"); -printf(" (1)初始化队列 q\n"); -InitQueue(q); -printf(" (2)依次进队列元素a,b,c\n"); -if(!enQueue(q, 'a')) printf("\t提示:队满,不能进队\n"); -if(!enQueue(q,'b')) printf("\t 提示:队满,不能进队\n"); -if(!enQueue(q,'c')) printf("\t 提示:队满,不能进队\n"); -printf("(3)队列为%s\n",(QueueEmpty(q)?"空":"非空")); -if (deQueue(q,e) == 0) - printf("队空,不能出队\n"); -else - printf(" (4)出队一个元素子c\n",e); -printf(" (5)依次进队列元素 d,e,f\n"); -if(!enQueue(q,'d')) printf("\t 提示:队满,不能进队\n"); -if(!enQueue(q,'e')) printf("\t提示:队满,不能进队\n"); -if(!enQueue(q,'f')) printf("\t提示:队满,不能进队\n"); -printf(" (6)出队列序列:"); -while(!QueueEmpty(q)) -{ deQueue(q,e); -printf("&c ",e); -} -printf("\n"); -printf(" (7)释放队列\n"); -DestroyQueue(q); -return 1; -} \ No newline at end of file diff --git "a/2209040071/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217.cpp" "b/2209040071/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217.cpp" deleted file mode 100644 index b1674e3e59a27249b165dda5dab747cc60fab7fe..0000000000000000000000000000000000000000 --- "a/2209040071/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217.cpp" +++ /dev/null @@ -1,26 +0,0 @@ -#include"sqstack.cpp" -int main(){ - ElemType e; - Sqstack *s; - printf("顺序栈s的基本运算如下:\n"); - printf("(1)初始化栈s\n"); - Initstack(s); - printf("(2)栈为%s\n",(StackEmpty(s)?"空":"非空")); - printf("(3)依次进栈元素a,b,c,d,e\n"); - push(s,'a'); - push(s,'b'); - push(s,'c'); - push(s,'d'); - push(s,'e'); - printf("(4)栈为%s\n",(StackEmpty(s)?"空":"非空")); - printf("(5)出栈序列:"); - while(!StackEmpty(s)){ - pop(s,e); - printf("%c",e); - } - printf("\n"); - printf("(6)栈为%s\n",(StackEmpty(s)?"空":"非空")); - printf("(7)释放栈\n"); - destroystack(s); - return 1; -} diff --git "a/2209040075/chapter2/ex2-10\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\347\256\227\346\263\225.cpp" "b/2209040075/chapter2/ex2-10\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index 10345547c55cb32d82bea60c637f0d1bc9a9362a..0000000000000000000000000000000000000000 --- "a/2209040075/chapter2/ex2-10\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,133 +0,0 @@ -#include -#include - -typedef struct PolyNode* Polynomial; - -struct PolyNode { - int coefficient; - int expon; - Polynomial next; -}; - -void Attach(int c, int e, Polynomial* p) -{ - Polynomial temp = (Polynomial)malloc(sizeof(struct PolyNode)); - temp->coefficient = c; - temp->expon = e; - temp->next = NULL; - (*p)->next = temp; - (*p) = temp; -} - -Polynomial ReadPoly() -{ - Polynomial p, r, temp; - int c, e, N; - p = (Polynomial)malloc(sizeof(struct PolyNode)); - p->next = NULL; - r = p; - scanf("%d", &N); - while (N--) - { - scanf("%d %d", &c, &e); - Attach(c, e, &r); - } - temp = p; - p = p->next; - free(temp); - return p; -} - -Polynomial initPoly(Polynomial p1, Polynomial p2) -{ - Polynomial p, t, r; - p = (Polynomial)malloc(sizeof(struct PolyNode)); - p->next = NULL; - t = (Polynomial)malloc(sizeof(struct PolyNode)); - t = p2; - r = p; - while (t) - { - Attach((p1->coefficient) * (t->coefficient), p1->expon + t->expon, &r); - t = t->next; - } - return p; -} - -Polynomial Mult(Polynomial p1, Polynomial p2) -{ - Polynomial p3 = initPoly(p1, p2); - Polynomial t1, t2, r, temp; - int c, e; - t1 = p1->next; - while (t1) - { - r = p3; - t2 = p2; - while (t2) - { - c = (t1->coefficient) * (t2->coefficient); - e = (t1->expon) + (t2->expon); - while ((rear->next) && (rear->next->expon > e)) - { - r = r->next; - } - if ((r->next) && (r->next->expon == e)) - { - if (r->next->coefficient + c) - { - r->next->coefficient += c; - } - else - { - temp = r->next; - r->next = temp->next; - free(temp); - } - } - else - { - temp = (Polynomial)malloc(sizeof(struct PolyNode)); - temp->coefficient = c; - temp->expon = e; - temp->next = r->next; - r->next = temp; - } - t2 = t2->next; - } - t1 = t1->next; - } - temp = p3; - p3 = p3->next; - free(temp); - return p3; -} - -void PrintPoly(Polynomial p) -{ - Polynomial Q; - Q = p; - if (!Q) - printf("0\n"); - while (Q->next) - { - printf("%dx^%d + ", Q->coefficient, Qptr->expon); - Q = Q->next; - } - printf("%dx^%d\n", Q->coefficient, Q->expon); -} - -int main() -{ - Polynomial p1, p2, result; - p1 = ReadPoly(); - p2 = ReadPoly(); - result = Mult(p1, p2); - printf("多项式1为:\n"); - PrintPoly(p1); - printf("多项式2为:\n"); - PrintPoly(p2); - printf("两式相乘结果为:\n"); - PrintPoly(result); - return 0; -} \ No newline at end of file diff --git "a/2209040075/chapter2/lc_\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" "b/2209040075/chapter2/lc_\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" deleted file mode 100644 index 6625fdb5462dc47219052c87570064422acaea24..0000000000000000000000000000000000000000 --- "a/2209040075/chapter2/lc_\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.cpp" +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode() : val(0), next(nullptr) {} - * ListNode(int x) : val(x), next(nullptr) {} - * ListNode(int x, ListNode *next) : val(x), next(next) {} - * }; - */ -class Solution { -public: - ListNode* swapPairs(ListNode* head) { - ListNode*dummy=new ListNode(0); - dummy->next=head; - ListNode *pre =dummy,*cur =dummy->next; - while (cur && cur->next){ - ListNode *next=cur->next; - pre->next=next; - cur->next=next->next; - next->next=cur; - pre=cur; - cur=cur->next; - } - return dummy->next; - } -}; \ No newline at end of file diff --git "a/2209040075/chapter2/lc_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" "b/2209040075/chapter2/lc_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" deleted file mode 100644 index 6007f38937bf392cd61ae0797e68c72f8e856d13..0000000000000000000000000000000000000000 --- "a/2209040075/chapter2/lc_\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.cpp" +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode() : val(0), next(nullptr) {} - * ListNode(int x) : val(x), next(nullptr) {} - * ListNode(int x, ListNode *next) : val(x), next(next) {} - * }; - */ -class Solution { -public: - ListNode* deleteDuplicates(ListNode* head) { - ListNode *dummy=new ListNode(0),*p=dummy; - dummy->next=head; - while(p->next){ - ListNode *q=p->next; - while(q->next && q->next->val==q->val)q=q->next; - if(q==p->next)p=p->next; - else p->next=q->next; - - } - return dummy->next; - } -}; \ No newline at end of file diff --git "a/2209040075/chapter2/lc_\345\220\210\345\271\266\344\270\244\344\270\252\346\225\260\347\273\204.cpp" "b/2209040075/chapter2/lc_\345\220\210\345\271\266\344\270\244\344\270\252\346\225\260\347\273\204.cpp" deleted file mode 100644 index 3017745ce49286afde6de68f2e04f14fb2ddf677..0000000000000000000000000000000000000000 --- "a/2209040075/chapter2/lc_\345\220\210\345\271\266\344\270\244\344\270\252\346\225\260\347\273\204.cpp" +++ /dev/null @@ -1,12 +0,0 @@ -class Solution { -public: - void merge(vector& nums1, int m, vector& nums2, int n) { - int t=m+n-1; - int i=m-1,j=n-1; - while(i>=0 && j>=0){ - if (nums1[i]>nums2[j]) nums1[t--]=nums1[i--]; - else nums1[t--]=nums2[j--]; - } - while(j>=0) nums1[t--]=nums2[j--]; - } -}; \ No newline at end of file diff --git "a/2209040075/chapter2/lc_\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" "b/2209040075/chapter2/lc_\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" deleted file mode 100644 index 25b60f01955ee6cd31355fb027290fb3f8755e39..0000000000000000000000000000000000000000 --- "a/2209040075/chapter2/lc_\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode() : val(0), next(nullptr) {} - * ListNode(int x) : val(x), next(nullptr) {} - * ListNode(int x, ListNode *next) : val(x), next(next) {} - * }; - */ -class Solution { -public: - ListNode* removeElements(ListNode* head, int val) { - ListNode* dummy=new ListNode(0); - dummy->next =head; - ListNode*cur = dummy; - while(cur->next){ - if(cur->next->val==val){ - cur->next=cur->next->next; - } else{ - cur=cur->next; - } - - } - return dummy->next; - } -}; \ No newline at end of file diff --git "a/2209040075/chapter2/lc_\347\277\273\350\275\254\345\210\227\350\241\250.cpp" "b/2209040075/chapter2/lc_\347\277\273\350\275\254\345\210\227\350\241\250.cpp" deleted file mode 100644 index 8c27ca115cb8b5e104b0e07ad1c1f1f2bd5fe558..0000000000000000000000000000000000000000 --- "a/2209040075/chapter2/lc_\347\277\273\350\275\254\345\210\227\350\241\250.cpp" +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode() : val(0), next(nullptr) {} - * ListNode(int x) : val(x), next(nullptr) {} - * ListNode(int x, ListNode *next) : val(x), next(next) {} - * }; - */ -class Solution { -public: - ListNode* reverseBetween(ListNode* head, int left, int right) { - ListNode *dummy=new ListNode(0); - dummy->next=head; - - ListNode *pre =dummy; - for(int i =0 ;inext; - ListNode *cur = pre->next; - for(int i = 0;inext; - cur->next=next->next; - next->next=pre->next; - pre->next=next; - } - return dummy->next; - } -}; \ No newline at end of file diff --git "a/2209040075/chapter2/\345\256\236\351\252\2142.2.cpp" "b/2209040075/chapter2/\345\256\236\351\252\2142.2.cpp" deleted file mode 100644 index 89a3e6984645e55db8186ca53cb8633fc5edbb4b..0000000000000000000000000000000000000000 --- "a/2209040075/chapter2/\345\256\236\351\252\2142.2.cpp" +++ /dev/null @@ -1,33 +0,0 @@ -#include "linklist.cpp" -int main(){ -LinkNode *h; -ELemType e; -printf("顺序表的基本运算如下:\n"); -printf(" (1)初始化单链表h\n"); -Initlist(h); -printf(" (2)依次采用尾插法插入元素a,b,c,d,e\n"); -ListInsert(h,1,'a'); -ListInsert(h,2,'b'); -ListInsert(h,3,'c'); -ListInsert(h,4,'d'); -ListInsert(h,5,'e'); -printf(" (3)输出单链表h:\n"); -Displist(h); -printf(" (4)单链表h长度:%d\n",ListLenghth(h)); -printf(" (5)单链表h为%s\n",(ListEmpty(h)?"空":"非空")); -GetElem(h,3,e); -printf(" (6)单链表h的第三个元素:%c\n",e); -printf(" (7)元素a的位置:%d\n",LocateELem(h,'a')); -printf(" (8)在第四个元素位置上插入元素f\n"); -ListInsert(h,4,'f'); -printf(" (9)输出单链表h:") -DispList(h); -printf(" (10)删除h的第3个元素\n"); -ListDelete(h,3,e); -printf(" (11)输出单链表h:"); -DispList(h); -printf(" (12)释放单链表h\n"); -DestroyList(h); -return 1; - -} diff --git "a/2209040075/chapter2/\345\256\236\351\252\2142.6.cpp" "b/2209040075/chapter2/\345\256\236\351\252\2142.6.cpp" deleted file mode 100644 index c3cf193e308a1ab1c6a1480b1492b1ada98fa94c..0000000000000000000000000000000000000000 --- "a/2209040075/chapter2/\345\256\236\351\252\2142.6.cpp" +++ /dev/null @@ -1,77 +0,0 @@ -#include "linklist.cpp" -#include "string.h" -void split1(LinkNode * &L){ -LinkNode *pre,*p,*q; -if(L->next == NULL || L->next->next == NULL) - return; -int x=L-next->data; -pre=L->next; -p=pre-next; -while(p!=NULL){ -if(p->datanext=p->next; - p->next=L->next; - L-next=p; - p=pre-next; -} -else -{ - pre=p; - p=pre->next; -} - -} - - -} -void split2(LinkNode *&l){ -LinkNode *p=L->next,*r,*r1,*L1; -if (L->next==NULL || L->next->next==NULL) -return; -int x=L->next->data; -r=L; -L1=(LinkNode *)malloc(sizeof(LinkNode)); -r1=L1; -while(p!==NULL){ -if(p->nextnext=p; -r=p; -p=p->next; - -} -else -{ - r1->next=p; - r1=p; - p=p->next; -} -} -r1->next=NULL; -r->next=L1->next; -free(L1); - -} -int main(){ - int d; - LinkNode *L; - ELemType a[]="daxgdchaeb"; - int n=strlen(a); - printf("solution 1\n"); - CreateListR(L,a,n); - printf("L:"); - DispList(L); - printf("以首节点值进行划分\n"); - Split1(L); - printf("L:"); - DispList(L); - DestroyList(L); - printf("solution 2\n"); - CreateListR(L,a,n); - printf("L:"); - DispList(L); - printf("以首节点值进行划分\n"); - Split2(L); - printf("L: ");Displist(L); - DestroyList(L); - return 1; -} diff --git "a/2209040075/lc_1\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" "b/2209040075/lc_1\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" deleted file mode 100644 index c511198f5fc12ad3a496729ca6afd8c219bca9f9..0000000000000000000000000000000000000000 --- "a/2209040075/lc_1\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" +++ /dev/null @@ -1,25 +0,0 @@ -#include -int arrSum(int* arr, int start, int end){ - int b, res = 0; - for(b = start; b < start + end; b++) - { - res += arr[b]; - } - - return res; -} -int sumOddLengthSubarrays(int* arr, int arrSize){ - int b = 0; - int d = 0; - int k = 0; - int ans = 0; - - for(b = 1; b <= arrSize; b += 2){ - for(d = 0; d < b; d += b){ - for(k = d; k < arrSize - b + 1;k++){ - ans += arrSum(arr, k ,b); - } - } - } - return ans; -} \ No newline at end of file diff --git "a/2209040077/chapter3/lc_\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" "b/2209040077/chapter3/lc_\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" deleted file mode 100644 index a5b339eb3d65bd2c812ce606fff5d44cc096ffcb..0000000000000000000000000000000000000000 --- "a/2209040077/chapter3/lc_\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" +++ /dev/null @@ -1,109 +0,0 @@ -#include -#include - -#define bool char -#define MaxSize 1000 - -typedef int ElemType; - -typedef struct Sk -{ - ElemType data[MaxSize]; - int top; -} Sk; - -bool ck(Sk* S) -{ - S->top = -1; - return 1; -} - -bool Ey(Sk S) -{ - if(S.top == -1) - return 1; - else - return 0; -} - -bool Push(Sk* S, ElemType e) -{ - if(S->top == MaxSize-1) - return 0; - S->data[++S->top] = e; - return 1; -} - -bool Pp(Sk* S, ElemType* e) -{ - if(S->top == -1) - return 0; - *e = S->data[S->top--]; - return 1; -} - -bool Pt(Sk S) -{ - for(int i = 0; i < S.top+1; i++) - { - printf("%d ",S.data[i]); - } - return 1; -} - -int main() -{ - Sk S1, S2; - ck(&S1); - ck(&S2); - int er; - - char str[1000]; - gets(str); - - char c = str[0]; - int i = 0; - for(i = 0; str[i] != '\0'; i++) - { - c = str[i]; - int x, t; - if(str[i] <= '9' && str[i] >= '0') - { - t = 0; - while(str[i] <= '9' && str[i] >= '0') - { - x = str[i]-48; - t = t*10 + x; - i++; - } - - Push(&S1, t); - i--; - } - else if(c == ' ') - continue; - else if(c == '+' || c == '-' || c == '*') - { - - int m,p; - Pp(&S1, &m); - Pp(&S1, &p); - - if(c == '+') - { - er = p+m; - } - else if(c == '-') - { - er = p-m; - } - else if(c == '*') - { - er = p*m; - } - Push(&S1, er); - } - } - printf("%d", er); - return 0; -} \ No newline at end of file diff --git "a/2209040077/chapter3/lc_\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp " "b/2209040077/chapter3/lc_\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp " deleted file mode 100644 index a5b339eb3d65bd2c812ce606fff5d44cc096ffcb..0000000000000000000000000000000000000000 --- "a/2209040077/chapter3/lc_\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp " +++ /dev/null @@ -1,109 +0,0 @@ -#include -#include - -#define bool char -#define MaxSize 1000 - -typedef int ElemType; - -typedef struct Sk -{ - ElemType data[MaxSize]; - int top; -} Sk; - -bool ck(Sk* S) -{ - S->top = -1; - return 1; -} - -bool Ey(Sk S) -{ - if(S.top == -1) - return 1; - else - return 0; -} - -bool Push(Sk* S, ElemType e) -{ - if(S->top == MaxSize-1) - return 0; - S->data[++S->top] = e; - return 1; -} - -bool Pp(Sk* S, ElemType* e) -{ - if(S->top == -1) - return 0; - *e = S->data[S->top--]; - return 1; -} - -bool Pt(Sk S) -{ - for(int i = 0; i < S.top+1; i++) - { - printf("%d ",S.data[i]); - } - return 1; -} - -int main() -{ - Sk S1, S2; - ck(&S1); - ck(&S2); - int er; - - char str[1000]; - gets(str); - - char c = str[0]; - int i = 0; - for(i = 0; str[i] != '\0'; i++) - { - c = str[i]; - int x, t; - if(str[i] <= '9' && str[i] >= '0') - { - t = 0; - while(str[i] <= '9' && str[i] >= '0') - { - x = str[i]-48; - t = t*10 + x; - i++; - } - - Push(&S1, t); - i--; - } - else if(c == ' ') - continue; - else if(c == '+' || c == '-' || c == '*') - { - - int m,p; - Pp(&S1, &m); - Pp(&S1, &p); - - if(c == '+') - { - er = p+m; - } - else if(c == '-') - { - er = p-m; - } - else if(c == '*') - { - er = p*m; - } - Push(&S1, er); - } - } - printf("%d", er); - return 0; -} \ No newline at end of file diff --git "a/2209040077/chapter3/lc\345\237\272\346\234\254\350\256\241\347\256\227\346\234\2722.cpp" "b/2209040077/chapter3/lc\345\237\272\346\234\254\350\256\241\347\256\227\346\234\2722.cpp" deleted file mode 100644 index 1c1addef2a0e0447d3f08ed066f89d4958f39c1d..0000000000000000000000000000000000000000 --- "a/2209040077/chapter3/lc\345\237\272\346\234\254\350\256\241\347\256\227\346\234\2722.cpp" +++ /dev/null @@ -1,70 +0,0 @@ -#include -#include -#include -using namespace std; -double op(double n1,double n2,char ra); -int main() { - stack num; - stack s; - char io[1000] = {0}; - fgets(io, 1000, stdin); - for(int i = 0; i='0' && io[i]<='9') { - num.push((double)io[i]-48); - i++; - int a=1; - double n; - while(io[i]>='0' && io[i]<='9') { - n = num.top(); - n*=10; - n+=io[i]-48; - a++; - i++; - num.pop(); - num.push(n); - } - i--; - } - else if(io[i] == '+' || io[i] == '-' || io[i] == '*' || io[i] == '/' || io[i] == '(' || io[i] == ')') { - if(s.empty()) s.push(io[i]); - else { - if((io[i]==')'&&s.top()=='(')) s.pop(); - else if(io[i]=='('||s.top()=='('||((io[i]=='*'||io[i]=='/')&&(s.top()=='+'||s.top()=='-')))s.push(io[i]); - else { - double n1 = num.top(); - num.pop(); - double n2 = num.top(); - num.pop(); - char opera = s.top(); - s.pop(); - num.push(op(n1,n2,opera)); - s.push(io[i]); - } - } - } - } - while(!s.empty()) { - char opera = s.top(); - s.pop(); - if(opera!='('&&opera!=')'){ - double n1 = num.top(); - num.pop(); - double n2 = num.top(); - num.pop(); - num.push(op(n1,n2,opera)); - } - } - printf("%lf",num.top()); -} -double op(double n1,double n2,char opera) { - switch(opera) { - case '+': - return n2+n1; - case'-': - return n2-n1; - case'*': - return n2*n1; - case'/': - return n2/n1; - } -} \ No newline at end of file diff --git "a/2209040079/chapter2/exp2-2\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2209040079/chapter2/exp2-2\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" index 44cede57d326c6c73a1e656bdcc1c9757825e522..133ea9f4e9d377fc5bf6165b54655cd97f54fe4e 100644 --- "a/2209040079/chapter2/exp2-2\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ "b/2209040079/chapter2/exp2-2\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" @@ -1,23 +1,4 @@ -#include -#include - -typedef char ElemType; - -typedef struct LNode { - ElemType data; - struct LNode *next; -} LinkNode; -void CreateListF(LinkNode *&L, ElemType a[], int n); -void CreateListR(LinkNode *&L, ElemType a[], int n); -void InitList(LinkNode *&L); -void DestroyList(LinkNode *&L); -bool ListEmpty(LinkNode *L); -int ListLength(LinkNode *L); -void DispList(LinkNode *L); -bool GetElem(LinkNode *L, int i, ElemType &e); -int LocateElem(LinkNode *L, ElemType e); -bool ListInsert(LinkNode *&L, int i, ElemType e); -bool ListDelete(LinkNode *&L, int i, ElemType &e) ; +#include "linklist.cpp" int main() { int l; diff --git "a/2209040079/chapter3/exp3-1\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2209040079/chapter3/exp3-1\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" index 4c2717523af637d22fdfb4edadb35d4702a30dc1..cadfcf8a84e677223df537aa4c80d733f59e459c 100644 --- "a/2209040079/chapter3/exp3-1\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ "b/2209040079/chapter3/exp3-1\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" @@ -1,49 +1,4 @@ -#include -#include -#define MaxSize 50 -typedef int ElemType; -typedef struct{ - ElemType data[MaxSize]; - int top; -}SqStack; -void IniStack(SqStack *&s){ - s=(SqStack *)malloc(sizeof(SqStack)); - s->top=-1; -} -bool StackEmpty(SqStack *s){ - return(s->top==-1); -} -bool Push(SqStack *&s,ElemType e) -{ - if(s->top==MaxSize-1) - { - return false; - } - s->top++; - s->data[s->top]=e; - return true; -} -bool Pop(SqStack *&s,ElemType &e) -{ - if(s->top==-1) - { - return false; - } - e=s->data[s->top]; - s->top--; - return true -} -void DestroyStack(SqStack *&s) -{ - free(s); -} -bool GetTop(SqStack *s,ElemType &e) -{ - if(s->top==-1) - return false; - e=s->data[s->top]; - return true; -} +#include"sqstack.cpp" int main() { ElemType e; diff --git "a/2209040079/chapter3/exp3-3\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2209040079/chapter3/exp3-3\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" deleted file mode 100644 index 8bd7ae707ba7f514e41f4fc5bb5de09dbca57085..0000000000000000000000000000000000000000 --- "a/2209040079/chapter3/exp3-3\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,74 +0,0 @@ -#include -#include -#define MaxSize 10 -typedef char ElemType; -typedef struct -{ - ElemType data[MaxSize]; - int front,rear; -} SqQueue; -void InitQueue(SqQueue *&q) -{ q=(SqQueue *)malloc (sizeof(SqQueue)); - q->front=q->rear=0; -} -void DestroyQueue(SqQueue *&q) -{ - free(q); -} -bool QueueEmpty(SqQueue *q) -{ - return(q->front==q->rear); -} -bool enQueue(SqQueue *&q,ElemType e) -{ - if ((q->rear+1)%MaxSize==q->front) - return false; - q->rear=(q->rear+1)%MaxSize; - q->data[q->rear]=e; - return true; -} -bool deQueue(SqQueue *&q,ElemType &e) -{ - if (q->front==q->rear) - return false; - q->front=(q->front+1)%MaxSize; - e=q->data[q->front]; - return true; -} -int length(SqQueue *&q){ - if(q->rear>q->front) - return q->rear-q->front; - else - return q->front-q->rear; -} -int main() -{ - ElemType e; - SqQueue *q; - printf("环形队列的基本运算如下:\n"); - printf("(1)初始化队列q\n"); - InitQueue(q); - printf("(2)依次进队列元素a、b、c\n"); - enQueue(q,'a'); - enQueue(q,'b'); - enQueue(q,'c'); - printf("(3)队列%s\n",(QueueEmpty(q)?"空":"非空")); - if(deQueue(q,e)==0) - printf("队空不能出队\n"); - else - printf("(4)出队一个元素%c\n",e); - printf("(5)依次进队d、e、f\n"); - enQueue(q,'d'); - enQueue(q,'e'); - enQueue(q,'f'); - printf("(6)出队序列:"); - while(!QueueEmpty(q)) - { - deQueue(q,e); - printf("%c",e); - } - printf("\n"); - printf("(7)释放队列\n"); - free(q); - return 0; -} diff --git "a/2209040079/chapter4/lc\345\256\236\347\216\260strStr().cpp" "b/2209040079/chapter4/lc\345\256\236\347\216\260strStr().cpp" deleted file mode 100644 index 01c3150ee7c28ea0fb9116e65184f5a8c62c9011..0000000000000000000000000000000000000000 --- "a/2209040079/chapter4/lc\345\256\236\347\216\260strStr().cpp" +++ /dev/null @@ -1,21 +0,0 @@ -class Solution { -public: - int strStr(string haystack, string needle) { - int i,j; - bool m; - int a=haystack.size(); - int b=needle.size(); - for(i=0;i<=a-b;i++){ - m=true; - for(j=0;jval; - head = head->next; - m++; - } - for(i = 0,j = m-1; i - -int* Next(char* dest) { - int len = 0; - while (dest[len] != '\0') { - len++; - } - int* next = (int*)malloc((len + 1) * sizeof(int)); - next[0] = 0; - for (int i = 1, j = 0; i < len; i++) { - while (j > 0 && dest[i] != dest[j]) { - j = next[j - 1]; - } - if (dest[i] == dest[j]) { - j++; - } - next[i] = j; - } - return next; -} - -int strStr(char* haystack, char* needle) { - int len1 = 0; - while (haystack[len1] != '\0') { - len1++; - } - int len2 = 0; - while (needle[len2] != '\0') { - len2++; - } - int* next = Next(needle); - for (int i = 0, j = 0; i < len1; i++) { - while (j > 0 && haystack[i] != needle[j]) { - j = next[j - 1]; - } - if (haystack[i] == needle[j]) { - j++; - } - if (j == len2) { - free(next); - return i - j + 1; - } - } - free(next); - return -1; -} \ No newline at end of file diff --git "a/2224020014/chapter_4/\346\225\260\347\273\204\344\270\255\345\255\227\347\254\246\344\270\262\347\232\204\345\214\271\351\205\215.cpp" "b/2224020014/chapter_4/\346\225\260\347\273\204\344\270\255\345\255\227\347\254\246\344\270\262\347\232\204\345\214\271\351\205\215.cpp" deleted file mode 100644 index 2372f1725126b230a4e00bc9e29e54e5dec9f95e..0000000000000000000000000000000000000000 --- "a/2224020014/chapter_4/\346\225\260\347\273\204\344\270\255\345\255\227\347\254\246\344\270\262\347\232\204\345\214\271\351\205\215.cpp" +++ /dev/null @@ -1,57 +0,0 @@ -int KMP(char* text, char* pattern) { - int n = strlen(text); - int m = strlen(pattern); - // 计算模式串的next数组 - int* next = (int*)malloc(sizeof(int) * m); - next[0] = -1; - int i = 0, j = -1; - while (i < m - 1) { - if (j == -1 || pattern[i] == pattern[j]) { - i++; - j++; - next[i] = j; - } else { - j = next[j]; - } - } - // 使用next数组进行匹配 - i = 0; - j = 0; - while (i < n && j < m) { - if (j == -1 || text[i] == pattern[j]) { - i++; - j++; - } else { - j = next[j]; - } - } - // 如果匹配成功,返回子字符串在文本串中的起始位置 - if (j == m) { - return i - m; - } - // 否则返回-1 - return -1; -} - -char** stringMatching(char** words, int wordsSize, int* returnSize) { - char** res = (char**)malloc(sizeof(char*) * wordsSize); - int resSize = 0; - // 遍历所有单词 - for (int i = 0; i < wordsSize; i++) { - char* word = words[i]; - // 在其它单词中进行匹配 - for (int j = 0; j < wordsSize; j++) { - // 跳过与自己匹配 - if (i == j) { - continue; - } - // 如果找到了子字符串位置,将该单词添加到结果数组中 - if (KMP(words[j], word) != -1) { - res[resSize++] = word; - break; - } - } - } - *returnSize = resSize; - return res; -} \ No newline at end of file diff --git "a/2224020082/chapter1/lc\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\232\204\345\222\214.cpp" "b/2224020082/chapter1/lc\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\232\204\345\222\214.cpp" deleted file mode 100644 index 0581723fa49945ed4babfaed3b0fe89cc9b855b5..0000000000000000000000000000000000000000 --- "a/2224020082/chapter1/lc\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\232\204\345\222\214.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - int sumOddLengthSubarrays(vector& arr) { -int m=0; - for(int i=0;i -#include -#define MAX 20 -typedef struct node -{ - double coef; - int exp; - struct node *next; -} PolyNode; -void CreatePolyR(PolyNode *&L,double a[],int b[],int n) -{ - PolyNode *s,*r;int i; - L=(PolyNode *)malloc(sizeof(PolyNode)); - L->next=NULL; - r=L; - for (i=0;icoef=a[i]; - s->exp=b[i]; - r->next=s; - r=s; - } - r->next=NULL; -} -void DestroyPoly(PolyNode *&L) -{ - PolyNode *pre=L,*p=pre->next; - while (p!=NULL) - { - free(pre); - pre=p; - p=pre->next; - } - free(pre); -} -void DispPoly(PolyNode *L) -{ - bool first=true; - PolyNode *p=L->next; - while (p!=NULL) - { - if (first) - first=false; - else if (p->coef>0) - printf("+"); - if (p->exp==0) - printf("%g",p->coef); - else if (p->exp==1) - printf("%gx",p->coef); - else - printf("%gx^%d",p->coef,p->exp); - p=p->next; - } - printf("\n"); -} -void Sort(PolyNode *&L) -{ - PolyNode *p=L->next,*pre,*q; - if (p!=NULL) - { - q=p->next; - p->next=NULL; - p=q; - while (p!=NULL) - { - q=p->next; - pre=L; - while (pre->next!=NULL && pre->next->exp>p->exp) - pre=pre->next; - p->next=pre->next; - pre->next=p; - p=q; - } - } -} -void Mult1(PolyNode *ha,PolyNode *hb,PolyNode *&hc) -{ - PolyNode *pa=ha->next,*pb,*s,*tc; - hc=(PolyNode *)malloc(sizeof(PolyNode)); - tc=hc; - while (pa!=NULL) - { - pb=hb->next; - while (pb!=NULL) - { - s=(PolyNode *)malloc(sizeof(PolyNode)); - s->coef=pa->coef*pb->coef; - s->exp=pa->exp+pb->exp; - tc->next=s; - tc=s; - pb=pb->next; - } - pa=pa->next; - } - tc->next=NULL; -} -void Comb(PolyNode *&L) -{ - PolyNode *pre=L->next,*p; - if (pre==NULL) return; - p=pre->next; - while (p!=NULL) - { - while (p->exp==pre->exp) - { - pre->coef+=p->coef; - pre->next=p->next; - free(p); - p=pre->next; - } - pre=p; - p=p->next; - } -} -void DelZero(PolyNode *&L) -{ - PolyNode *pre=L,*p=pre->next; - while (p!=NULL) - { - if (p->coef==0.0) - { - pre->next=p->next; - free(p); - } - pre=p; - p=p->next; - } -} -void Mult(PolyNode *ha,PolyNode *hb,PolyNode *&hc) -{ - Mult1(ha,hb,hc); - printf("相乘结果: ");DispPoly(hc); - Sort(hc); - printf("按指数排序后: ");DispPoly(hc); - Comb(hc); - printf("合并重复指数项:");DispPoly(hc); - DelZero(hc); - printf("删除序数为0项: ");DispPoly(hc); -} -int main() -{ - PolyNode *Poly1,*Poly2,*Poly3; - double a[MAX]; - int b[MAX],n; - - a[0]=2;b[0]=3; a[1]=1;b[1]=0; a[2]=3;b[2]=1; - n=3; - printf("第1个多项式:\n"); - CreatePolyR(Poly1,a,b,n); - printf(" 排序前多项式1:");DispPoly(Poly1); - Sort(Poly1); - printf(" 排序后多项式1:");DispPoly(Poly1); - - printf("第2个多项式:\n"); - a[0]=2; b[0]=3; a[1]=-3;b[1]=2; - a[2]=5; b[2]=4; a[3]=-3;b[3]=0; - n=4; - CreatePolyR(Poly2,a,b,n); - printf(" 排序前多项式2:");DispPoly(Poly2); - Sort(Poly2); - printf(" 排序后多项式2:");DispPoly(Poly2); - Mult(Poly1,Poly2,Poly3); - printf("相乘后多项式3: ");DispPoly(Poly3); - DestroyPoly(Poly1); - DestroyPoly(Poly2); - DestroyPoly(Poly3); - return 1; -} diff --git a/2224020082/exp3-1.cpp b/2224020082/exp3-1.cpp deleted file mode 100644 index 5644257ce1246fb3ca2287b0e52ec432b1fd0207..0000000000000000000000000000000000000000 --- a/2224020082/exp3-1.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "sqstack.cpp" -int main() -{ - ElemType e; - SqStack *s; - printf("顺序栈s的基本运算如下:\n"); - printf(" (1)初始化栈s\n"); - InitStack(s); - printf(" (2)栈为%s\n",(StackEmpty(s)?"空":"非空")); - printf(" (3)依次进栈元素a,b,c,d,e\n"); - Push(s,'a'); - Push(s,'b'); - Push(s,'c'); - Push(s,'d'); - Push(s,'e'); - printf(" (4)栈为%s\n",(StackEmpty(s)?"空":"非空")); - printf(" (5)出栈序列:"); - while (!StackEmpty(s)) - { - Pop(s,e); - printf("%c ",e); - } - printf("\n"); - printf(" (6)栈为%s\n",(StackEmpty(s)?"空":"非空")); - printf(" (7)释放栈\n"); - DestroyStack(s); - return 1; -} diff --git a/2224020082/exp3-3.cpp b/2224020082/exp3-3.cpp deleted file mode 100644 index 229d73cfec98f2b5956432c08311345177e7399f..0000000000000000000000000000000000000000 --- a/2224020082/exp3-3.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include"sqqueue.cpp" -int main() -{ ElemType e; - SqQueue * q; - peintf("环形队列基本运算如下:\n"); - printf(" (1)初始化队列q\n"); - InitQueue(q); - printf(" (2)依次进队列元素a,b,c\n";) - if (!enQueue(q,'a')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'b')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'c')) printf("\t提示:队满,不能进队\n"); - printf(" (3)队列为%s\n",(QueueEmpty(q)?"空":"非空")); - if (deQueue(q,e)==0) - printf("队空,不能出队\n"); - else - printf(" (4)出队一个元素%c\n",e); - printf(" (5)依次进队列元素d,e,f\n"); - if (!enQueue(q,'d')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'e')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'f')) printf("\t提示:队满,不能进队\n"); - printf(" (6)出队列序列:"); - while (!QueueEmpty(q)) - { deQueue(q,e); - printf("%c ",e); - } - printf("\n"); - printf(" (7)释放队列\n"); - DestroyQueue(q); - return 1; -} diff --git a/2224020082/exp3-8.cpp b/2224020082/exp3-8.cpp deleted file mode 100644 index 6ec35126a11384746b278787da44c964af10a7d9..0000000000000000000000000000000000000000 --- a/2224020082/exp3-8.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include -#define MaxSize 100 -typedef struct -{ int col[MaxSize]; - int top; -} StackType; -void dispasolution(StackType St) -{ static int count=0; - printf(" 第%d个解:",++count); - for (int i=1;i<=St.top;i++) - printf("(%d,%d) ",i,St.col[i]); - printf("\n"); -} -bool place(StackType St,int k,int j) -{ int i=1; - if (k==1) return true; - while (i<=k-1) - { if ((St.col[i]==j) || (abs(j-St.col[i])==abs(i-k))) - return false; - i++; - } - return true; -} -void queen(int n) -{ - int k; - bool find; - StackType St; - St.top=0; - St.top++; St.col[St.top]=0; - while (St.top!=0) - { k=St.top; - find=false; - for (int j=St.col[k]+1;j<=n;j++) - if (place(St,k,j)) - { St.col[St.top]=j; - find=true; - break; - } - if (find) - { if (k==n) - dispasolution(St); - else - { St.top++; - St.col[St.top]=0; - } - } - else - St.top--; - } -} -int main() -{ int n; - printf("皇后问题(n<20) n="); - scanf("%d",&n); - if (n>20) - printf("n值太大\n"); - else - { printf(" %d皇后问题求解如下:\n",n); - queen(n); - } - return 1; -} diff --git a/2224020082/exp7-5.cpp b/2224020082/exp7-5.cpp deleted file mode 100644 index 2f9dee47bc61195a89eb575cde3b76675538eb2e..0000000000000000000000000000000000000000 --- a/2224020082/exp7-5.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include -#include -#define N 50 -#define M 2*N-1 -typedef struct -{ - char data[5]; - int weight; - int parent; - int lchild; - int rchild; -} HTNode; -typedef struct -{ - char cd[N]; - int start; -} HCode; -void CreateHT(HTNode ht[],int n) -{ - int i,k,lnode,rnode; - int min1,min2; - for (i=0;i<2*n-1;i++) - ht[i].parent=ht[i].lchild=ht[i].rchild=-1; - for (i=n;i<2*n-1;i++) - { - min1=min2=32767; - lnode=rnode=-1; - for (k=0;k<=i-1;k++) - if (ht[k].parent==-1) - { - if (ht[k].weight -#include -#include -#include -using namespace std; -#define INF 0x3f3f3f3f -#define MAXV 105 -int mat[MAXV][MAXV]; -int U[MAXV]; -int lowcost[MAXV]; -int n; - -int Prim() -{ memset(U,0,sizeof(U)); - memset(lowcost,0x3f,sizeof(lowcost)); - int ans=0; - lowcost[1]=0; - for(int i=1;i<=n;i++) - { int minc=INF,k=0; - for(int j=1;j<=n;j++) 点k - if(!U[j] && lowcost[j]mat[k][i]) - lowcost[i]=mat[k][i]; - } - return ans; -} -//----并查集基本运算算法 -int parent[MAXV]; -int rnk[MAXV]; -void Init(int n) -{ for (int i=1;i<=n;i++) - { parent[i]=i; - rnk[i]=0; - } -} -int Find(int x) -{ if (x!=parent[x]) - parent[x]=Find(parent[x]); - return parent[x]; -} -void Union(int x,int y) -{ int rx=Find(x); - int ry=Find(y); - if (rx==ry) - return; - if (rnk[rx]u=u; - this->v=v; - this->w=w; - } - bool operator<(const Edge &s) const - { - return w E; - for (int i=1;i<=n;i++) - for (int j=1;j<=n;j++) - if (i=0;s--) - printf(",%d",apath[s]); - printf(" \t路径长度为:%d\n",A[i][j]); - } - } -} - -void Floyd(MatGraph g) -{ int A[MAXV][MAXV],path[MAXV][MAXV]; - int i,j,k; - for (i=0;iA[i][k]+A[k][j]) - { A[i][j]=A[i][k]+A[k][j]; - path[i][j]=path[k][j]; - } - } - Dispath(g,A,path); -} -int main() -{ - MatGraph g; - int A[MAXV][MAXV]={ - {0,5,INF,7,INF,INF}, - {INF,0,4,INF,INF,INF}, - {8,INF,0,INF,INF,9}, - {INF,INF,5,0,INF,6}, - {INF,INF,INF,5,0,INF}, - {3,INF,INF,INF,1,0}}; - int n=6, e=10; - CreateMat(g,A,n,e); - printf("有向图G的邻接矩阵:\n"); DispMat(g); - printf("弗洛伊德算法求解结果:\n"); - Floyd(g); - return 1; -} \ No newline at end of file diff --git "a/2224020084/\347\254\254\344\270\200\347\253\240/exp1-1.cpp" "b/2224020084/\347\254\254\344\270\200\347\253\240/exp1-1.cpp" deleted file mode 100644 index 72c4f280574689987f275ef6b850251e13811a0f..0000000000000000000000000000000000000000 --- "a/2224020084/\347\254\254\344\270\200\347\253\240/exp1-1.cpp" +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include -long add1(long n) -{ - long i,sum=0; - for (i=1;i<=n;i++) - sum+=i; - return sum; -} - -void AddTime1(long n) -{ - clock_t t; - long sum; - t=clock(); - sum=add1(n); - t=clock()-t; - printf("方法1:\n"); - printf(" 结果:1~%d之和:%ld\n",n,sum); - printf(" 用时:%lf秒\n" ,((float)t)/CLOCKS_PER_SEC); -} \ No newline at end of file diff --git "a/2224020084/\347\254\254\344\270\200\347\253\240/exp1-3.cpp" "b/2224020084/\347\254\254\344\270\200\347\253\240/exp1-3.cpp" deleted file mode 100644 index 2fbdad15aa3ef2de8d7a2cf0c66eef7023418026..0000000000000000000000000000000000000000 --- "a/2224020084/\347\254\254\344\270\200\347\253\240/exp1-3.cpp" +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include -bool prime1(long n) -{ - long i; - for (i=2;i -#include -using namespace std; -vector multiply(vector& poly1, vector& poly2) { - if(poly1.size() != poly2.size()){ - return {-1}; - } - int n = poly1.size(); - vector result(n, 0); - - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - result[i + j] += poly1[i] * poly2[j]; - } - } - - return result; -} -int main() { - vector poly1 = {2, 3, 4}, poly2 = {1, 5}; - vector result = multiply(poly1, poly2); - - if(result[0] != 0){ - for (int i = result.size() - 1; i >= 0; i--) { - cout << result[i]; - if (i > 0) cout << "x^" << i; - if (i == result.size() - 1) cout << endl; - } - }else{ - cout << "The result is zero." << endl; - } - - return 0; -}; diff --git "a/2224020084/\347\254\254\344\272\214\347\253\240/exp2-2.cpp" "b/2224020084/\347\254\254\344\272\214\347\253\240/exp2-2.cpp" deleted file mode 100644 index 83affc9884f0e2f50c402ef67f8b5e9929b9b629..0000000000000000000000000000000000000000 --- "a/2224020084/\347\254\254\344\272\214\347\253\240/exp2-2.cpp" +++ /dev/null @@ -1,33 +0,0 @@ -#include "linklist.cpp" -int main() -{ - LinkNode *h; - ElemType e; - printf("单链表的基本运算如下:\n"); - printf(" (1)初始化单链表h\n"); - InitList(h); - printf(" (2)依次采用尾插法插入a,b,c,d,e元素\n"); - ListInsert(h,1,'a'); - ListInsert(h,2,'b'); - ListInsert(h,3,'c'); - ListInsert(h,4,'d'); - ListInsert(h,5,'e'); - printf(" (3)输出单链表h:"); - DispList(h); - printf(" (4)单链表h长度:%d\n",ListLength(h)); - printf(" (5)单链表h为%s\n",(ListEmpty(h)?"空":"非空")); - GetElem(h,3,e); - printf(" (6)单链表h的第3个元素:%c\n",e); - printf(" (7)元素a的位置:%d\n",LocateElem(h,'a')); - printf(" (8)在第4个元素位置上插入f元素\n"); - ListInsert(h,4,'f'); - printf(" (9)输出单链表h:"); - DispList(h); - printf(" (10)删除h的第3个元素\n"); - ListDelete(h,3,e); - printf(" (11)输出单链表h:"); - DispList(h); - printf(" (12)释放单链表h\n"); - DestroyList(h); - return 1; -} diff --git "a/2224020084/\347\254\254\344\272\214\347\253\240/exp2-6.cpp" "b/2224020084/\347\254\254\344\272\214\347\253\240/exp2-6.cpp" deleted file mode 100644 index 83f8540b3187e107de5f3f855adaa5f5e595f39a..0000000000000000000000000000000000000000 --- "a/2224020084/\347\254\254\344\272\214\347\253\240/exp2-6.cpp" +++ /dev/null @@ -1,72 +0,0 @@ -#include "linklist.cpp" -void Split1(LinkNode *&L) -{ - LinkNode *pre,*p,*q; - if(L->next==NULL || L->next->next==NULL) - return; - int x=L->next->data; - pre=L->next; - p=pre->next; - while(p!=NULL) - { - if(p->datanext=p->next; - p->next=L->next; - L->next=p; - p=pre->next; - } - else - { - pre=p; - p=pre->next; - } - } -} -void Split2(LinkNode *&L) - LinkNode *p=L->next,*r,*L1,*r1; - if(L->next==NULL || L->next->next==NULL) - return; - int x=L->next->data; - r=L; - L1=(LinkNode*)malloc(sizeof(LinkNode)); - r1=L1; - while (p!=NULL) - { - if (p->datanext=p; r=p; - p=p->next; - } - else - { - r1->next=p; r1=p; - p=p->next; - } - } - r1->next=NULL; - r->next=L1->next; - free(L1); -} - -int main() -{ - LinkNode *L; - ElemType a[]="daxgdchaeb"; - int n=strlen(a); - printf("解法1\n"); - CreateListR(L,a,n); - printf(" L: "); DispList(L); - printf(" 以首结点值进行划分\n"); - Split1(L); - printf(" L: "); DispList(L); - DestroyList(L); - printf("解法2\n"); - CreateListR(L,a,n); - printf(" L: "); DispList(L); - printf(" 以首结点值进行划分\n"); - Split2(L); - printf(" L: "); DispList(L); - DestroyList(L); - return 1; -} diff --git "a/2224020085/chapter5/Hanoi\351\227\256\351\242\230.cpp" "b/2224020085/chapter5/Hanoi\351\227\256\351\242\230.cpp" deleted file mode 100644 index 54f7ab4dd0cae768b98310b82ccfba4db75cc26f..0000000000000000000000000000000000000000 --- "a/2224020085/chapter5/Hanoi\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,21 +0,0 @@ -#include -//递归算法 -void hanoi1(int n,char a,char b,char c) -{ - if(n==1) - printf("将第%d个盘从%c移到%c\n",n,a,c); - else - { - hanoi1(n-1,a,c,b); - printf("将第%d个盘从%c移到%c\n",n,a,c); - hanoi1(n-1,b,a,c); - } -} -int main() -{ - char x='a'; - char y='b'; - char z='c'; - int n=3; - hanoi1(n,x,y,z); -} \ No newline at end of file diff --git "a/2224020085/chapter5/n\347\232\207\345\220\216.cpp" "b/2224020085/chapter5/n\347\232\207\345\220\216.cpp" deleted file mode 100644 index f7631fad00ccc4af08a6dccf9ae835d150e0c427..0000000000000000000000000000000000000000 --- "a/2224020085/chapter5/n\347\232\207\345\220\216.cpp" +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -const int N=20; -int q[N]; -int count=0; -void print(int n) -{ - count++; - int i; - printf("第%d个解:",count); - for(i=1;i<=n;i++) - printf("(%d,%d)",i,q[i]); - printf("\n"); -} -bool place(int k,int j) -{ - int i=1; - while(in) - print(n); - else - for(j=1;j<=n;j++) - if(place(k,j)) - { - q[k]=j; - queen(k+1,n); - } -} -int main() -{ - int n; - printf("皇后问题(n<20)n:"); - scanf("%d",&n); - if(n>20) - printf("不能求解"); - else - { - printf("%d皇后问题求解如下:\n",n); - queen(1,n); - printf("\n"); - } - return 1; -} \ No newline at end of file diff --git "a/2224020085/chapter5/\346\201\242\345\244\215IP\345\234\260\345\235\200.cpp" "b/2224020085/chapter5/\346\201\242\345\244\215IP\345\234\260\345\235\200.cpp" deleted file mode 100644 index f55ff718dfcafab00ae7b555f54238a48a16b441..0000000000000000000000000000000000000000 --- "a/2224020085/chapter5/\346\201\242\345\244\215IP\345\234\260\345\235\200.cpp" +++ /dev/null @@ -1,49 +0,0 @@ -#include -#define MaxSize 100 -typedef struct{ - char data[MaxSize]; - int length; -}IP; -void addch(IP &ip,char ch) -{ - ip.data[ip.length]=ch; - ip.length++; -} -IP addot(IP ip) -{ - addch(ip,'.'); - return ip; -} -void solveip(char s[],int n,int start, int step,IP ip) -{ - if(start<=n) - { - if(start==n && step==4) - { - for(int i=0;i -#define N<稀疏矩阵的列数> -#define MaxSize<稀疏矩阵中非零元素的最多个数> -typedef struct -{ - int r; - int c; - ElemType d; -}TuoNode; -typedef struct -{ - int rows; - int cols; - int nums; - TupNode data[MaxSize]; -}TSMatrix; -//创建 -void CreateMat(TSMatrix &t,ElemType A[M][N]) -{ - int i,j; - t.rows=M; - t.cols=N; - t.nums=0; - for(i=0;i=t.rows || j>=t.cols) - return false; - while(kt.data[k].r) k++; - while(kt.data[k].c) k++; - if(t.data[k].r==i && t.data[k].c==j) - t.data[k].d=x; - else - { - for(k1=t.nums-1;k1>=k;k1--) - { - t.data[k1+1].r=t.data[k1].r; - t.data[k1+1].c=t.data[k1].c; - t.data[k1+1].d=t.data[k1].d; - } - t.data[k].r=i; - t.data[k].c=j; - t.data[k].d=x; - t.nums++; - } - retuen true; -} \ No newline at end of file diff --git "a/2224020085/chapter7/\344\272\214\345\217\211\346\240\221\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2224020085/chapter7/\344\272\214\345\217\211\346\240\221\345\237\272\346\234\254\350\277\220\347\256\227.cpp" deleted file mode 100644 index 0471b820fbf0517c86aedca76e770fb325b8534a..0000000000000000000000000000000000000000 --- "a/2224020085/chapter7/\344\272\214\345\217\211\346\240\221\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,138 +0,0 @@ -#include -#include -#define MaxSize 100 -typedef char ElemType; //将ElemType类型设成char型 -//二叉树的声明 -typedef struct TuoNode{ - ElemType data; - struct node *lchild; //左孩子结点 - struct node *rchild; //右孩子结点 -}BTNode; -//创建二叉树 -void CreateBTree(BTNode *&b,char *str) -{ - BTNode *St[MaxSize],*p; - int top=-1,k,j=0; - char ch; - b=NULL; - ch=str[j]; - while(ch!='\0') - { - switch(ch) - { - case '(':top++;St[top]=p;k=1;break; //处理左子树 - case ')':top--;break; //子树处理完毕 - case ',':k=2;break; //处理右子树 - default:p=(BTNode *)malloc(sizeof(BTNode)); - p->data=ch;p->lchild=p->rchild=NULL; - if(b==NULL) - b=p; //将p设为二叉树的根节点 - else - { - switch(k) - { - case 1:St[top]->lchild=p;break; - case 2:St[top]->rchild=p;break; - } - } - } - j++;ch=str[j]; - } -} -//销毁二叉树 采用递归的思想 -void DestroyBTree(BTNode *&b) -{ - if(b!=NULL) - { - DestroyBTree(b->lchild); - DestroyBTree(b->rchild); - free(b); - } -} -//查找特定值的结点 -BTNode *FindNode(BTNode *b,ElemType x) -{ - BTNode *p; - if(b==NULL) //判断是否为空 - return NULL; - else if(b->data==x) - return b; - else - { - p=FindNode(b->lchild,x) //采用递归 - if(p!=NULL) - return p; - else - return FindNode(b->rchild,x); - } -} -//返回指定结点的左孩子指针 -BTNode* LchildNode(BTNode *p) -{ - return p->lchild; -} -//返回指定结点的右孩子指针 -BTNode* RchildNode(BTNode *p) -{ - return p->rchild; -} -//求二叉树高度 -int BTHeight(BTNode *b) -{ - int lchildh,rchildh; - if(b==NULL) - return(0); - else - { - lchildh=BTHeight(b->lchild); //采用递归 - rchildh=BTHeight(b->rchild); - return(lchildh>rchildh)?(lchildh+1):(rchildh+1); //加1=加一个根节点 - } -} -//用括号表示法输出二叉树 -void DispBTree(BTNode *b) -{ - if(b!=NULL) - { - printf("%c",b->data); - if(b->lchild!=NULL||b->rchild!=NULL) - { - printf("("); - DispBTree(b->lchild); - if(b->rchild!=NULL) - printf(","); - DispBTree(b->rchild); - printf(")"); - } - } -} -int main() -{ - BTNode *b,*p,*lp,*rp; - printf("二叉树基本运算:\n"); - printf("1.创建二叉树\n"); - CreateBTree(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I))"); - printf("2.输出二叉树:"); - DispBTree(b); - printf("\n"); - printf("3.H结点:"); - p=FindNode(b,'H'); - if(p!=NULL) - { - lp=LchildNode(p); - if(lp!=NULL) - printf("左孩子为%c",lp->data); - else - printf("无左孩子"); - rp=RchildNode(p); - if(rp!=NULL) - printf("右孩子为%c",rp->data); - else - printf("无右孩子"); - } - printf("\n"); - printf("4.二叉树的高度:%d\n",BTHeight(b)); - printf("释放二叉树b\n"); - DestroyBTree(b); - return 1; -} \ No newline at end of file diff --git "a/2224020086/chapter1/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2224020086/chapter1/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index 8f86c9c58a126924d91b30a0e01fa07446a4141b..0000000000000000000000000000000000000000 --- "a/2224020086/chapter1/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,84 +0,0 @@ -#include -struct ListNode { - int val; - ListNode* next; - ListNode(int value) : val(value), next(nullptr) {} -}; -class LinkedList { -private: - ListNode* head; - -public: - LinkedList() : head(nullptr) {} - void insert(int value) { - ListNode* newNode = new ListNode(value); - - if (head == nullptr) { - head = newNode; - } else { - ListNode* curr = head; - while (curr->next != nullptr) { - curr = curr->next; - } - curr->next = newNode; - } - } - void remove(int value) { - ListNode* curr = head; - ListNode* prev = nullptr; - - while (curr != nullptr) { - if (curr->val == value) { - if (prev == nullptr) { - head = curr->next; - } else { - prev->next = curr->next; - } - delete curr; - return; - } - prev = curr; - curr = curr->next; - } - } - ListNode* search(int value) { - ListNode* curr = head; - - while (curr != nullptr) { - if (curr->val == value) { - return curr; - } - curr = curr->next; - } - - return nullptr; - } - void printList() { - ListNode* curr = head; - - while (curr != nullptr) { - std::cout << curr->val << " -> "; - curr = curr->next; - } - std::cout << "nullptr" << std::endl; - } -}; -int main() { - LinkedList list; - list.insert(1); - list.insert(2); - list.insert(3); - list.insert(4); - std::cout << "Initial List: "; - list.printList(); - list.remove(3); - std::cout << "List after removal: "; - list.printList(); - ListNode* node = list.search(2); - if (node != nullptr) { - std::cout << "Node found: " << node->val << std::endl; - } else { - std::cout << "Node not found." << std::endl; - } - return 0; -} \ No newline at end of file diff --git "a/2224020086/chapter1/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" "b/2224020086/chapter1/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" deleted file mode 100644 index 95da03d364ae251ea4b4d1b4e728984d161d0de8..0000000000000000000000000000000000000000 --- "a/2224020086/chapter1/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" +++ /dev/null @@ -1,74 +0,0 @@ -#include -struct ListNode { - int val; - ListNode* next; - ListNode(int value) : val(value), next(nullptr) {} -}; -ListNode* partition(ListNode* head, int pivot) { - ListNode* smallerHead = nullptr; - ListNode* smallerTail = nullptr; - ListNode* greaterHead = nullptr; - ListNode* greaterTail = nullptr; - ListNode* curr = head; - while (curr != nullptr) { - ListNode* nextNode = curr->next; - curr->next = nullptr; - if (curr->val < pivot) { - if (smallerHead == nullptr) { - smallerHead = curr; - smallerTail = curr; - } else { - smallerTail->next = curr; - smallerTail = curr; - } - } else { - if (greaterHead == nullptr) { - greaterHead = curr; - greaterTail = curr; - } else { - greaterTail->next = curr; - greaterTail = curr; - } - } - curr = nextNode; - } - if (smallerHead == nullptr) { - return greaterHead; - } - smallerTail->next = greaterHead; - return smallerHead; -} -void printList(ListNode* head) { - ListNode* curr = head; - while (curr != nullptr) { - std::cout << curr->val << " -> "; - curr = curr->next; - } - std::cout << "nullptr" << std::endl; -} -int main() { - ListNode* head = new ListNode(3); - ListNode* node1 = new ListNode(5); - ListNode* node2 = new ListNode(8); - ListNode* node3 = new ListNode(2); - ListNode* node4 = new ListNode(4); - ListNode* node5 = new ListNode(1); - head->next = node1; - node1->next = node2; - node2->next = node3; - node3->next = node4; - node4->next = node5; - std::cout << "Initial List: "; - printList(head); - int pivot = 4; - ListNode* newHead = partition(head, pivot); - std::cout << "Partitioned List: "; - printList(newHead); - ListNode* curr = newHead; - while (curr != nullptr) { - ListNode* nextNode = curr->next; - delete curr; - curr = nextNode; - } - return 0; -} \ No newline at end of file diff --git a/2224020087/chapter7/.keep b/2224020087/chapter6/experiment/.keep similarity index 100% rename from 2224020087/chapter7/.keep rename to 2224020087/chapter6/experiment/.keep diff --git "a/2224020087/chapter6/experiment/\345\256\236\351\252\2146.1.cpp" "b/2224020087/chapter6/experiment/\345\256\236\351\252\2146.1.cpp" deleted file mode 100644 index ca7a86dd0dbc3cb5d96dd9b5f8577b07a31a5ea4..0000000000000000000000000000000000000000 --- "a/2224020087/chapter6/experiment/\345\256\236\351\252\2146.1.cpp" +++ /dev/null @@ -1,181 +0,0 @@ -#include -#define N 4 -typedef int elemtype; -#define maxsize 100 -typedef struct{ - int r; - int c; - elemtype d; -}tupnode; - -typedef struct{ - int rows; - int cols; - int nums; - tupnode data[maxsize]; -}tsmatrix; - -void creatmat(tsmatrix &t,elemtype A[N][N]){ - int i,j; - t.rows=N;t.cols=N;t.nums=0; - for(i=0;ib.data[j].c){ - c.data[k].r=b.data[i].r; - c.data[k].c=b.data[i].c; - c.data[k].d=b.data[i].d; - k++;j++; - } - else - {v=a.data[i].d+b.data[j].d; - if(v!=0){ - c.data[k].r=a.data[i].r; - c.data[k].c=a.data[i].c; - c.data[k].d=v; - k++; - } - i++;j++; - } - } - else if(a.data[i].r -#define maxlen 10 -void fun(int a[maxlen][maxlen],int n){ - int i,j,k=0,m; - if(n%2==0) m=n/2; - else m=n/2+1; - for(i=0;i=i;j--) - {k++; - a[n-i-1][j]=k; - } - for(j=n-i-2;j>=i+1;j--){ - k++; - a[j][i]=k; - } - } -} - -int main(){ - int i,j,n; - int a[maxlen][maxlen]; - printf("n(n<10):"); - scanf("%d",&n); - fun(a,n); - printf("%dַ:\n",n); - for(i=0;i -#define N 4 -#define M 10 -int value(int a[],int i,int j){ - if(i>=j) - return a[(i*(i-1))/2+j]; - else - return a[(j*(j-1))/2+i]; -} - -void madd(int a[],int b[],int c[][N]) -{ int i,j; - for(i=0;i>& mat) { - int sum=0; - int i,j; - int n=mat.size(); - - for(i=0;i> matrixReshape(vector>& mat, int r, int c) { - vector> ans(r, vector(c)); - int m=mat.size(); - int n=mat[0].size(); - if (m*n!=r*c) return mat; - for(int i=0;idata); - else printf(""); - rp=rchildnode(p); - if(rp!=NULL)printf("ҺΪ%c",rp->data); - else printf("Һ"); - } - printf("\n"); - printf("4bĸ߶ȣ%d\n",btheight(b)); - printf("5ͷŶb\n"); - destroybtree(b); - return 1; -} diff --git a/2224020087/chapter7/experiment7.5.cpp b/2224020087/chapter7/experiment7.5.cpp deleted file mode 100644 index 2c7fc166ddb11985f201208e26466f72784335df..0000000000000000000000000000000000000000 --- a/2224020087/chapter7/experiment7.5.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include -#include -#define N 50 -#define M 2*N-1 -typedef struct{ - char data[5]; - int weight; - int parent; - int lchild; - int rchild; -}htnode; - -typedef struct{ - char cd[N]; - int start; -}hcode; - -void createht(htnode ht[],int n){ - int i,k,lnode,rnode; - int min1,min2; - for(i=0;i<2*n-1;i++) - ht[i].parent=ht[i].lchild=ht[i].rchild=-1; - for(i=0;i<2*n-1;i++){ - min1=min2=32767; - lnode=rnode=-1; - for(k=0;k<=i-1;k++) - if(ht[k].parent==-1) - {if(ht[k].weight - -int main() { - int num1, num2, sum; - - std::cout << "Enter first number: "; - std::cin >> num1; - - std::cout << "Enter second number: "; - std::cin >> num2; - - sum = num1 + num2; - - std::cout << "Sum of the two numbers is: " << sum << std::endl; - - return 0; -} diff --git a/2224020090/1-4.cpp b/2224020090/1-4.cpp deleted file mode 100644 index a6d15735361ddca55506feb0cb2786fae8b9a1fd..0000000000000000000000000000000000000000 --- a/2224020090/1-4.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include - -int main() { - std::vector nums = {1, 2, 3, 4, 5}; - int sum = 0; - - for (int i = 0; i < nums.size(); i++) { - for (int j = i; j < nums.size(); j += 2) { - for (int k = i; k <= j; k++) { - sum += nums[k]; - } - } - } - - std::cout << "Sum of all odd length subarrays: " << sum << std::endl; - - return 0; -} diff --git a/2224020090/2-10.cpp b/2224020090/2-10.cpp deleted file mode 100644 index 78a29153199ef2765a3290f57156660e03bdad89..0000000000000000000000000000000000000000 --- a/2224020090/2-10.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include - -struct ListNode { - int val; - ListNode* next; - ListNode(int x) : val(x), next(nullptr) {} -}; - -void deleteNode(ListNode* node) { - ListNode* temp = node->next; - node->val = temp->val; - node->next = temp->next; - delete temp; -} - -void printList(ListNode* head) { - ListNode* current = head; - while (current != nullptr) { - std::cout << current->val << " "; - current = current->next; - } - std::cout << std::endl; -} - -int main() { - ListNode* head = new ListNode(1); - ListNode* node2 = new ListNode(2); - ListNode* node3 = new ListNode(3); - ListNode* node4 = new ListNode(4); - - head->next = node2; - node2->next = node3; - node3->next = node4; - - std::cout << "Original list: "; - printList(head); - - deleteNode(node3); - - std::cout << "Updated list after deleting node with value 3: "; - printList(head); - - return 0; -} \ No newline at end of file diff --git a/2224020090/2-2.cpp b/2224020090/2-2.cpp deleted file mode 100644 index 05b63fb696806c4ee97ca0fe9029d8b35a4eb08c..0000000000000000000000000000000000000000 --- a/2224020090/2-2.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include - -int removeElement(std::vector& nums, int val) { - int i = 0; - for (int j = 0; j < nums.size(); j++) { - if (nums[j] != val) { - nums[i] = nums[j]; - i++; - } - } - return i; -} - -int main() { - std::vector nums = {3, 2, 2, 3}; - int val = 3; - - int length = removeElement(nums, val); - - std::cout << "New length of the array: " << length << std::endl; - std::cout << "Updated array: "; - for (int i = 0; i < length; i++) { - std::cout << nums[i] << " "; - } - - return 0; -} diff --git a/2224020090/2-6.cpp b/2224020090/2-6.cpp deleted file mode 100644 index a42bb9a0f7bfcb674e65058c3fe68eaef4cd3618..0000000000000000000000000000000000000000 --- a/2224020090/2-6.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include - -double findMedianSortedArrays(std::vector& nums1, std::vector& nums2) { - int m = nums1.size(); - int n = nums2.size(); - int total = m + n; - int i = 0, j = 0; - int prev = 0, curr = 0; - - for (int k = 0; k <= total / 2; k++) { - prev = curr; - if (i < m && (j >= n || nums1[i] < nums2[j])) { - curr = nums1[i]; - i++; - } else { - curr = nums2[j]; - j++; - } - } - - if (total % 2 == 0) { - return (prev + curr) / 2.0; - } else { - return curr; - } -} - -int main() { - std::vector nums1 = {1, 3}; - std::vector nums2 = {2, 4}; - - double median = findMedianSortedArrays(nums1, nums2); - - std::cout << "Median of the two sorted arrays: " << median << std::endl; - - return 0; -} \ No newline at end of file diff --git a/2224020090/4-1.cpp b/2224020090/4-1.cpp deleted file mode 100644 index ef27899c8332e5dbd508339ca53a0ff71dda6019..0000000000000000000000000000000000000000 --- a/2224020090/4-1.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include - -using namespace std; - -bool isPalindrome(string s) { - int left = 0; - int right = s.length() - 1; - - while (left < right) { - while (left < right && !isalnum(s[left])) { - left++; - } - while (left < right && !isalnum(s[right])) { - right--; - } - - if (tolower(s[left]) != tolower(s[right])) { - return false; - } - - left++; - right--; - } - - return true; -} - -int main() { - string s1 = "A man, a plan, a canal: Panama"; - string s2 = "race a car"; - string s3 = "Was it a car or a cat I saw?"; - string s4 = "No 'x' in Nixon"; - - cout << s1 << " is " << (isPalindrome(s1) ? "a palindrome" : "not a palindrome") << endl; - cout << s2 << " is " << (isPalindrome(s2) ? "a palindrome" : "not a palindrome") << endl; - cout << s3 << " is " << (isPalindrome(s3) ? "a palindrome" : "not a palindrome") << endl; - cout << s4 << " is " << (isPalindrome(s4) ? "a palindrome" : "not a palindrome") << endl; - - return 0; -} diff --git a/2224020090/4-2.cpp b/2224020090/4-2.cpp deleted file mode 100644 index bbd0167e94f76fa8c8cd37ea83f8379ca5dc64ae..0000000000000000000000000000000000000000 --- a/2224020090/4-2.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include - -using namespace std; - -string longestCommonPrefix(vector& strs) { - if (strs.empty()) { - return ""; - } - - string prefix = strs[0]; - - for (int i = 1; i < strs.size(); i++) { - while (strs[i].find(prefix) != 0) { - prefix = prefix.substr(0, prefix.length() - 1); - - if (prefix.empty()) { - return ""; - } - } - } - - return prefix; -} - -int main() { - vector strs1 = {"flower", "flow", "flight"}; - vector strs2 = {"dog", "racecar", "car"}; - vector strs3 = {"apple", "banana", "cherry"}; - - cout << "The longest common prefix for strs1 is: " << longestCommonPrefix(strs1) << endl; - cout << "The longest common prefix for strs2 is: " << longestCommonPrefix(strs2) << endl; - cout << "The longest common prefix for strs3 is: " << longestCommonPrefix(strs3) << endl; - - return 0; -} \ No newline at end of file diff --git a/2224020090/4-4.cpp b/2224020090/4-4.cpp deleted file mode 100644 index 304a8bb898e5d381e60f2f4c607e2c7091326158..0000000000000000000000000000000000000000 --- a/2224020090/4-4.cpp +++ /dev/null @@ -1,17 +0,0 @@ -if (n == 0) { - return 0; -} - -for (int i = 0; i <= m - n; i++) { - int j; - for (j = 0; j < n; j++) { - if (haystack[i + j] != needle[j]) { - break; - } - } - if (j == n) { - return i; - } -} - -return -1; diff --git a/2224020090/4-5.cpp b/2224020090/4-5.cpp deleted file mode 100644 index 588514d1959208aed9047fcdab4b8d4ed4e20271..0000000000000000000000000000000000000000 --- a/2224020090/4-5.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include -#include - -using namespace std; - -bool repeatedSubstringPattern(string s) { - int n = s.length(); - - for (int i = 1; i <= n / 2; i++) { - if (n % i == 0) { - string sub = s.substr(0, i); - string pattern = ""; - - for (int j = 0; j < n / i; j++) { - pattern += sub; - } - - if (pattern == s) { - return true; - } - } - } - - return false; -} - -int main() { - string s1 = "abcabc"; - string s2 = "abcdabcd"; - string s3 = "aaaaa"; - - cout << s1 << " has repeated substring: " << (repeatedSubstringPattern(s1) ? "true" : "false") << endl; - cout << s2 << " has repeated substring: " << (repeatedSubstringPattern(s2) ? "true" : "false") << endl; - cout << s3 << " has repeated substring: " << (repeatedSubstringPattern(s3) ? "true" : "false") << endl; - - return 0; -} - - - return true; -} - -int main() { - string s1 = "A man, a plan, a canal: Panama"; - string s2 = "race a car"; - string s3 = "Was it a car or a cat I saw?"; - string s4 = "No 'x' in Nixon"; - - cout << s1 << " is " << (isPalindrome(s1) ? "a palindrome" : "not a palindrome") << endl; - cout << s2 << " is " << (isPalindrome(s2) ? "a palindrome" : "not a palindrome") << endl; - cout << s3 << " is " << (isPalindrome(s3) ? "a palindrome" : "not a palindrome") << endl; - cout << s4 << " is " << (isPalindrome(s4) ? "a palindrome" : "not a palindrome") << endl; - - return 0; -} diff --git a/2224020090/5-1.cpp b/2224020090/5-1.cpp deleted file mode 100644 index a00f52cae3675e789ae93f36013986bd0d6867d1..0000000000000000000000000000000000000000 --- a/2224020090/5-1.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include - -int fibonacci(int n) { - if (n <= 1) { - return n; - } - int a = 0, b = 1, c; - for (int i = 2; i <= n; i++) { - c = a + b; - a = b; - b = c; - } - return b; -} - -int main() { - int n = 10; - std::cout << "The " << n << "th Fibonacci number is: " << fibonacci(n) << std::endl; - return 0; -} diff --git a/2224020090/5-4.cpp b/2224020090/5-4.cpp deleted file mode 100644 index 179c266774f43fbfa5de141d064443241a88ad24..0000000000000000000000000000000000000000 --- a/2224020090/5-4.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include - -struct ListNode { - int val; - ListNode* next; - ListNode(int x) : val(x), next(nullptr) {} -}; - -bool isPalindrome(ListNode* head) { - if (head == nullptr || head->next == nullptr) { - return true; - } - - ListNode* slow = head; - ListNode* fast = head; - while (fast->next != nullptr && fast->next->next != nullptr) { - slow = slow->next; - fast = fast->next->next; - } - ListNode* prev = nullptr; - ListNode* curr = slow->next; - while (curr != nullptr) { - ListNode* next = curr->next; - curr->next = prev; - prev = curr; - curr = next; - } - - ListNode* p1 = head; - ListNode* p2 = prev; - while (p2 != nullptr) { - if (p1->val != p2->val) { - return false; - } - p1 = p1->next; - p2 = p2->next; - } - - prev = nullptr; - curr = slow->next; - while (curr != nullptr) { - ListNode* next = curr->next; - curr->next = prev; - prev = curr; - curr = next; - } - slow->next = prev; - - return true; -} - -int main() { - ListNode* head = new ListNode(1); - head->next = new ListNode(2); - head->next->next = new ListNode(3); - head->next->next->next = new ListNode(2); - head->next->next->next->next = new ListNode(1); - - std::cout << "Is the linked list palindrome? " << std::boolalpha << isPalindrome(head) << std::endl; - - return 0; -} diff --git a/2224020090/6-1.cpp b/2224020090/6-1.cpp deleted file mode 100644 index 6bd9418e1d2ef40f22e75179b4a270ad328a91a0..0000000000000000000000000000000000000000 --- a/2224020090/6-1.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -int findMaxConsecutiveOnes(std::vector& nums) { - int maxCount = 0; - int count = 0; - for (int num : nums) { - if (num == 1) { - count++; - maxCount = std::max(maxCount, count); - } else { - count = 0; - } - } - return maxCount; -} - -int main() { - std::vector nums = {1, 1, 0, 1, 1, 1}; - int maxConsecutiveOnes = findMaxConsecutiveOnes(nums); - std::cout << "The maximum number of consecutive ones is: " << maxConsecutiveOnes << std::endl; - return 0; -} diff --git a/2224020090/6-3.cpp b/2224020090/6-3.cpp deleted file mode 100644 index 44f46fdebe76423228cd7b12292ed0236169abdf..0000000000000000000000000000000000000000 --- a/2224020090/6-3.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include - -void moveZeroes(std::vector& nums) { - int nonZeroIndex = 0; - for (int num : nums) { - if (num != 0) { - nums[nonZeroIndex++] = num; - } - } - while (nonZeroIndex < nums.size()) { - nums[nonZeroIndex++] = 0; - } -} - -int main() { - std::vector nums = {0, 1, 0, 3, 12}; - moveZeroes(nums); - std::cout << "After moving zeroes, the array is: "; - for (int num : nums) { - std::cout << num << " "; - } - std::cout << std::endl; - return 0; -} \ No newline at end of file diff --git a/2224020090/6-5.cpp b/2224020090/6-5.cpp deleted file mode 100644 index d0112c5c7c7e702225dc6664f9905f2694d1639c..0000000000000000000000000000000000000000 --- a/2224020090/6-5.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include - -int diagonalSum(std::vector>& mat) { - int sum = 0; - int n = mat.size(); - for (int i = 0; i < n; i++) { - sum += mat[i][i]; - sum += mat[i][n - 1 - i]; - } - if (n % 2 == 1) { - sum -= mat[n / 2][n / 2]; - } - return sum; -} - -int main() { - std::vector> mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; - int sum = diagonalSum(mat); - std::cout << "The sum of the diagonal elements is: " << sum << std::endl; - return 0; -} diff --git a/2224020090/6-6.cpp b/2224020090/6-6.cpp deleted file mode 100644 index efee03c77745bd04135c00feb16ed1a0e8cc9fa1..0000000000000000000000000000000000000000 --- a/2224020090/6-6.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include - -std::vector> matrixReshape(std::vector>& mat, int r, int c) { - int m = mat.size(); - int n = mat[0].size(); - if (m * n != r * c) { - return mat; - } - std::vector> reshaped(r, std::vector(c, 0)); - for (int i = 0; i < m * n; i++) { - reshaped[i / c][i % c] = mat[i / n][i % n]; - } - return reshaped; -} - -int main() { - std::vector> mat = {{1, 2}, {3, 4}}; - int r = 1; - int c = 4; - std::vector> reshaped = matrixReshape(mat, r, c); - std::cout << "The reshaped matrix is: " << std::endl; - for (const auto& row : reshaped) { - for (int num : row) { - std::cout << num << " "; - } - std::cout << std::endl; - } - return 0; -} diff --git a/2224020090/7-11.cpp b/2224020090/7-11.cpp deleted file mode 100644 index 179c266774f43fbfa5de141d064443241a88ad24..0000000000000000000000000000000000000000 --- a/2224020090/7-11.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include - -struct ListNode { - int val; - ListNode* next; - ListNode(int x) : val(x), next(nullptr) {} -}; - -bool isPalindrome(ListNode* head) { - if (head == nullptr || head->next == nullptr) { - return true; - } - - ListNode* slow = head; - ListNode* fast = head; - while (fast->next != nullptr && fast->next->next != nullptr) { - slow = slow->next; - fast = fast->next->next; - } - ListNode* prev = nullptr; - ListNode* curr = slow->next; - while (curr != nullptr) { - ListNode* next = curr->next; - curr->next = prev; - prev = curr; - curr = next; - } - - ListNode* p1 = head; - ListNode* p2 = prev; - while (p2 != nullptr) { - if (p1->val != p2->val) { - return false; - } - p1 = p1->next; - p2 = p2->next; - } - - prev = nullptr; - curr = slow->next; - while (curr != nullptr) { - ListNode* next = curr->next; - curr->next = prev; - prev = curr; - curr = next; - } - slow->next = prev; - - return true; -} - -int main() { - ListNode* head = new ListNode(1); - head->next = new ListNode(2); - head->next->next = new ListNode(3); - head->next->next->next = new ListNode(2); - head->next->next->next->next = new ListNode(1); - - std::cout << "Is the linked list palindrome? " << std::boolalpha << isPalindrome(head) << std::endl; - - return 0; -} diff --git a/2224020090/7-14.cpp b/2224020090/7-14.cpp deleted file mode 100644 index 380b4a1724ff80ae5ff60b2d411f09b661bd99f1..0000000000000000000000000000000000000000 --- a/2224020090/7-14.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include -using namespace std; -struct TreeNode { - int val; - TreeNode* left; - TreeNode* right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; -vector largestValues(TreeNode* root) { - vector result; - if (root == NULL) { - return result; - } - - queue q; - q.push(root); - while (!q.empty()) { - int size = q.size(); - int maxVal = INT_MIN; - for (int i = 0; i < size; i++) { - TreeNode* node = q.front(); - q.pop(); - maxVal = max(maxVal, node->val); - if (node->left != NULL) { - q.push(node->left); - } - if (node->right != NULL) { - q.push(node->right); - } - } - result.push_back(maxVal); - } - return result; -} - -int main() - TreeNode* root = new TreeNode(1); - root->left = new TreeNode(3); - root->right = new TreeNode(2); - root->left->left = new TreeNode(5); - root->left->right = new TreeNode(3); - root->right->right = new TreeNode(9); - vector maxValues = largestValues(root); - cout << "每个树行中的最大值为:"; - for (int val : maxValues) { - cout << val << " "; - } - cout << endl; - - return 0; -} diff --git a/2224020090/7-17.cpp b/2224020090/7-17.cpp deleted file mode 100644 index 6aff4f441819e53159f06d56d87eed2639a85556..0000000000000000000000000000000000000000 --- a/2224020090/7-17.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include -#include -#include -using namespace std; -struct TreeNode { - int val; - TreeNode* left; - TreeNode* right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; -int widthOfBinaryTree(TreeNode* root) { - if (root == NULL) { - return 0; - } - queue> q; - q.push({root, 1}); - int maxWidth = 0; - while (!q.empty()) { - int size = q.size(); - unsigned long long leftmost = q.front().second; - unsigned long long rightmost = leftmost; - for (int i = 0; i < size; i++) { - TreeNode* node = q.front().first; - unsigned long long index = q.front().second; - q.pop(); - rightmost = index; - if (node->left != NULL) { - q.push({node->left, 2 * index}); - } - if (node->right != NULL) { - q.push({node->right, 2 * index + 1}); - } - } - maxWidth = max(maxWidth, static_cast(rightmost - leftmost + 1)); - } - return maxWidth; -} - -int main() { - TreeNode* root = new TreeNode(1); - root->left = new TreeNode(3); - root->right = new TreeNode(2); - root->left->left = new TreeNode(5); - root->left->right = new TreeNode(3); - root->right->right = new TreeNode(9); - int maxWidth = widthOfBinaryTree(root); - cout << "二叉树的最大宽度为:" << maxWidth << endl; - - return 0; -} diff --git a/2224020090/7-18.cpp b/2224020090/7-18.cpp deleted file mode 100644 index fc7e009286a6ffa309832aa79f96da5234591422..0000000000000000000000000000000000000000 --- a/2224020090/7-18.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include -using namespace std; -struct TreeNode { - int val; - TreeNode* left; - TreeNode* right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; -bool hasPathSum(TreeNode* root, int sum) { - if (root == NULL) { - return false; - } - if (root->left == NULL && root->right == NULL && root->val == sum) { - return true; - } - return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); -} - -int main() { - TreeNode* root = new TreeNode(5); - root->left = new TreeNode(4); - root->right = new TreeNode(8); - root->left->left = new TreeNode(11); - root->left->left->left = new TreeNode(7); - root->left->left->right = new TreeNode(2); - root->right->left = new TreeNode(13); - root->right->right = new TreeNode(4); - root->right->right->right = new TreeNode(1); - int targetSum = 22; - bool hasPath = hasPathSum(root, targetSum); - if (hasPath) { - cout << "二叉树存在从根到叶子节点的路径和等于" << targetSum << "的路径。" << endl; - } else { - cout << "二叉树不存在从根到叶子节点的路径和等于" << targetSum << "的路径。" << endl; - } - - return 0; -} diff --git a/2224020090/7-3.cpp b/2224020090/7-3.cpp deleted file mode 100644 index efee03c77745bd04135c00feb16ed1a0e8cc9fa1..0000000000000000000000000000000000000000 --- a/2224020090/7-3.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include - -std::vector> matrixReshape(std::vector>& mat, int r, int c) { - int m = mat.size(); - int n = mat[0].size(); - if (m * n != r * c) { - return mat; - } - std::vector> reshaped(r, std::vector(c, 0)); - for (int i = 0; i < m * n; i++) { - reshaped[i / c][i % c] = mat[i / n][i % n]; - } - return reshaped; -} - -int main() { - std::vector> mat = {{1, 2}, {3, 4}}; - int r = 1; - int c = 4; - std::vector> reshaped = matrixReshape(mat, r, c); - std::cout << "The reshaped matrix is: " << std::endl; - for (const auto& row : reshaped) { - for (int num : row) { - std::cout << num << " "; - } - std::cout << std::endl; - } - return 0; -} diff --git a/2224020090/7-4.cpp b/2224020090/7-4.cpp deleted file mode 100644 index c526515673e75bdb72424d574431ff9d1ebd1e80..0000000000000000000000000000000000000000 --- a/2224020090/7-4.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include -using namespace std; - -struct TreeNode { - int val; - TreeNode* left; - TreeNode* right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -void levelOrderTraversal(TreeNode* root) { - if (root == NULL) { - return; - } - queue q; - q.push(root); - while (!q.empty()) { - TreeNode* current = q.front(); - q.pop(); - cout << current->val << " "; - if (current->left != NULL) { - q.push(current->left); - } - if (current->right != NULL) { - q.push(current->right); - } - } -} - -int main() { - - TreeNode* root = new TreeNode(1); - root->left = new TreeNode(2); - root->right = new TreeNode(3); - root->left->left = new TreeNode(4); - root->left->right = new TreeNode(5); - - \ No newline at end of file diff --git a/2224020090/7-8.cpp b/2224020090/7-8.cpp deleted file mode 100644 index e12be0128e0c013c99d11ab2b05a13640b784da9..0000000000000000000000000000000000000000 --- a/2224020090/7-8.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include -using namespace std; -struct TreeNode { - int val; - TreeNode* left; - TreeNode* right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; -TreeNode* lowestCommonAncestorHelper(TreeNode* root, TreeNode* p, TreeNode* q) { - if (root == NULL || root == p || root == q) { - return root; - } - TreeNode* left = lowestCommonAncestorHelper(root->left, p, q); - TreeNode* right = lowestCommonAncestorHelper(root->right, p, q); - if (left != NULL && right != NULL) { - return root; - } - return (left != NULL) ? left : right; -} -TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { - return lowestCommonAncestorHelper(root, p, q); -} - -int main() { - TreeNode* root = new TreeNode(3); - root->left = new TreeNode(5); - root->right = new TreeNode(1); - root->left->left = new TreeNode(6); - root->left->right = new TreeNode(2); - root->right->left = new TreeNode(0); - root->right->right = new TreeNode(8); - root->left->right->left = new TreeNode(7); - root->left->right->right = new TreeNode(4); - - TreeNode* p = root->left; - TreeNode* q = root->right; - TreeNode* ancestor = lowestCommonAncestor(root, p, q); - cout << "最近公共祖先的值为:" << ancestor->val << endl; - - return 0; -} diff --git "a/2224020091/\345\256\236\347\216\260\345\206\222\346\263\241\346\216\222\345\272\217\347\256\227\346\263\225.cpp" "b/2224020091/\345\256\236\347\216\260\345\206\222\346\263\241\346\216\222\345\272\217\347\256\227\346\263\225.cpp" deleted file mode 100644 index 9f96a426d91e177fa41fa2c0aa7c2cb477737fa2..0000000000000000000000000000000000000000 --- "a/2224020091/\345\256\236\347\216\260\345\206\222\346\263\241\346\216\222\345\272\217\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,42 +0,0 @@ -#include -using namespace std; - -void bubbleSort(int arr[], int n) { - for (int i = 0; i < n-1; i++) { - for (int j = 0; j < n-i-1; j++) { - if (arr[j] > arr[j+1]) { - // 交换arr[j]和arr[j+1] - int temp = arr[j]; - arr[j] = arr[j+1]; - arr[j+1] = temp; - - // 输出当前趟的排序结果 - cout << "第" << i+1 << "趟排序结果:"; - for (int k = 0; k < n; k++) { - cout << arr[k] << " "; - } - cout << endl; - } - } - } -} - -int main() { - int arr[] = {64, 34, 25, 12, 22, 11, 90}; - int n = sizeof(arr) / sizeof(arr[0]); - cout << "初始数组:"; - for (int i = 0; i < n; i++) { - cout << arr[i] << " "; - } - cout << endl; - - bubbleSort(arr, n); - - cout << "排序后的数组:"; - for (int i = 0; i < n; i++) { - cout << arr[i] << " "; - } - cout << endl; - - return 0; -} \ No newline at end of file diff --git "a/2224020094/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2224020094/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index 43a496f0f6674ce90f108e2993c0670ca7a5f1d4..0000000000000000000000000000000000000000 --- "a/2224020094/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include - -int* twoSum(int* nums, int numsSize, int target, int* returnSize) -{ - int *num = (int*)malloc(sizeof(int)*2); - for(int i = 0; i < numsSize - 1; i++) - { - for(int j = i + 1; j < numsSize; j++) - { - if(nums[i] + nums[j] == target) - { - num[0] = i; - num[1] = j; - *returnSize = 2; - return num; - } - } - } - return NULL; -} - -int main() -{ - int nums[] = {2, 7, 11, 15}; - int target = 9; - int returnSize; - int* result = twoSum(nums, 4, target, &returnSize); - if (result != NULL) - { - printf("Indices: %d, %d\n", result[0], result[1]); - free(result); - } - else - { - printf("No result found\n"); - } - return 0; -} diff --git "a/2224020096/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" "b/2224020096/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" deleted file mode 100644 index e1f408b12dac860e39b539d77f35911bfe973c09..0000000000000000000000000000000000000000 --- "a/2224020096/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" +++ /dev/null @@ -1,208 +0,0 @@ -//头文件 -//tree.h -#include -#include -#include -#define MaxSons 3 -typedef char ElemType; -typedef struct node -{ - ElemType data[15]; //结点的值 - struct node *sons[MaxSons]; //指向孩子结点 -}TSonNode; //孩子链存储结构中的结点类型 -typedef struct -{ - char N[15]; - char n[15]; -}array; -//读取文件内容到数组R中 -void ReadFile(array R[],FILE *fp,int &n) -{ - while((fscanf(fp,"%s",R[n].N))!=EOF&&(fscanf(fp,"%s",R[n].n))!=EOF) - n++; -} -//创建一颗树 -TSonNode *CreateTree(char str[],array R[],int n) -{ - TSonNode *t; - int k,i=0,j=0; - t=(TSonNode *)malloc(sizeof(TSonNode)); - strcpy(t->data,str); - for(k=0;ksons[k]=NULL; - while(isons[j]=CreateTree(R[i].n,R,n); - j++; - } - i++; - } - return t; -} -//输出树(孩子链存储结构) -void DispTree(TSonNode *t) -{ - int i=0; - if(t==NULL) - printf("此树为空树!\n"); - else - { - printf("%s",t->data); - if(t->sons[i]!=NULL) //若t结点至少有一个孩子 - { - printf("("); - for(i=0;isons[i]); - if(t->sons[i+1]!=NULL) - printf(","); - else - break; - } - printf(")"); - } - } -} -//销毁树 -void DestroyTree(TSonNode *t) -{ - if(t==NULL) - printf("此树为空树!\n"); - else - { - for(int i=0;isons[i]!=NULL) - DestroyTree(t->sons[i]); - else - break; - } - free(t); - } -} -//查找某一结点 -TSonNode *FindNode(TSonNode *t,char str[]) -{ - TSonNode *p; - if(t==NULL) - return NULL; - else - { - if(strcmp(t->data,str)==0) - return t; - else - { - for(int i=0;isons[i]!=NULL) - { - p=FindNode(t->sons[i],str); - if(p!=NULL) - return p; - } - } - return NULL; - } - } -} -//求某一结点的孩子个数 -int ChildCount(TSonNode *p) -{ - int count=0; - for(int i=0;isons[i]!=NULL) - count++; - else - break; - } - return count; -} -//求某棵树中的叶子结点数 -//本例中,叶子结点数等于班级数,一个叶子结点对应一个班级 -int LeafCount(TSonNode *p) -{ - int count=0; - if(p==NULL) - return 0; - else - { - if(p->sons[0]==NULL) - count++; - else - { - for(int i=0;isons[i]!=NULL) - count=count+LeafCount(p->sons[i]); - else - break; - } - } - } - return count; -} -//求某棵树的叶子结点值的和 -int LeafSumOfvalue(TSonNode *p) -{ - int sum=0; - if(p==NULL) - return 0; - else - { - if(p->sons[0]==NULL) - return atoi(p->data); - else - { - for(int i=0;isons[i]!=NULL) - sum+=LeafSumOfvalue(p->sons[i]); - else - break; - } - } - } - return sum; -} - -//源文件 -//exp7.cpp -#include -#include -#include"tree.h" -#define MaxSize 66 -int main() -{ - int n=0; - TSonNode *t; - array R[MaxSize]; - FILE *fp; - if((fp=fopen("table.txt","r"))==NULL) //以只读方式打开table.txt文件 - { - printf("error!cannot open the file!"); - exit(1); - } - printf("读取文件内容存入数组R中\n"); - ReadFile(R,fp,n); - printf("输出数组R:\n"); - for(int i=0;i -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct node -{ - ElemType data; - struct node *lchild; - struct node *rchild; - -}BTNode; -void CreateBTree(BTNode *&b,char *str) -{ - BTNode * St[MaxSize],*p; - int top=-1,k,j=0;char ch; - b=NULL; - ch=str[j]; - while(ch!='\0') - { - switch(ch) - { - case'(':top++;St[top]=p;k=1;break; - case')':top--;break; - case',':k=2;break; - - default:p=(BTNode *)malloc(sizeof(BTNode)); - p->data=ch;p->lchild=p->rchild=NULL; - if(b==NULL) - b=p; - else - { - switch(k) - { - case 1:St[top]->lchild=p;break; - case 2:St[top]->rchild=p;break; - } - } - } - j++;ch=str[j]; - } -} -void DestroyBTree(BTNode *&b) -{ - if(b!=NULL) - { - DestroyBTree(b->lchild); - DestroyBTree(b->rchild); - free(b); - } -} -BTNode *FindNode(BTNode *b,ElemType x) -{ - BTNode *p; - if(b==NULL) - return NULL; - else if(b->data==x) - return b; - else - { - p=FindNode(b->lchild,x); - if(p!=NULL) - return p; - else - return FindNode(b->rchild,x); - } -} -BTNode *LchildNode(BTNode *p) -{ - return p->lchild; - -} -BTNode *RchildNode(BTNode *p) -{ - return p->rchild; -} -int BTHeight(BTNode *b) -{ - int lchildh,rchildh; - if(b==NULL)return(0); - else - { - lchildh=BTHeight(b->lchild); - rchildh=BTHeight(b->rchild); - return(lchildh>rchildh)?(lchildh+1):(rchildh+1); - } -} -void DispBTree(BTNode *b) -{ - if(b!=NULL) - { - printf("%c",b->data); - if(b->lchild!=NULL||b->rchild!=NULL) - { - printf("("); - DispBTree(b->lchild); - if(b->rchild!=NULL)printf(","); - DispBTree(b->rchild); - printf(")"); - } - } -} -int main() -{ - BTNode *b,*p,*lp,*rp;; - printf("二叉树的基本运算如下:\n"); - printf(" (1)创建二叉树\n"); - CreateBTree(b,"A(B(D,E(H(J,k(L,M(,N))))),C(F,G(,I)))"); - printf(" (2)输出二叉树:");DispBTree(b);printf("\n"); - printf(" (3)H结点:"); - p=FindNode(b,'H'); - if(p!=NULL) - { lp= LchildNode(p); - if(lp!=NULL)printf("左孩子为%c",lp->data); - else printf("无左孩子"); - rp=RchildNode(p); - if(rp!=NULL) printf("右孩子为%c",rp->data); - else printf("无右孩子"); - - } - printf("\n"); - printf(" (4)二叉树b的高度:%d\n",BTHeight(b)); - printf(" (5)释放二叉树b\n"); - DestroyBTree(b); - return 1; -} diff --git "a/2224020096/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.cpp" "b/2224020096/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.cpp" deleted file mode 100644 index f02c97772d50f900412afd85ba5f7c75d6bd6935..0000000000000000000000000000000000000000 --- "a/2224020096/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.cpp" +++ /dev/null @@ -1,154 +0,0 @@ -#include -#include -#define max 100 -#define INF 32767 -#define InfoType char -typedef struct -{ - int no; - InfoType info; -}VertexType; - -typedef struct -{ - int edges[max][max]; - int n,e; - VertexType vexs[max]; -}MatGraph; - - -typedef struct ANode -{ - int adjvex; - struct ANode *nextarc; - int weight; -}ArcNode; -typedef struct Vnode -{ - InfoType info; - ArcNode *firstarc; -}VNode; -typedef struct -{ - VNode adjlist[max]; - int n,e; -}AdjGraph; - -void Creatadj(AdjGraph *&G,int A[max][max],int n,int e) //图 -{ - int i,j; - ArcNode *p; - G=(AdjGraph *)malloc(sizeof(AdjGraph)); - for(i=0;iadjlist[i].firstarc=NULL; - } - for(i=0;i=0;j--) - { - if(A[i][j]!=0&&A[i][j]!=INF) - { - p=(ArcNode *)malloc(sizeof(ArcNode)); - p->adjvex=j; - p->weight=A[i][j]; - p->nextarc=G->adjlist[i].firstarc; - G->adjlist[i].firstarc=p; - } - } - - } - G->n=n; - G->e=e; - -} - -void Creatmat(MatGraph &g,int A[max][max],int n,int e) //矩阵 -{ - int i,j; - g.n=n; - g.e=e; - for(i=0;in;i++) - { - p=G->adjlist[i].firstarc; - printf("顶点%d ",i); - while(p!=NULL) - { - printf("%d[%d]->",p->adjvex,p->weight); - p=p->nextarc; - - } - printf("^\n"); - - } -} - - - -void DedtoryAdj(AdjGraph *&G) //销毁 -{ - int i; - ArcNode *pre,*p; - for(i=0;in;i++) - { - pre=G->adjlist[i].firstarc; - while(pre!=NULL) - { - p=pre; - pre=pre->nextarc; - free(p); - } - } - free(G);G=NULL; -} -int main() -{ - AdjGraph *G; - MatGraph g; - int n=6; - int e=10; - int A[max][max]={{0,5,INF,7,INF,INF},{INF,0,4,INF,INF,INF},{8,INF,0,INF,9,INF},{INF,INF,5,0,INF,6},{INF,INF,INF,5,0,INF},{3,INF,INF,INF,1,0}}; - Creatmat(g,A,n,e); - printf("邻接矩阵为:\n\n"); - Dispmat(g); - Creatadj(G,A,n,e); - printf("邻接表为:\n\n"); - DispAdj(G); - printf("销毁邻接表为:\n\n"); - DedtoryAdj(G); - if(G==NULL) - printf("成功!\n\n"); - system("pause"); - return 0; -} diff --git "a/2224020096/\345\256\236\347\216\260\347\250\200\347\226\217\347\237\251\351\230\265\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2224020096/\345\256\236\347\216\260\347\250\200\347\226\217\347\237\251\351\230\265\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" deleted file mode 100644 index bbe21844eabfb5a802a3e053ccc4c9034e37a903..0000000000000000000000000000000000000000 --- "a/2224020096/\345\256\236\347\216\260\347\250\200\347\226\217\347\237\251\351\230\265\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,213 +0,0 @@ -#include - -#define MaxSize 100//矩阵中非零元素最多个数 -#define N 4 //稀疏矩阵行数 -#define M 4 //稀疏矩阵列数 -typedef int elem; -typedef struct -{ - int r; //行数 - int c; //列数 - elem d; //数据元素 -}TupNode; //三元组定义 - -typedef struct -{ - int row; //行数值 - int cols; //列数值 - int nums; //非零元素个数 - TupNode data[MaxSize]; //存放数组 -}TSMatrix; //三元组顺序表定义 - -//生成稀疏矩阵A三元组 -void CreatMat(TSMatrix &t, elem A[N][M]) -{ - int i, j; - t.row = N; //总行数 - t.cols = M; //总列数 - t.nums = 0; //总非零元素数,初始为零 - for(i = 0; i < N; i++)//遍历数组A - { - for(j = 0; j < M; j++) - { - if(A[i][j] != 0) //判断是否为非零元素 - { - t.data[t.nums].r = i; //记录非零元素所在的行数 - t.data[t.nums].c = j; //记录非零元素所在的列数 - t.data[t.nums].d = A[i][j]; //记录非零元素值 - t.nums++; //非零元素个数加一 - } - } - } -} - -//输出三元组 -void DipMat(TSMatrix t) -{ - int i; - if(t.nums <= 0)//判断是否有非零元素 - return; - printf("\t%d\t%d\t%d\n",t.row, t.cols, t.nums); - printf("-------------------------------------\n"); - for(i = 0; i < t.nums; i++) - printf("\t%d\t%d\t%d\n",t.data[i].r, t.data[i].c, t.data[i].d); -} - -//三元组转置 -void TranTat(TSMatrix t, TSMatrix &tb) -{ - int p, q = 0, v; //q为ta.data的下标,v进行计数,p为t.data的下标 - tb.row = t.cols; //总行数和总列数互换 - tb.cols = t.row; - tb.nums = t.nums; //非零元素个数 - if(t.nums != 0) //当存在非零元素时进行转置 - { - for(v = 0; v < t.cols; v++)//t列数为ta的行数 - { - for(p = 0; p < t.nums; p++) - if(t.data[p].c == v) - { - tb.data[q].r = t.data[p].c;//行列互换 - tb.data[q].c = t.data[p].r; - tb.data[q].d = t.data[p].d;//元素值传递 - q++; //下一个data - } - } - } -} - -//两个稀疏矩阵相加后对应的三元组 -void MatAdd(TSMatrix a, TSMatrix b, TSMatrix &c) -{ - if(a.row != b.row || a.cols != b.cols)//判断是否符合矩阵相加条件,即两矩阵行数和列数分别相等 - { - printf("矩阵相加操作失败\n"); - return; - } - c.row = a.row; //总行数赋值 - c.cols = a.cols; //总列数赋值 - int i = 0 , j = 0, k = 0; - while(i < a.nums || j < b.nums) //遍历两个三元组 - { - if(a.data[i].r < b.data[j].r)//比较非零数值所在的行数大小,将较小的行数的非零数值放进c的三元组 - { - c.data[k].r = a.data[i].r; - c.data[k].c = a.data[i].c; - c.data[k].d = a.data[i].d; - i++; - k++; - } - else if(a.data[i].r == b.data[j].r)//比较非零数值所在的行数大小,相等时 - { - if(a.data[i].c < b.data[j].c)//比较非零数值所在的列数大小,将较小的列数非零数值放进c的三元组 - { - c.data[k].r = a.data[i].r; - c.data[k].c = a.data[i].c; - c.data[k].d = a.data[i].d; - i++; - k++; - } - else if(a.data[i].c == b.data[j].c)//比较非零数值所在的列数大小,将两个非零元素相加的值放进c的三元组 - { - c.data[k].r = b.data[j].r; - c.data[k].c = b.data[j].c; - c.data[k].d = a.data[i].d + b.data[j].d; - i++; - j++; - k++; - } - else //比较非零数值所在的列数大小,将较小的列数非零数值放进c的三元组 - { - c.data[k].r = b.data[j].r; - c.data[k].c = b.data[j].c; - c.data[k].d = b.data[j].d; - j++; - k++; - } - } - else //比较非零数值所在的行数大小,将较小的行数数非零数值放进c的三元组 - { - c.data[k].r = b.data[j].r; - c.data[k].c = b.data[j].c; - c.data[k].d = b.data[j].d; - j++; - k++; - } - } - c.nums = k;//非零元素个数 -} - -//返回三元组 t 表示的 A[i][j]值 -int getvalue(TSMatrix t,int i,int j) -{ - for(int k = 0; k < t.nums; k++) - { - if(t.data[k].r == i && t.data[k].c == j) - return t.data[k].d; - } - return 0; -} - -//两个稀疏矩阵相乘后对应的三元组 -void MatMul(TSMatrix a,TSMatrix b,TSMatrix &c) -{ - if(a.cols != b.row) //判断是否满足两矩阵相乘的条件,即第一个矩阵的列数与第二矩阵的行数相等 - { - printf("矩阵相乘操作失败\n"); - return; - } - int i, j, k = 0, s; - c.row = a.row; - c.cols = b.cols; - - for(i = 0; i < a.row; i++) //控制行数 - { - for(j = 0; j < b.cols; j++) //控制列数 - { - s = 0; - for(int m = 0; m < b.cols; m++) - { - s = s + getvalue(a, m, i)*getvalue(b, j, m);//第一个矩阵行的每个元素与第二个矩阵列的每个元素相乘,并将结果相加 - } - if(s != 0) //如果数据元素不为0,则将其放进c三元组中 - { - c.data[k].r = i; - c.data[k].c = j; - c.data[k].d = s; - k++; - } - } - } - c.nums = k; //总非零元素个数 -} - -int main() -{ - TSMatrix t1, t2, c; //t1, t2为三元组 - elem a[4][4] = { - {1,0,3,0}, - {0,1,0,0}, - {0,0,1,0}, - {0,0,1,1}}; - CreatMat(t1, a); //生成稀疏矩阵A三元组 - printf("a的三元组:\n"); - DipMat(t1); //输出三元组 - elem b[4][4] = { - {3,0,0,0}, - {0,4,0,0}, - {0,0,1,0}, - {0,0,0,2}}; - CreatMat(t2, b); //生成稀疏矩阵B三元组 - printf("b的三元组:\n"); - DipMat(t2); //输出三元组 - TranTat(t1, c); //三元组转置 - printf("a转置后\n"); - DipMat(c); //输出三元组 - printf("c = a + b\nc的三元组:\n"); - MatAdd(t1, t2, c); //计算两个稀疏矩阵相加后对应的三元组 - DipMat(c); //输出三元组 - printf("c = a * b\nc的三元组:\n"); - MatMul(t1, t2, c); //矩阵相乘 - DipMat(c); //输出三元组 - return 0; -} diff --git "a/2224020096/\346\236\204\351\200\240\345\223\210\346\233\274\345\244\253\345\222\214\347\224\237\346\210\220\345\223\210\346\233\274\345\244\253\347\274\226\347\240\201.cpp" "b/2224020096/\346\236\204\351\200\240\345\223\210\346\233\274\345\244\253\345\222\214\347\224\237\346\210\220\345\223\210\346\233\274\345\244\253\347\274\226\347\240\201.cpp" deleted file mode 100644 index 8053f112ec5af934f719194bca54c00463f2d744..0000000000000000000000000000000000000000 --- "a/2224020096/\346\236\204\351\200\240\345\223\210\346\233\274\345\244\253\345\222\214\347\224\237\346\210\220\345\223\210\346\233\274\345\244\253\347\274\226\347\240\201.cpp" +++ /dev/null @@ -1,40 +0,0 @@ -void HuffmanTree(element *huffTree, int *w, int n) { - int i1, i2; - for (int i = 0; i < 2 * n - 1; i++) { //初始化项目结点 - huffTree[i].parent = -1; - huffTree[i].lchild = -1; - huffTree[i].rchild = -1; - } - //初始化前n个结点的权值 - for (int i = 0; i < n; i++) { - huffTree[i].weight = w[i]; - } - for (int k = n; k < 2 * n - 1; k++) { //构建哈夫曼树 - select(huffTree, k, &i1, &i2); - printf("最小下标:%d,次小下标;%d\n", i1, i2); - huffTree[k].weight = huffTree[i1].weight + huffTree[i2].weight;//权值相加 - huffTree[i1].parent = k;//将k下标赋给即将合并的结点的双亲 - huffTree[i2].parent = k; - huffTree[k].lchild = i1;//将合并的两个结点下标赋为k的左右孩子 - huffTree[k].rchild = i2; - } -} -void select(element *huffTree, int k, int *i1, int *i2) { //寻找parent为-1的权值最小和次小的两个结点 - int index = 0, count = 0, temp; - int arr[k]; - for (int i = 0; i < k; i++) { - if (huffTree[i].parent == -1) - arr[count++] = i; //将所有叶子结点的下标保存到arr中 - } - for (int i = 0; i < count; i++) {//冒泡排序 - for (int j = 0; j < count - i - 1; j++) { - if (huffTree[arr[j]].weight > huffTree[arr[j + 1]].weight) { - temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } - } - } - *i1 = arr[0]; - *i2 = arr[1]; -} \ No newline at end of file diff --git "a/2224020096/\346\261\2025X5\351\230\266\347\232\204\350\236\272\346\227\213\346\226\271\351\230\265.cpp" "b/2224020096/\346\261\2025X5\351\230\266\347\232\204\350\236\272\346\227\213\346\226\271\351\230\265.cpp" deleted file mode 100644 index f9da110b9e3bbdc46168c17aea10dfe35d46acc7..0000000000000000000000000000000000000000 --- "a/2224020096/\346\261\2025X5\351\230\266\347\232\204\350\236\272\346\227\213\346\226\271\351\230\265.cpp" +++ /dev/null @@ -1,99 +0,0 @@ -# include - -# define MaxLen 10 - -void fun(int a[MaxLen][MaxLen],int n) - -{ - - int i,j,k=0,m; - - if( n % 2==0) //m = 「n/2」 - - m=n/2; - - else - - m=n/2+1; - - for(i=0;i=i;j--) //产生该圈下横行的数字 - - { - - k++; - - a[n-i-1][j]=k; - - } - - for(j=n-i-2;j>=i+1;j--) //产生该圈左竖列的数字 - - { - - k++; - - a[j][i]=k; - - } - - } - -} - -void main() - -{ - - int n,i,j; - - int a[MaxLen][MaxLen]; - - printf("\n"); - - printf("输入n(n<10):"); - - scanf("%d",&n); - - fun(a,n); - - printf("%d阶数字方阵如下:\n",n); - - for(i=0;i -#define MAX_LEN 1000 -using namespace std; -//upper triangular matrix -int acc(int i, int j,int& n) { - if (i < j) - swap(i, j); - return i * (i + 1)/2 + j; -} -void add(int a[], int b[], int add_a_b[], int n) { - int len = (1 + n) * n / 2; - for (int i = 0; i < len; i++) { - add_a_b[i] = a[i] + b[i]; - } -} -void mult(int a[], int b[], int mult_a_b[], int n) { - for (int i = 0; i < n; i++) { - for (int j = 0; j <= i; j++) { - mult_a_b[acc(i, j, n)] = 0; - for (int k = 0; k < n; k++) - mult_a_b[acc(i, j, n)] += a[acc(i, k, n)] * b[acc(k, j, n)]; - } - } -} - -int main() -{ - std::cout << "输入阶数"; - int n, a[MAX_LEN], b[MAX_LEN], add_a_b[MAX_LEN], mult_a_b[MAX_LEN]; - cin >> n; - int len = (1 + n) * n / 2; - cout << "输入a矩阵" << endl; - for (int i = 0; i < len; i++) { - cin >> a[i]; - } - cout << "输入b矩阵" << endl; - for (int i = 0; i < len; i++) { - cin >> b[i]; - } - add(a, b, add_a_b, n); - cout << "相加的结果是:" << endl; - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - cout << add_a_b[acc(i, j, n)] << " "; - } - cout << endl; - } - mult(a, b, mult_a_b, n); - cout << "相乘的结果是:" << endl; - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - cout << mult_a_b[acc(i, j, n)]<<" "; - } - cout << endl; - } -} - - - - diff --git "a/2224020096/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204.cpp" "b/2224020096/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204.cpp" deleted file mode 100644 index 3d19beea8d54a55b60b401f64a2b1f7de5d00ab9..0000000000000000000000000000000000000000 --- "a/2224020096/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204.cpp" +++ /dev/null @@ -1,143 +0,0 @@ -#include "tree.cpp" //包含二叉树的基本运算 -typedef char ElemType; - -/*--------- -求二叉树中从根结点到叶子结点的路径 ------------*/ - -//采用先序遍历方法输出所有从叶子结点到根结点的逆路径 -void AllPath1(BTNode *b,ElemType path[],int pathlen){ //path[]存储路径【相当于一个栈】,pathlen记录路径长度 - int i; //调用AllPath1函数时,传进来的path[]是空数组,pathlen为0; - if(b!=NULL){ - if(b->lchild==NULL && b->rchild==NULL){ //若b为叶子结点 - printf("%c到根结点的逆路径:%c->",b->data,b->data); //打印叶子结点 - for(i=pathlen-1;i>0;i--) //打印路径时,并没有修改栈顶指针pathlen的值 - printf("%c->",path[i]); //所以每次递归遍历的路径存储在上次递归路径的后面 - printf("%c\n",path[0]); - }else{ - path[pathlen] = b->data; //将当前结点放入路径中 - pathlen++; //路径长度加1 - AllPath1(b->lchild,path,pathlen); //递归扫描左子树 - AllPath1(b->rchild,path,pathlen); //递归扫描右子树 - } - } -} - - -//采用先序遍历方法输出第一条最长的逆路径 -void LongPath1(BTNode *b,ElemType path[],int pathlen,ElemType longpath[],int &longpathlen ){ - int i; //longpath[]保存当前最长路径,每次递归的路径长度都可能被修改 - if(b==NULL){ //若当前节点为空 - if(pathlen>longpathlen){ //若当前路径更长,将路径保存 - for(i=pathlen-1;i>=0;i--) - longpath[i]=path[i]; - longpathlen= pathlen; - } - } else{ - path[pathlen] = b->data; - pathlen++; - LongPath1(b->lchild,path,pathlen,longpath,longpathlen); - LongPath1(b->rchild,path,pathlen,longpath,longpathlen); - } -} - -//采用后序非递归遍历方法输出所有从叶子结点到根结点的逆路径 -void AllPath2(BTNode *b){ - BTNode * st[MaxSize]; //定义一个顺序栈st - int top=-1; //栈顶指针初始化 - BTNode *p,*r; - bool flag; - p=b; - do{ //至少循环一次 - while(p!=NULL){ //扫描结点p的所有左下结点并进栈 - top++; - st[top] = p; //结点p进栈 - p=p->lchild; //移动到左孩子 - - } - r=NULL; //r指向刚刚访问的结点,初始时为空 - flag=true; //flag为真表示正在处理栈顶结点 - while(top>-1 && flag){ //栈不空且flag为真时进入循环 - p=st[top]; //取出当前的栈顶结点p - if(p->rchild == r){ //若结点p的右孩子为空或者为刚刚访问过的结点 - if(p->lchild==NULL && p->rchild==NULL){ //若为叶子结点,输出栈中所有结点值 - printf("%c到根结点逆路径:",p->data); - for(int i=top;i>0;i--){ - printf("%c->",st[i]->data); - } - printf("%c\n",st[0]->data); - - } - top--; //退栈 - r=p; //r指向刚访问过的结点 - }else{ - p=p->rchild; //转向处理其右子树 - flag=false; //表示当前不是处理栈顶结点 - } - } - } while(top>-1); //栈空时,退出循环 -} - -//采用层次遍历输出所有从叶子结点到根结点的逆路径 -void AllPath3(BTNode *b){ - //定义顺序队列 - struct snode{ - BTNode *node; //存放当前结点指针 - int parent; //存放双亲结点在队列中的位置 - }Qu[MaxSize]; - int front,rear,p; //定义队头和队尾指针 - front=rear=-1; //置队列为空队列 - rear++; - Qu[rear].node=b; //根结点指针进入队列 - Qu[rear].parent=-1; //根结点没有双亲结点 - while(frontlchild==NULL && b->rchild==NULL){ //若b为叶子结点 - printf("%c到根结点逆路径:",b->data); - p=front; - while(Qu[p].parent != -1){ - printf("%c->",Qu[p].node->data); - p=Qu[p].parent; - } - printf("%c\n",Qu[p].node->data); - } - if(b->lchild!=NULL){ //若有左孩子,将其进队 - rear++; - Qu[rear].node=b->lchild; - Qu[rear].parent=front; - - } - if(b->rchild!=NULL){ //若有右孩子,将其进队 - rear++; - Qu[rear].node=b->rchild; - Qu[rear].parent=front; - } - } - -} - -int main(){ - BTNode *b; - ElemType path[MaxSize],longpath[MaxSize]; - int i,longpathlen=0; - CreatBTree(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); - printf("二叉树b:"); - DispBTree(b);printf("\n"); - printf("先序遍历方法:\n"); - AllPath1(b,path,0); - LongPath1(b,path,0,longpath,longpathlen); - printf("第一最长逆路径长度:%d\n",longpathlen); - printf("第一最长逆路径:"); - for(i=longpathlen-1;i>=0;i--) - printf("%c",longpath[i]); - printf("\n"); - printf("后序非递归遍历方法:\n"); - AllPath2(b); - printf("层次遍历方法:\n"); - AllPath3(b); - DestroyBTree(b); - return 1; -} - - \ No newline at end of file diff --git "a/2224020096/\346\261\202\350\247\243\345\273\272\345\205\254\350\267\257\347\232\204\351\227\256\351\242\230.cpp" "b/2224020096/\346\261\202\350\247\243\345\273\272\345\205\254\350\267\257\347\232\204\351\227\256\351\242\230.cpp" deleted file mode 100644 index 0cfab517ce74d0ad1bcbf171036b2aa2135e0571..0000000000000000000000000000000000000000 --- "a/2224020096/\346\261\202\350\247\243\345\273\272\345\205\254\350\267\257\347\232\204\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,41 +0,0 @@ -#include -using namespace std; -const int N=20006; -int n,k,m,p,t,f[N]; -struct xd{int x,y,c1,c2;}a[N]; -inline int read(){ - int T=0,F=1; char ch=getchar(); - while(ch<'0'||ch>'9'){if(ch=='-') F=-1; ch=getchar();} - while(ch>='0'&&ch<='9') T=(T<<3)+(T<<1)+(ch-48),ch=getchar(); - return F*T; -} -int getf(int u){return f[u]==u?u:f[u]=getf(f[u]);} -bool merge(int u,int v){ - u=getf(u),v=getf(v); - if(u==v) return 0; - f[u]=v; return 1; -} -bool check(int u){ - p=0; - for(int i=1;i<=n;++i) f[i]=i; - for(int i=1;i<=m;++i) if(a[i].c1<=u) if(merge(a[i].x,a[i].y)) ++p; - if(p>1); - if(l==r) return l; - if(check(mid)) return ef(l,mid); - else return ef(mid+1,r); -} -int main(){ - n=read(),k=read(),m=read()-1; - for(int i=1;i<=m;++i) a[i].x=read(),a[i].y=read(),a[i].c1=read(),a[i].c2=read(); - t=ef(1,30000),printf("%d\n",t); - for(int i=1;i<=n;++i) f[i]=i; - for(int i=1;i<=m;++i) if(a[i].c1<=t) if(merge(a[i].x,a[i].y)) printf("%d %d\n",i,1); - for(int i=1;i<=m;++i) if(a[i].c2<=t) if(merge(a[i].x,a[i].y)) printf("%d %d\n",i,2); - return 0; -} \ No newline at end of file diff --git "a/2224020096/\351\207\207\347\224\250\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225\346\261\202\345\270\246\346\235\203\346\234\211\345\220\221\345\233\276\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" "b/2224020096/\351\207\207\347\224\250\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225\346\261\202\345\270\246\346\235\203\346\234\211\345\220\221\345\233\276\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" deleted file mode 100644 index 2c0f2f607c5b059d7dbb6f87fdf5debe15be2a4d..0000000000000000000000000000000000000000 --- "a/2224020096/\351\207\207\347\224\250\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225\346\261\202\345\270\246\346\235\203\346\234\211\345\220\221\345\233\276\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" +++ /dev/null @@ -1,66 +0,0 @@ -#include -#define V 4 //设定图中的顶点数 -#define INF 65535 // 设置一个最大值 -int P[V][V] = { 0 }; //记录各个顶点之间的最短路径 -void printMatrix(int matrix[][V]); //输出各个顶点之间的最短路径 -void printPath(int i, int j); // 递归输出各个顶点之间最短路径的具体线路 -void floydWarshall(int graph[][V]); // 实现弗洛伊德算法 -int main() { - // 有向加权图中各个顶点之间的路径信息 - int graph[V][V] = { {0, 3, INF, 5}, - {2, 0, INF, 4}, - {INF, 1, 0, INF}, - {INF, INF, 2, 0} }; - floydWarshall(graph); -} -// 中序递归输出各个顶点之间最短路径的具体线路 -void printPath(int i, int j) -{ - int k = P[i][j]; - if (k == 0) - return; - printPath(i, k); - printf("%d-", k + 1); - printPath(k, j); -} -// 输出各个顶点之间的最短路径 -void printMatrix(int graph[][V]) { - int i, j; - for (i = 0; i < V; i++) { - for (j = 0; j < V; j++) { - if (j == i) { - continue; - } - printf("%d - %d: 最短路径为:", i + 1, j + 1); - if (graph[i][j] == INF) - printf("%s\n", "INF"); - else { - printf("%d", graph[i][j]); - printf(",依次经过:%d-", i + 1); - //调用递归函数 - printPath(i, j); - printf("%d\n", j + 1); - } - } - } -} -// 实现弗洛伊德算法,graph[][V] 为有向加权图 -void floydWarshall(int graph[][V]) { - int i, j, k; - //遍历每个顶点,将其作为其它顶点之间的中间顶点,更新 graph 数组 - for (k = 0; k < V; k++) { - for (i = 0; i < V; i++) { - for (j = 0; j < V; j++) { - //如果新的路径比之前记录的更短,则更新 graph 数组 - if (graph[i][k] + graph[k][j] < graph[i][j]) { - graph[i][j] = graph[i][k] + graph[k][j]; - //记录此路径 - P[i][j] = k; - } - } - } - } - // 输出各个顶点之间的最短路径 - printMatrix(graph); -} - diff --git "a/2224020097/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2224020097/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index 78b256e8871447a8accaa86b3e3eadc0749208db..0000000000000000000000000000000000000000 --- "a/2224020097/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,24 +0,0 @@ -impl Solution { - pub fn two_sum(nums: Vec, target: i32) -> Vec { - let mut nums_sorted = nums.clone(); - nums_sorted.sort(); - for (idx, num) in nums_sorted.iter().enumerate() { - let mut left = idx + 1; - let mut right = nums_sorted.len() - 1; - while left <= right { - let mid = (left + right) / 2; - if nums_sorted[mid] == target - num { - let mut idx1 = nums.iter().position(|&x| x == *num).unwrap(); - let mut idx2 = nums.iter().rposition(|&x| x == nums_sorted[mid]).unwrap(); - return vec![idx1 as i32, idx2 as i32]; - } - if nums_sorted[mid] < target - num { - left = mid + 1; - } else { - right = mid - 1; - } - } - } - return vec![]; - } -} diff --git "a/2224020099/chapter_2/chaper2\345\256\236\351\252\214\351\242\230/\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\347\256\227\346\263\225.cpp" "b/2224020099/chapter_2/chaper2\345\256\236\351\252\214\351\242\230/\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index 673e8354e918964871dfc2820f1e2e65cf923aa0..0000000000000000000000000000000000000000 --- "a/2224020099/chapter_2/chaper2\345\256\236\351\252\214\351\242\230/\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include - -using namespace std; - -vector multiply(vector& poly1, vector& poly2) { - if(poly1.size() != poly2.size()){ - return {-1}; - } - int n = poly1.size(); - vector result(n, 0); - - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - result[i + j] += poly1[i] * poly2[j]; - } - } - - return result; -} - -int main() { - vector poly1 = {2, 3, 4}, poly2 = {1, 5}; - vector result = multiply(poly1, poly2); - - if(result[0] != 0){ - for (int i = result.size() - 1; i >= 0; i--) { - cout << result[i]; - if (i > 0) cout << "x^" << i; - if (i == result.size() - 1) cout << endl; - } - }else{ - cout << "The result is zero." << endl; - } - - return 0; -}; diff --git "a/2224020099/chapter_2/chaper2\345\256\236\351\252\214\351\242\230/\345\215\225\351\223\276\350\241\250\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2224020099/chapter_2/chaper2\345\256\236\351\252\214\351\242\230/\345\215\225\351\223\276\350\241\250\345\237\272\346\234\254\350\277\220\347\256\227.cpp" deleted file mode 100644 index da00da3d59caf874621f74a7c1afc335cab3e5d7..0000000000000000000000000000000000000000 --- "a/2224020099/chapter_2/chaper2\345\256\236\351\252\214\351\242\230/\345\215\225\351\223\276\350\241\250\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include - -using namespace std; - -struct ListNode { - int val; - std::unique_ptr next; - ListNode(int x) : val(x), next(nullptr) {} -}; - -void insert(ListNode* &head, int val) { - std::unique_ptr node = std::make_unique(val); - node->next = std::move(head); - head = std::move(node); -} - -void deleteNode(ListNode* &head, int val) { - if (head == nullptr) return; - if (head->val == val) { - head = std::move(head->next); - return; - } - ListNode* prev = head; - ListNode* cur = head->next; - while (cur != nullptr && cur->val != val) { - prev = cur; - cur = cur->next; - } - if (cur != nullptr) { - prev->next = std::move(cur->next); - } -} - -void traverse(ListNode* head) { - ListNode* cur = head; - while (cur != nullptr) { - cout << cur->val << " "; - cur = cur->next.get(); - } - cout << endl; -} - -int main() { - ListNode* head = nullptr; - - insert(head, 1); - insert(head, 2); - insert(head, 3); - insert(head, 4); - insert(head, 5); - - traverse(head); - - deleteNode(head, 3); - - traverse(head); - - return 0; -} diff --git "a/2224020099/chapter_2/chaper2\345\256\236\351\252\214\351\242\230/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" "b/2224020099/chapter_2/chaper2\345\256\236\351\252\214\351\242\230/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" deleted file mode 100644 index 68e4e8522818f1965808dd386a5b7f36f276a566..0000000000000000000000000000000000000000 --- "a/2224020099/chapter_2/chaper2\345\256\236\351\252\214\351\242\230/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" +++ /dev/null @@ -1,63 +0,0 @@ -#include -#include - -struct ListNode { - int val; - struct ListNode *next; -}; - -struct ListNode* createNode(int val) { - struct ListNode *node = (struct ListNode*)malloc(sizeof(struct ListNode)); - node->val = val; - node->next = NULL; - return node; -} - -struct ListNode* partition(struct ListNode* head, int k) { - struct ListNode *left = NULL, *right = NULL; - struct ListNode *curr = head; - while (curr != NULL) { - if (curr->val < k) { - if (left == NULL) { - left = curr; - } else { - left->next = curr; - } - left = left->next; - } else { - if (right == NULL) { - right = curr; - } else { - right->next = curr; - } - right = right->next; - } - curr = curr->next; - } - if (left != NULL) { - left->next = right; - } - return left; -} - -void printList(struct ListNode* head) { - struct ListNode *curr = head; - while (curr != NULL) { - printf("%d ", curr->val); - curr = curr->next; - } - printf("\n"); -} - -int main() { - struct ListNode *head = createNode(1); - head->next = createNode(4); - head->next->next = createNode(3); - head->next->next->next = createNode(2); - head->next->next->next->next = createNode(5); - head->next->next->next->next->next = createNode(2); - head->next->next->next->next->next->next = createNode(4); - head->next->next->next->next->next->next->next = createNode(6); - head->next->next->next->next->next->next->next->next = createNode(3); - int k = 3; -} diff --git "a/2224020099/chapter_3/\345\237\272\346\234\254\350\256\241\347\256\227\346\234\2722.cpp" "b/2224020099/chapter_3/\345\237\272\346\234\254\350\256\241\347\256\227\346\234\2722.cpp" deleted file mode 100644 index 432d3c12d3b33dd69e740e7b3ca9f6e847fc0084..0000000000000000000000000000000000000000 --- "a/2224020099/chapter_3/\345\237\272\346\234\254\350\256\241\347\256\227\346\234\2722.cpp" +++ /dev/null @@ -1,37 +0,0 @@ -#include - -int calculate(char * s){ - int *stack = (int *)malloc(sizeof(int) * (strlen(s) + 1)); - int top = -1; - int num = 0; - char sign = '+'; - for (int i = 0; s[i]; i++) { - if (s[i] >= '0' && s[i] <= '9') { - num = num * 10 + (s[i] - '0'); - } - if ((s[i] < '0' && s[i] != ' ') || s[i + 1] == '\0') { - switch (sign) { - case '+': - stack[++top] = num; - break; - case '-': - stack[++top] = -num; - break; - case '*': - stack[top] *= num; - break; - case '/': - stack[top] /= num; - break; - } - sign = s[i]; - num = 0; - } - } - int result = 0; - for (int i = 0; i <= top; i++) { - result += stack[i]; - } - free(stack); - return result; -} diff --git "a/2224020099/chapter_3/\346\234\200\345\260\217\346\240\210.cpp" "b/2224020099/chapter_3/\346\234\200\345\260\217\346\240\210.cpp" deleted file mode 100644 index 997909fa7381338b88c8e55d18c585a745b9ce44..0000000000000000000000000000000000000000 --- "a/2224020099/chapter_3/\346\234\200\345\260\217\346\240\210.cpp" +++ /dev/null @@ -1,41 +0,0 @@ -typedef struct { - int *data; - int *min; - int top; -} MinStack; - -MinStack* minStackCreate() { - MinStack *obj = (MinStack *)malloc(sizeof(MinStack)); - obj->data = (int *)malloc(sizeof(int) * 30000); - obj->min = (int *)malloc(sizeof(int) * 30000); - obj->top = -1; - return obj; -} - -void minStackPush(MinStack* obj, int x) { - obj->top++; - obj->data[obj->top] = x; - if (obj->top == 0) { - obj->min[obj->top] = x; - } else { - obj->min[obj->top] = fmin(x, obj->min[obj->top - 1]); - } -} - -void minStackPop(MinStack* obj) { - obj->top--; -} - -int minStackTop(MinStack* obj) { - return obj->data[obj->top]; -} - -int minStackGetMin(MinStack* obj) { - return obj->min[obj->top]; -} - -void minStackFree(MinStack* obj) { - free(obj->data); - free(obj->min); - free(obj); -} diff --git "a/2224020099/chapter_3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" "b/2224020099/chapter_3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" deleted file mode 100644 index 1f1cb65d5a737045be9e3025be873e2d0a0a86d1..0000000000000000000000000000000000000000 --- "a/2224020099/chapter_3/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.cpp" +++ /dev/null @@ -1,21 +0,0 @@ -bool isValid(char* s) { - int n=strlen(s); - char stacks[10000]={0}; - int top=-1; - int i; - for(i=0;istack1 = (int *)malloc(sizeof(int) * 10000); - obj->stack2 = (int *)malloc(sizeof(int) * 10000); - obj->top1 = -1; - obj->top2 = -1; - return obj; -} - -void myQueuePush(MyQueue* obj, int x) { - obj->stack1[++(obj->top1)] = x; -} - -int myQueuePop(MyQueue* obj) { - if (obj->top2 == -1) { - while (obj->top1 != -1) { - obj->stack2[++(obj->top2)] = obj->stack1[(obj->top1)--]; - } - } - return obj->stack2[(obj->top2)--]; -} - -int myQueuePeek(MyQueue* obj) { - if (obj->top2 == -1) { - while (obj->top1 != -1) { - obj->stack2[++(obj->top2)] = obj->stack1[(obj->top1)--]; - } - } - return obj->stack2[obj->top2]; -} - -bool myQueueEmpty(MyQueue* obj) { - return obj->top1 == -1 && obj->top2 == -1; -} - -void myQueueFree(MyQueue* obj) { - free(obj->stack1); - free(obj->stack2); - free(obj); -} diff --git "a/2224020099/chapter_3/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" "b/2224020099/chapter_3/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" deleted file mode 100644 index 17e950c24110044afa0516a0c22821e211aa9aac..0000000000000000000000000000000000000000 --- "a/2224020099/chapter_3/\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.cpp" +++ /dev/null @@ -1,30 +0,0 @@ -typedef struct { - int queue[10000]; - int top; -} MyStack; - -MyStack* myStackCreate() { - MyStack* obj = (MyStack*)malloc(sizeof(MyStack)); - obj->top = -1; - return obj; -} - -void myStackPush(MyStack* obj, int x) { - obj->queue[++(obj->top)] = x; -} - -int myStackPop(MyStack* obj) { - return obj->queue[(obj->top)--]; -} - -int myStackTop(MyStack* obj) { - return obj->queue[obj->top]; -} - -bool myStackEmpty(MyStack* obj) { - return (obj->top == -1); -} - -void myStackFree(MyStack* obj) { - free(obj); -} diff --git "a/2224020099/chapter_3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" "b/2224020099/chapter_3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" deleted file mode 100644 index 368a3bef16490a1104acb61249aa81b71e63023f..0000000000000000000000000000000000000000 --- "a/2224020099/chapter_3/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include - -int evalRPN(char ** tokens, int tokensSize){ - int *stack = (int *)malloc(sizeof(int) * tokensSize); - int top = -1; - for (int i = 0; i < tokensSize; i++) { - char *token = tokens[i]; - if (strcmp(token, "+") == 0) { - stack[top - 1] += stack[top]; - top--; - } else if (strcmp(token, "-") == 0) { - stack[top - 1] -= stack[top]; - top--; - } else if (strcmp(token, "*") == 0) { - stack[top - 1] *= stack[top]; - top--; - } else if (strcmp(token, "/") == 0) { - stack[top - 1] /= stack[top]; - top--; - } else { - stack[++top] = atoi(token); - } - } - int result = stack[0]; - free(stack); - return result; -} diff --git "a/2224020101/chapter5/\346\201\242\345\244\215IP\345\234\260\345\235\200.cpp" "b/2224020101/chapter5/\346\201\242\345\244\215IP\345\234\260\345\235\200.cpp" deleted file mode 100644 index 3b895885cb94423d409b39117a6a8006d3d433bc..0000000000000000000000000000000000000000 --- "a/2224020101/chapter5/\346\201\242\345\244\215IP\345\234\260\345\235\200.cpp" +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include - -using namespace std; - -void restoreIPAddresses(string s, int start, int step, string address, vector& result) { - if (start == s.size() && step == 4) - result.push_back(address); - return; - if (step >= 4 || start >= s.size()) - return; - for (int len = 1; len <= 3; len++) { - if (start + len > s.size()) - break; - string segment = s.substr(start, len); - int value = stoi(segment); - if (value > 255 || (segment[0] == '0' && segment.size() > 1)) - continue; - string nextAddress = address.empty() ? segment : address + "." + segment; - restoreIPAddresses(s, start + len, step + 1, nextAddress, result); - } -} - -vector restoreIpAddresses(string s) { - vector result; - restoreIPAddresses(s, 0, 0, "", result); - return result; -} - -int main() { - string s = "25525511135"; - vector result = restoreIpAddresses(s); - for (int i = 0; i < result.size(); i++) { - cout << result[i] << endl; - } - return 0; -} \ No newline at end of file diff --git "a/2224020101/chapter5/\347\224\250\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" "b/2224020101/chapter5/\347\224\250\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" deleted file mode 100644 index 0e0d3eee78ee20a97e4fe1c11911a0b4fad0c7b6..0000000000000000000000000000000000000000 --- "a/2224020101/chapter5/\347\224\250\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include -using namespace std; - -bool is_valid(vector& cols, int row, int col) { - for (int i = 0; i < row; i++) { - if (cols[i] == col || abs(cols[i] - col) == abs(i - row)) - return false; - } - return true; -} - -void backtrack(vector& cols, int row, vector>& res) { - if (row == cols.size()) - string board(cols.size(), ' '); - for (int i = 0; i < cols.size(); i++) { - board[cols[i]] = 'Q'; - } - res.push_back(board); - return; - for (int col = 0; col < cols.size(); col++) { - if (is_valid(cols, row, col)) - cols[row] = col; - backtrack(cols, row + 1, res); - cols[row] = -1; -} - -vector> solve_n_queens(int n) { - vector cols(n); - vector> res; - backtrack(cols, 0, res); - return res; -} - -int main() { - int n = 4; - vector> res = solve_n_queens(n); - for (int i = 0; i < res.size(); i++) { - cout << "Solution " << i + 1 << ":" << endl; - cout << res[i][0] << endl; - for (int j = 1; j < res[i].size(); j++) { - cout << res[i][j] << endl; - } - } - return 0; -} \ No newline at end of file diff --git "a/2224020101/chapter5/\351\207\207\347\224\250\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243Hanoi\351\227\256\351\242\230.cpp" "b/2224020101/chapter5/\351\207\207\347\224\250\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243Hanoi\351\227\256\351\242\230.cpp" deleted file mode 100644 index ea994ac5c7c4087211b7d4bb843d66fa24766bf6..0000000000000000000000000000000000000000 --- "a/2224020101/chapter5/\351\207\207\347\224\250\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243Hanoi\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,20 +0,0 @@ -#include -using namespace std; - -// 定义一个递归函数,用于求解汉诺塔问题 -void hanoi(int n, char from_rod, char to_rod, char aux_rod) { - if (n == 1) - cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod << endl; - else - hanoi(n - 1, from_rod, aux_rod, to_rod); - cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << endl; - hanoi(n - 1, aux_rod, to_rod, from_rod); -} - -int main() -{ - int n; - cout << "Enter the number of disks: "; - cin >> n; // 调用递归函数求解汉诺塔问题,其中A表示源杆,C表示目标杆,B表示辅助杆 - return 0; -} \ No newline at end of file diff --git "a/2224020101/chapter6/\345\256\236\347\216\260\347\250\200\347\226\217\347\237\251\351\230\265\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2224020101/chapter6/\345\256\236\347\216\260\347\250\200\347\226\217\347\237\251\351\230\265\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" deleted file mode 100644 index 0ea40bbfbf720048363646e1a0c9e50f6ea2dcea..0000000000000000000000000000000000000000 --- "a/2224020101/chapter6/\345\256\236\347\216\260\347\250\200\347\226\217\347\237\251\351\230\265\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include - -// 定义矩阵结构体 -typedef struct { - int rows; // 矩阵行数 - int cols; // 矩阵列数 - int data[100][100]; // 存储矩阵元素的数组 -} SparseMatrix; - -// 创建稀疏矩阵 -SparseMatrix* createSparseMatrix(int rows, int cols) { - SparseMatrix* mat = (SparseMatrix*)malloc(sizeof(SparseMatrix)); - mat->rows = rows; - mat->cols = cols; - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - mat->data[i][j] = 0; - } - } - return mat; -} - -// 打印稀疏矩阵 -void printSparseMatrix(SparseMatrix* mat) { - printf("Matrix:\n"); - for (int i = 0; i < mat->rows; i++) { - for (int j = 0; j < mat->cols; j++) { - printf("%d ", mat->data[i][j]); - } - printf("\n"); - } -} - -// 稀疏矩阵乘法运算 -SparseMatrix* multiplySparseMatrix(SparseMatrix* mat1, SparseMatrix* mat2) { - SparseMatrix* result = createSparseMatrix(mat1->rows, mat2->cols); - for (int i = 0; i < mat1->rows; i++) { - for (int j = 0; j < mat2->cols; j++) { - for (int k = 0; k < mat1->cols; k++) { - result->data[i][j] += mat1->data[i][k] * mat2->data[k][j]; - } - } - } - return result; -} - -int main() { - // 创建两个稀疏矩阵A和B - SparseMatrix* A = createSparseMatrix(3, 4); - SparseMatrix* B = createSparseMatrix(4, 3); - // 设置矩阵元素的值 - A->data[0][0] = 1; A->data[0][2] = 2; A->data[1][1] = 3; A->data[2][0] = 4; A->data[2][2] = 5; - B->data[0][0] = 6; B->data[1][0] = 7; B->data[2][1] = 8; B->data[2][2] = 9; B->data[2][0] = 10; B->data[1][2] = 11; B->data[0][1] = 12; B->data[0][2] = 13; B->data[0][0] = 14; B->data[1][1] = 15; B->data[1][2] = 16; B->data[2][2] = 17; B->data[2][3] = 18; B->data[3][2] = 19; B->data[3][3] = 20; B->data[3][0] = 21; B->data[3][1] = -3956849784755795544; B->data[3][3] = -3956849784755795544; B->data[3][2] = -3956849784755795544; B->data[3][0] = -3956849784755795544; B->data[3][1] = -3956849784755795544; B->data[2][3] = -3956849784755795544; B->data[2][2] = -3956849784755795544; B->data[2][0] = - \ No newline at end of file diff --git "a/2224020101/chapter6/\346\261\2025\344\271\2305\351\230\266\347\232\204\350\236\272\346\227\213\346\226\271\351\230\265.cpp" "b/2224020101/chapter6/\346\261\2025\344\271\2305\351\230\266\347\232\204\350\236\272\346\227\213\346\226\271\351\230\265.cpp" deleted file mode 100644 index ef8f7e0079e6e92e4b9d46eefd214839f3fbe47d..0000000000000000000000000000000000000000 --- "a/2224020101/chapter6/\346\261\2025\344\271\2305\351\230\266\347\232\204\350\236\272\346\227\213\346\226\271\351\230\265.cpp" +++ /dev/null @@ -1,71 +0,0 @@ -#include - -int main() { - int n = 5; - int matrix[n][n]; - - // 初始化矩阵 - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - matrix[i][j] = 0; - } - } - - // 定义左、上、右、下四个方向的偏移量 - int left_offset = -1, right_offset = 1; - int upper_offset = -1, lower_offset = 1; - - // 定义变量记录当前方向和位置 - int direction = 0; // 0表示向右,1表示向下,2表示向左,3表示向上 - int position = 0; // 当前应该填入的值 - - // 填充螺旋矩阵 - while (position <= n * n) { - // 根据当前方向移动 - switch (direction) { - case 0: // 向右 - for (int i = left_offset + position; i < n && position < n * n; i++) { - matrix[position / n][i] = position % n + 1; - position++; - } - if (position >= n * n) break; // 如果已经填满,跳出循环 - direction = 1; // 改为向下方向 - break; - case 1: // 向下 - for (int i = upper_offset + position / n; i < n && position < n * n; i++) { - matrix[i][position / n] = position % n + 1; - position++; - } - if (position >= n * n) break; // 如果已经填满,跳出循环 - direction = 2; // 改为向左方向 - break; - case 2: // 向左 - for (int i = right_offset + position / n - 1; i >= left_offset && position < n * n; i--) { - matrix[position / n][i] = position % n + 1; - position++; - } - if (position >= n * n) break; // 如果已经填满,跳出循环 - direction = 3; // 改为向上方向 - break; - case 3: // 向上 - for (int i = lower_offset + position % n - 1; i >= upper_offset && position < n * n; i--) { - matrix[i][position / n] = position % n + 1; - position++; - } - if (position >= n * n) break; // 如果已经填满,跳出循环 - direction = 0; // 改为向右方向,继续填充下一行 - break; - } - // 根据当前位置和偏移量更新左、上、右、下四个方向的偏移量 - left_offset++; lower_offset++; right_offset--; upper_offset--; - } - - // 输出结果矩阵 - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - printf("%d ", matrix[i][j]); // 这里假设输出整数,如果输出字符需要将%d改为%c并修改字符类型和赋值方式 - } - printf("\n"); // 每行输出完毕后换行 - } - - return 0; \ No newline at end of file diff --git "a/2224020101/chapter6/\346\261\202\344\270\244\344\270\252\345\257\271\347\247\260\347\237\251\351\230\265\344\271\213\345\222\214\344\270\216\344\271\230\347\247\257.cpp" "b/2224020101/chapter6/\346\261\202\344\270\244\344\270\252\345\257\271\347\247\260\347\237\251\351\230\265\344\271\213\345\222\214\344\270\216\344\271\230\347\247\257.cpp" deleted file mode 100644 index e8f5c58c37307a08c85a79417973e6d570e456d3..0000000000000000000000000000000000000000 --- "a/2224020101/chapter6/\346\261\202\344\270\244\344\270\252\345\257\271\347\247\260\347\237\251\351\230\265\344\271\213\345\222\214\344\270\216\344\271\230\347\247\257.cpp" +++ /dev/null @@ -1,55 +0,0 @@ -#include - -#define N 3 // 定义矩阵的阶数为3 - -void add(int A[][N], int B[][N], int C[][N]) { - for (int i = 0; i < N; i++) { - for (int j = 0; j < N; j++) { - C[i][j] = A[i][j] + B[i][j]; - } - } -} - -void multiply(int A[][N], int B[][N], int C[][N]) { - for (int i = 0; i < N; i++) { - for (int j = 0; j < N; j++) { - C[i][j] = 0; - for (int k = 0; k < N; k++) { - C[i][j] += A[i][k] * B[k][j]; - } - } - } -} - -int main() { - int A[N][N], B[N][N], C[N][N]; // 定义三个N阶矩阵A、B和C - printf("请输入矩阵A:\n"); - for (int i = 0; i < N; i++) { - for (int j = 0; j < N; j++) { - scanf("%d", &A[i][j]); - } - } - printf("请输入矩阵B:\n"); - for (int i = 0; i < N; i++) { - for (int j = 0; j < N; j++) { - scanf("%d", &B[i][j]); - } - } - add(A, B, C); // 求A+B - printf("A+B=\n"); - for (int i = 0; i < N; i++) { - for (int j = 0; j < N; j++) { - printf("%d ", C[i][j]); - } - printf("\n"); - } - multiply(A, B, C); // 求AB - printf("AB=\n"); - for (int i = 0; i < N; i++) { - for (int j = 0; j < N; j++) { - printf("%d ", C[i][j]); - } - printf("\n"); - } - return 0; -} \ No newline at end of file diff --git "a/2224020101/chapter7/\345\256\236\347\216\260\344\272\214\345\217\211\346\240\221\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\342\200\230.cpp" "b/2224020101/chapter7/\345\256\236\347\216\260\344\272\214\345\217\211\346\240\221\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\342\200\230.cpp" deleted file mode 100644 index bbe8172e5280f1b401bec1f086b119c11bf9f74c..0000000000000000000000000000000000000000 --- "a/2224020101/chapter7/\345\256\236\347\216\260\344\272\214\345\217\211\346\240\221\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\342\200\230.cpp" +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include - -// 定义二叉树节点结构体 -struct TreeNode { - int val; - struct TreeNode *left; - struct TreeNode *right; -}; - -// 创建新节点 -struct TreeNode* createNode(int val) { - struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - node->val = val; - node->left = NULL; - node->right = NULL; - return node; -} - -// 插入节点 -struct TreeNode* insertNode(struct TreeNode* root, int val) { - if (root == NULL) { - return createNode(val); - } - if (val < root->val) { - root->left = insertNode(root->left, val); - } else if (val > root->val) { - root->right = insertNode(root->right, val); - } - return root; -} - -// 查找节点 -struct TreeNode* searchNode(struct TreeNode* root, int val) { - if (root == NULL || root->val == val) { - return root; - } - if (val < root->val) { - return searchNode(root->left, val); - } else { - return searchNode(root->right, val); - } -} - -// 删除节点 -struct TreeNode* deleteNode(struct TreeNode* root, int val) { - if (root == NULL) { - return root; - } - if (val < root->val) { - root->left = deleteNode(root->left, val); - } else if (val > root->val) { - root->right = deleteNode(root->right, val); - } else { - if (root->left == NULL) { - struct TreeNode* temp = root->right; - free(root); - return temp; - } else if (root->right == NULL) { - struct TreeNode* temp = root->left; - free(root); - return temp; - } else { - struct TreeNode* minNode = findMinNode(root->right); - root->val = minNode->val; - root->right = deleteNode(root->right, minNode->val); - } - } - return root; -} - -// 查找最小节点(辅助函数) -struct TreeNode* findMinNode(struct TreeNode* node) { - while (node->left != NULL) { - node = node->left; - } - return node; -} \ No newline at end of file diff --git "a/2224020101/chapter7/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\344\273\216\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\350\267\257\345\276\204.cpp" "b/2224020101/chapter7/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\344\273\216\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\350\267\257\345\276\204.cpp" deleted file mode 100644 index d105e7e090261c2f97b6f66f9f439bbeca073f3d..0000000000000000000000000000000000000000 --- "a/2224020101/chapter7/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\344\273\216\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\350\267\257\345\276\204.cpp" +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include - -// 定义二叉树结点结构体 -struct TreeNode { - int val; - struct TreeNode *left; - struct TreeNode *right; -}; - -// 创建二叉树 -struct TreeNode* createTree() { - int val; - scanf("%d", &val); - if (val == -1) { - return NULL; - } - struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - node->val = val; - node->left = createTree(); - node->right = createTree(); - return node; -} - -// 打印从根结点到叶子结点的路径 -void printPath(struct TreeNode* root, int depth) { - if (root == NULL) { - return; - } - for (int i = 0; i < depth; i++) { - printf("%d ", root->val); - } - printf("\n"); - if (root->left == NULL && root->right == NULL) { - printf("Path ended\n"); - return; - } else { - printPath(root->left, depth + 1); - printPath(root->right, depth + 1); - } -} - -// 主函数,测试代码 -int main() { - struct TreeNode* root = createTree(); - printPath(root, 0); - return 0; -} \ No newline at end of file diff --git a/2224020101/lc_7.3.cpp b/2224020101/lc_7.3.cpp deleted file mode 100644 index 8510d131a2b321258e38fcc77fb1dbf9be85bdbf..0000000000000000000000000000000000000000 --- a/2224020101/lc_7.3.cpp +++ /dev/null @@ -1,47 +0,0 @@ -//二叉树的后序遍历 -#include -#include - -// 定义二叉树节点结构体 -struct TreeNode { - int val; - struct TreeNode *left; - struct TreeNode *right; -}; - -// 创建新节点的函数 -struct TreeNode* createNode(int val) { - struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode)); - node->val = val; - node->left = NULL; - node->right = NULL; - return node; -} - -// 后序遍历函数 -void postorderTraversal(struct TreeNode* root) { - if (root == NULL) { - return; - } - postorderTraversal(root->left); - postorderTraversal(root->right); - printf("%d ", root->val); -} - -int main() { - // 创建二叉树 - struct TreeNode* root = createNode(1); - root->left = createNode(2); - root->right = createNode(3); - root->left->left = createNode(4); - root->left->right = createNode(5); - root->right->left = createNode(6); - root->right->right = createNode(7); - - // 后序遍历二叉树并输出结果 - printf("后序遍历结果:"); - postorderTraversal(root); - printf("\n"); - - return 0; -} \ No newline at end of file diff --git "a/2224020102/\345\210\251\347\224\250KMP\347\256\227\346\263\225\346\261\202\345\255\220\344\270\262\345\234\250\344\270\273\344\270\262\344\270\255\344\270\215\351\207\215\345\217\240\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" "b/2224020102/\345\210\251\347\224\250KMP\347\256\227\346\263\225\346\261\202\345\255\220\344\270\262\345\234\250\344\270\273\344\270\262\344\270\255\344\270\215\351\207\215\345\217\240\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" deleted file mode 100644 index 457cad4219510b3c0df9f61a768e17b0f53923ca..0000000000000000000000000000000000000000 --- "a/2224020102/\345\210\251\347\224\250KMP\347\256\227\346\263\225\346\261\202\345\255\220\344\270\262\345\234\250\344\270\273\344\270\262\344\270\255\344\270\215\351\207\215\345\217\240\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" +++ /dev/null @@ -1,81 +0,0 @@ -#include -#include -#include -using namespace std; - -void fail(const char* pat, int* f){ - const int len=strlen(pat); - f[0]=-1; - for (int j=1; j < len; j++){ - if (pat[j] == pat[f[j-1]+1]) - f[j] = f[j-1]+1; - else { - int i=f[j-1]; - while ( i >=0 && pat[j] != pat[i+1] ) - i = f[i]; - if (pat[j] ==pat[i+1]) - f[j] = i+1; - else - f[j] = -1; - } - } -} - -int search(const char* tg, const char *pt, const int* f){ - const int ptLen=strlen(pt); - const int tgLen = strlen(tg); - int tgPos=0,ptPos=0; - for (; tgPos < tgLen && ptPos < ptLen;) { - if (tg[tgPos] == pt[ptPos]){ - ptPos++; - tgPos++; - } - else { - if (ptPos == 0) - tgPos ++; - else - ptPos = f[ptPos-1] + 1; - } - } - return ptPos < ptLen ? -1: tgPos - ptLen; -} - -int count(const char* tg, const char *pt, const int* f){ - const int ptLen=strlen(pt); - const int tgLen = strlen(tg); - int tgPos=0,ptPos=0,count=0; - for (; tgPos < tgLen;) { - if (tg[tgPos] == pt[ptPos]){ - ptPos++; - tgPos++; - } - else { - if (ptPos == 0) - tgPos ++; - else - ptPos = f[ptPos-1] + 1; - } - if (ptPos >= ptLen ){ - count ++; - ptPos=f[ptLen-1] +1; - } - } - return count; -} - -int main() -{ - const char *tag="abcbcbcbc"; - const char *pat="bcbc"; - int len=strlen(pat); - int *f =new int[len]; - fail(pat,f); - cout <<"失效函数位置:\n"; - for(int i=0;i -#include -#include -using namespace std; - -typedef struct polynode -{ - double c; - int e; - struct polynode *next; -}PNode,*Polyn; -Polyn h1, h2; -double a[20]; -int b[20]; -void Read(char *s) -{ - double c; - int i = 0, e; - ifstream infile(s) - if (!infile) - { - cout << "file open error!" << endl; - exit(0); - } - while (1) - { - infile >> c >> e; - if (infile.eof()) - break; - a[i] = c; - b[i] = e; - i++; - } - infile.close(); -} -void Write(Polyn h) -{ - ofstream outfile("multiply.txt"); - Polyn p = h->next; - if (!outfile) - { - cout << "file open error!" << endl; - exit(0); - } - if (!p) - outfile << "0" << endl; - while (p) - { - outfile << p->c << " " << p->e << endl; - p = p->next; - } - outfile.close(); -} - -void clearArray() { - memset(a, 0, sizeof(a)); - memset(b, 0, sizeof(b)); -} - -Polyn Create(char *s) -{ - int i = 0; - Polyn h, last, p; - h = new PNode; - h->next = NULL; - last = h; - clearArray(); - Read(s); - while (a[i]!=0) - { - p = new PNode; - p->c = a[i]; - p->e = b[i]; - p->next = NULL; - last->next = p; - last = p; - i++; - } - return h; -} - -void PrintPoly(Polyn h) -{ - Polyn p = h->next; - if (!p) - cout << "0" << endl; - while (p) - { - if (p->next) - { - if (p->next->c < 0) - { - if (p->c == -1) - { - cout << "-x^" << p->e; - p = p->next; - } - else if (p->c == 1) - { - cout << "x^" << p->e; - p = p->next; - } - else - { - cout << p->c << "x^" << p->e; - p = p->next; - } - } - else if (p->next->c > 0) - { - if (p->c == 1) - { - cout << "x^" << p->e << "+"; - p = p->next; - } - else if (p->c == -1) - { - cout << "-x^" << p->e << "+"; - p = p->next; - } - else - { - cout << p->c << "x^" << p->e << "+"; - p = p->next; - } - } - } - else - { - if (p->c < 0) - { - if (p->c == -1) - { - cout << "-x^" << p->e << endl; - p = p->next; - } - else - { - cout << p->c << "x^" << p->e << endl; - p = p->next; - } - } - else if (p->c > 0) - { - if (p->c == 1) - { - cout << "x^" << p->e << endl; - p = p->next; - } - else - { - cout << p->c << "x^" << p->e << endl; - p = p->next; - } - } - } - } -} -void CreateNode(Polyn &p) -{ - p = new PNode; -} -void DeleteNode(Polyn &p) -{ - delete p; -} -Polyn add(Polyn h1, Polyn h2) -{ - Polyn p1, p2, p3, h, p; - p1 = h1->next; - p2 = h2->next; - CreateNode(h); - p3 = h; - while (p1&&p2) - { - if (p1->e < p2->e) - { - p = p1; - p1 = p1->next; - } - else if (p2->e < p1->e) - { - p = p2; - p2 = p2->next; - } - else - { - p1->c += p2->c; - if (p1->c == 0) - { - p = p1; - p1 = p1->next; - DeleteNode(p); - p = p2; - p2 = p2->next; - DeleteNode(p); - continue; - } - p = p2; - p2 = p2->next; - DeleteNode(p); - p = p1; - p1 = p1->next; - } - p3->next = p; - p3 = p; - } - if (p1) - p3->next = p1; - else if (p2) - p3->next = p2; - else - p3->next = NULL; - h1->next = h2->next = NULL; - return h; -} -Polyn mul(Polyn hp, Polyn hq) -{ - Polyn hr, ht, q, p, pt; - CreateNode(hr); - hr->next = NULL; - CreateNode(ht); - ht->next = NULL; - q = hq->next; - while (q) - { - pt = ht; - p = hp->next; - while (p) - { - CreateNode(pt->next); - pt = pt->next; - pt->c = p->c*q->c; - pt->e = p->e + q->e; - p = p->next; - } - pt->next = NULL; - q = q->next; - p = add(hr, ht); - DeleteNode(hr); - hr = p; - } - DeleteNode(ht); - return hr; -} -int main() -{ - Polyn h1, h2, h3; - cout << "读取文件,直到读入0时停止,建立单向链表" << endl; - h1 = Create("polynode1.txt"); - h2 = Create("polynode2.txt"); - cout << "P(x) = "; - PrintPoly(h1); - cout << "Q(x) = "; - PrintPoly(h2); - h3 = mul(h1, h2); - cout << "R(x) = P(x) * Q(x) = "; - PrintPoly(h3); - Write(h3); - return 0; -} \ No newline at end of file diff --git "a/2224020102/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2224020102/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" deleted file mode 100644 index a9a4e872dc8a620d563a22b595623c565674282c..0000000000000000000000000000000000000000 --- "a/2224020102/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,310 +0,0 @@ -#include -#include -#include -using namespace std; - - - -typedef struct LNode -{ - int data; - struct LNode* next; -}LNode, * LinkList; -LinkList L; - - - -void CreatList_head(LinkList& L, int num1) -{ - cout << "请依次换行输入" << num1 << "个数据!" << endl; - L = (LinkList)malloc(sizeof(LNode)); - L->next = NULL; - LNode* s; - for (int i = 0; i < num1; i++) - { - s = (LNode*)malloc(sizeof(LNode)); - cin >> s->data; - s->next = L->next; - L->next = s; - } - cout << "创建成功。" << endl; -} - - - -void CreatList_tail(LinkList& L, int num1) -{ - cout << "请依次换行输入" << num1 << "个数据" << endl; - L = (LinkList)malloc(sizeof(LNode)); - L->next = NULL; - LNode* s; - LNode* r = L; - for (int i = 0; i < num1; i++) - { - s = (LNode*)malloc(sizeof(LNode)); - cin >> s->data; - r->next = s; - r = s; - } - r->next = NULL; - cout << "创建成功。" << endl; - - - -void showfunction() -{ - cout << "单链表功能展示" << endl; - cout << "1.前插法初始化单链表" << endl; - cout << "2.尾插法初始化单链表" << endl; - cout << "3.输出单链表" << endl; - cout << "4.输出单链表长度" << endl; - cout << "5.输出单链表第a个元素" << endl; - cout << "6.输出元素b的位置" << endl; - cout << "7.指定位置插入单个元素" << endl; - cout << "8.指定位置删除单个元素" << endl; - cout << "9.判断单链表是否为空" << endl; - cout << "0.释放单链表" << endl; -} - -void print(LinkList& L) -{ - LNode* node = L->next; - cout << "表中元素为:"; - if (node == NULL) - { - cout << " 该表为空。" << endl; - } - else - { - while (node != NULL) - { - cout << " " << node->data << " "; - node = node->next; - } - cout << endl; - } -} - -void ListLength(LinkList& L) - int i = 0; - LNode* p = L; - while (p->next != NULL) - { - i++; - p = p->next; - } - cout << "单链表长度为"<next) - { - i = i + 1; - temp = temp->next; - if (elem == temp->data) - { - cout << "该值在链表中的第" << i << "位" << endl; - return 0; - } - } - cout << "查找失败!!!" << endl; - return 0; -} - -void GetElem(LinkList& L, int place) -{ - LNode* node = L; - int j = 0; - while (node && j < place) - { - node = node->next; - j = j + 1; - } - if (!node || j > place) - { - cout << "输入的值有问题!!!" << endl; - } - else - { - cout << "该位置上的值为" << node->data << endl; - } -} - -void Insert(LinkList& L, int place1, int elem) -{ - LNode* p = L; - int j = 0; - while (p && (j < place1 - 1)) - { - p = p->next; - j++; - } - if (p == NULL) - { - cout << "插入失败!!!" << endl; - } - else - { - LNode* s = (LNode*)malloc(sizeof(LNode)); - s->data = elem; - s->next = p->next; - p->next = s; - cout << "插入成功。" << endl; - } -} - -void DeleteElem(LinkList& L, int place3) -{ - LNode* p = L; - int j = 0; - while (p && j < place3 - 1) - { - p = p->next; - j++; - } - if (p == NULL) - { - cout << "删除失败!!!" << endl; - } - else - { - LNode* newone = p->next; - p->next = newone->next; - free(newone); - cout << "删除成功!!!" << endl; - } -} - -void DestroyList(LinkList& L) - - if (NULL ==L->next) - { - delete L; - } - else - { - DestroyList(L->next); - delete L; - } -} - -void ListEmpty(LinkList& L) - { - if (L->next == NULL) - { - cout << "单链表为空" << endl; - } - else - { - cout << "单链表不为空" << endl; - } -} - -int main() -{ - while (true) - { - showfunction(); - int numx; - cout << "选择要使用的功能" << endl; - cin >> numx; - int num1; - int place; - int place1; - int elem1; - int elem2; - int place3; - if (numx > 10) - { - cout << "无该功能,请重新选择" << endl; - } - else - { - switch (numx) - { - case 1: - { - cout << "输入要插入的数据数" << endl; - cin >> num1; - CreatList_head(L, num1); - system("pause"); - system("cls"); - break; - } - case 2: - { - cout << "输入要插入的数据数" << endl; - cin >> num1; - CreatList_tail(L, num1); - system("pause"); - system("cls"); - break; - } - case 3: - { - print(L); - system("pause"); - system("cls"); - break; - } - case 4: - { - ListLength(L); - system("pause"); - system("cls"); - break; - } - case 5: - { - cout << "要输入元素的位置为" << endl; - cin >> place; - GetElem( L, place); - system("pause"); - system("cls"); - break; - } - case 6: - { - cout << "输入要查找的元素" << endl; - cin >> elem1; - Getbyzhi(L, elem1); - system("pause"); - system("cls"); - break; - } - case 7: - { - cout << "输入位置为" << endl; - cin >> place1; - cout << "插入元素为" << endl; - cin >> elem2; - Insert(L, place1, elem2); - system("pause"); - system("cls"); - break; - } - case 8: - { - cout << "输入要删除元素的位置" << endl; - cin >> place3; - DeleteElem(L, place3); - system("pause"); - system("cls"); - break; - } - case 9: - { - ListEmpty(L); - } - case 0: - { - DestroyList(L); - } - } - } - } -} diff --git "a/2224020102/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2224020102/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index 269a96b7d42109dddb60aaf521e4f6738f7361ad..0000000000000000000000000000000000000000 --- "a/2224020102/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,30 +0,0 @@ -#include "sqqueue. cpp" -int main() -{ ElemType e; -SqQueue *q; -printf("环形队列基本运算如下:\n"); -printf(" (1)初始化队列 q\n"); -InitQueue(q); -printf(" (2)依次进队列元素a,b,c\n"); -if(!enQueue(q, 'a')) printf("\t提示:队满,不能进队\n"); -if(!enQueue(q,'b')) printf("\t 提示:队满,不能进队\n"); -if(!enQueue(q,'c')) printf("\t 提示:队满,不能进队\n"); -printf("(3)队列为%s\n",(QueueEmpty(q)?"空":"非空")); -if (deQueue(q,e) == 0) - printf("队空,不能出队\n"); -else - printf(" (4)出队一个元素子c\n",e); -printf(" (5)依次进队列元素 d,e,f\n"); -if(!enQueue(q,'d')) printf("\t 提示:队满,不能进队\n"); -if(!enQueue(q,'e')) printf("\t提示:队满,不能进队\n"); -if(!enQueue(q,'f')) printf("\t提示:队满,不能进队\n"); -printf(" (6)出队列序列:"); -while(!QueueEmpty(q)) -{ deQueue(q,e); -printf("&c ",e); -} -printf("\n"); -printf(" (7)释放队列\n"); -DestroyQueue(q); -return 1; -} \ No newline at end of file diff --git "a/2224020102/\345\256\236\347\216\260\351\241\272\345\272\217\344\270\262\347\232\204\345\220\204\347\247\215\346\250\241\345\274\217\345\214\271\351\205\215\347\256\227\346\263\225.cpp" "b/2224020102/\345\256\236\347\216\260\351\241\272\345\272\217\344\270\262\347\232\204\345\220\204\347\247\215\346\250\241\345\274\217\345\214\271\351\205\215\347\256\227\346\263\225.cpp" deleted file mode 100644 index eda7dc331ad99c12422104c0c5a5802702bbd1a0..0000000000000000000000000000000000000000 --- "a/2224020102/\345\256\236\347\216\260\351\241\272\345\272\217\344\270\262\347\232\204\345\220\204\347\247\215\346\250\241\345\274\217\345\214\271\351\205\215\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,156 +0,0 @@ -#include -#include -#include -using namespace std; -#define MaxSize 100 -int next[MaxSize],nextval[MaxSize]; -typedef struct -{ - char data[MaxSize]; - int length; -} SqString; -void StrAssign(SqString &s,char cstr[]) -{ - int i; - for(i=0; cstr[i]!='\0'; i++) - s.data[i]=cstr[i]; - s.length=i; -} -void DispStr(SqString s) -{ - int i; - for(i=0; i=t.length) - return (i-t.length); - else - return -1; -} -void getnext(SqString t,int next[]) -{ - int i=0,j=-1; - next[0]=-1; - while(i=t.length) - return i-t.length; - else - return -1; -} -void getnextval(SqString t,int next[]) -{ - int i=0,j=-1; - next[0]=-1; - while(i=t.length) - return i-t.length; - else - return -1; -} -int main() -{ - int j; - SqString s,t; - StrAssign(s,"abcabcdabcdeabcdefabcdefg"); - StrAssign(t,"abcdeabcdefab"); - printf("串s:"); - DispStr(s); - printf("\n"); - printf("串t:"); - DispStr(t); - printf("\n"); - printf("t串在s串中的位置=%d\n",BFIndex(s,t)); - getnext(t,next); - getnextval(t,nextval); - printf("j "); - for(j=0;j>a>>b>>c>>d>>e; - Push(s,a); - Push(s,b); - Push(s,c); - Push(s,d); - Push(s,e); - if(StackEmpty(s))printf("空\n"); - else printf("非空\n"); - printf("栈的长度为%d\n",Length(s)); - PrintStack(s); - Print(s); - if(StackEmpty(s))printf("空\n"); - else printf("非空\n"); - DestroyStack(s); - return 0; -} \ No newline at end of file diff --git "a/2224020102/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" "b/2224020102/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" deleted file mode 100644 index 89d95cb5e8ad248d4a84eec23449d0f7dac249eb..0000000000000000000000000000000000000000 --- "a/2224020102/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" +++ /dev/null @@ -1,102 +0,0 @@ -#include "stdio.h" -#include "stdlib.h" -#include "string.h" - -typedef char ElemType; - -typedef struct Node { - ElemType data; - struct Node *next; -}LinkNode; - -void CreateListR(LinkNode *&L, ElemType a[], int n) -{ - LinkNode *node = NULL; - int i = 0; - L = (LinkNode *)malloc(sizeof(LinkNode)); - L->next = NULL; - - LinkNode *p = L; - for(i=0; idata = a[i]; - node->next = NULL; - p->next = node; - p = node; - } -} - -void DispList(LinkNode *L) -{ - LinkNode *p = L->next; - while(p) - { - printf("%c ",p->data); - p = p->next; - } - printf("\n"); -} - -void Split(LinkNode *L, ElemType x) -{ - - LinkNode* pLeftHead = (LinkNode *)malloc(sizeof(LinkNode)); - LinkNode* pRightHead = (LinkNode *)malloc(sizeof(LinkNode)); - - LinkNode* left = pLeftHead; - LinkNode* right = pRightHead; - LinkNode* p = L->next; - while(p != NULL) - { - if (p->data < x) - { - left->next = p; - left = p; - } - else - { - right->next = p; - right = p; - } - p = p->next; - } - - - left->next = pRightHead->next; - right->next = NULL; - - L->next = pLeftHead->next; - - free(pLeftHead); - free(pRightHead); -} - -void DestroyList(LinkNode* L) -{ - LinkNode* node = L->next; - while(L != NULL) - { - node = L->next; - free(L); - L = node; - } -} - -int main() -{ - LinkNode *L = NULL; - ElemType a[] = "fgbedhca"; - int n = strlen(a); - CreateListR(L, a, n); - printf("划分前 L: "); - DispList(L); - ElemType x = 'd'; - printf("以%c进行划分\n", x); - Split(L, x); - printf("划分后 L: "); - DispList(L); - DestroyList(L); - - return 0; -} diff --git "a/2224020102/\346\201\242\345\244\215ip\345\234\260\345\235\200.cpp" "b/2224020102/\346\201\242\345\244\215ip\345\234\260\345\235\200.cpp" deleted file mode 100644 index bf7bc64a6db2b9f11bab8ecee397d2f3e1a81ba9..0000000000000000000000000000000000000000 --- "a/2224020102/\346\201\242\345\244\215ip\345\234\260\345\235\200.cpp" +++ /dev/null @@ -1,51 +0,0 @@ -class Solution { - List result = new ArrayList<>(); - - public List restoreIpAddresses(String s){ - if(s.length() > 12) return result; - backTrack(s, 0, 0); - return result; - } - - private void backTrack(String s, int startIndex, int pointNum){ - if(pointNum == 3){ - if(isValid3(s, startIndex, s.length()-1)){ - result.add(s); - } - return; - } - for(int i = startIndex; i < s.length(); i++){ - if(isValid3(s, startIndex, i)){ - s = s.substring(0, i+1) + "." + s.substring(i + 1); - pointNum++; - backTrack(s, i+2, pointNum); - pointNum--; - s = s.substring(0, i + 1) + s.substring(i + 2); - } else{ - break; - } - } - } - - - private Boolean isValid3(String s, int start, int end){ - if(start > end){ - return false; - } - if(s.charAt(start) == '0' && start != end){ - return false; - } - int num = 0; - for(int i = start; i <= end; i++){ - if(s.charAt(i) > '9' || s.charAt(i) < '0'){ - return false; - } - num = num * 10 + (s.charAt(i) - '0'); - if(num > 255){ - return false; - } - } - return true; - } - -} diff --git "a/2224020102/\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" "b/2224020102/\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" deleted file mode 100644 index 9e3b690d4a39d9f89a45d22fd2991aa4772ac87e..0000000000000000000000000000000000000000 --- "a/2224020102/\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" +++ /dev/null @@ -1,90 +0,0 @@ -#include -#define MaxSize 100 -typedef struct { - char data[MaxSize]; //串中字符 - int length; //串长 -} SqString; //声明顺序串类型 - -void init(SqString &s) -{ - s.length=0; -} - -int insert(SqString &s,char a) -{ - if(s.length>=MaxSize) - return 0;//溢出返回 - s.data[s.length++]=a; -} - -void print(SqString s) -{ - for(int i=0;isub.length) - //这里取不取等号决定了是否为第一个最长重复子串 - { - sub.length=length; - loc=i; - } - j+=length; - } - else j++; - } - } - for(i=loc,j=0;i -#include - -typedef struct Queen{ - int x; - int y; -}Queen; - -typedef struct ListNode{ - Queen q; - struct ListNode *Next; - struct ListNode *Last; -}ListNode; - -typedef struct List { - struct ListNode *header; - struct ListNode *trailer; - int _size; -}List; - -void CreateList(List *l); -void InsertList(List *l,Queen e); -Queen PopList(List *l); -void InsertBefore(int i,Queen e,List *l); - -void CreateList(List *l){ - l->_size=0; - l->header= (ListNode*)malloc(sizeof(ListNode)); - l->trailer=(ListNode*)malloc(sizeof(ListNode)); - l->header->Next=l->trailer; - l->header->Last=NULL; - l->trailer->Last=l->header; - l->trailer->Next=NULL; -} - -void InsertList(List *l,Queen e){ - ListNode *np=(ListNode*)malloc(sizeof(ListNode)); - np->q.x=e.x; - np->q.y=e.y; - np->Next=l->trailer; - np->Last=l->trailer->Last; - l->trailer->Last->Next=np; - l->trailer->Last=np; - l->_size++; -} - -void InsertBefore(int i,Queen e,List *l){ - int j; - ListNode *p=l->header->Next; - for(j=0;jNext; - } - ListNode *np=(ListNode*)malloc(sizeof(ListNode)); - np->q.x=e.x; - np->q.y=e.y; - np->Next=p; - np->Last=p->Last; - p->Last->Next=np; - p->Last=np; - l->_size++; -} - -void PushList(List *l,Queen e){ - ListNode *np=(ListNode*)malloc(sizeof(ListNode)); - np->q.x=e.x; - np->q.y=e.y; - np->Last=l->header; - np->Next=l->header->Next; - np->Next->Last=np; - l->header->Next=np; - l->_size++; -} - -Queen PopList(List *l){ - ListNode *now=l->header->Next; - Queen old=now->q; - now->Next->Last=now->Last; - l->header->Next=now->Next; - free(now); - l->_size--; - return old; -} - -int placeQ(int N){ - int solution_n = 0; - int i; - List solution; - CreateList(&solution); - Queen q; - q.x=0; - q.y=0; - int *xarray=(int*)calloc(N,sizeof(int)); - int *yarray=(int*)calloc(N,sizeof(int)); - int *sumarray=(int*)calloc(2*N,sizeof(int)); - int *diffarray=(int*)calloc(2*N,sizeof(int)); - for(i=0;i q.y) { - PushList(&solution, q); - xarray[q.x] = 1; - yarray[q.y] = 1; - sumarray[q.x + q.y] = 1; - diffarray[q.x - q.y + N] = 1; - if (N <= solution._size) { - solution_n++; - } - q.x++; - q.y = 0; - } - } - } - while((q.x>0)||(q.y -#define max 50 -int number = 0; -int p[max]; - -void trys(int col,int ma,bool s[],bool l[], bool r[]) -{ - if(col == ma) - { - number++; - printf("\n方案%d:",number); - for(int k =1; k - -void move(int n, char pos1, char pos3) -{ - - printf("盘子%d: 从 %c柱 移动到 %c柱\n", n, pos1, pos3); - -} - -void Hanoi(int n, char pos1, char pos2, char pos3) -{ - - if (n == 1) - { - move(n, pos1, pos3); - } - else - { - Hanoi(n-1, pos1, pos3, pos2); - move(n, pos1, pos3); - Hanoi(n-1, pos2, pos1, pos3); - } -} - -int main() -{ - int m; - printf("input the number of disks:"); - scanf("%d",&m); - char pos1 = 'A'; - char pos2 = 'B'; - char pos3 = 'C'; - printf("移动%d个盘子的步骤如下↓\n", n); - Hanoi(n, pos1, pos2, pos3); - return 0; -} - - diff --git "a/2224020103/chap3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2224020103/chap3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index e7d3931b800ecc7a42ce4e7130c5443e86e926f1..0000000000000000000000000000000000000000 --- "a/2224020103/chap3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,87 +0,0 @@ -#include -#include -#include - -#define MAX_SIZE 100 - -typedef char ElemType; -typedef struct -{ - ElemType data[MAX_SIZE]; - int queue_front; - int queue_rear; -}SeqQueue; - -static void init_queue(SeqQueue *&q) -{ - q = (SeqQueue *)malloc(sizeof(SeqQueue)); - q->queue_front = q->queue_rear = 0; -} - -static void destroy_queue(SeqQueue *&q) -{ - free(q); -} - -static bool queue_empty(SeqQueue *q) -{ - return (q->queue_front == q->queue_rear); -} - -static bool enter_queue(SeqQueue *&q, ElemType e) -{ - if((q->queue_rear + 1) % MAX_SIZE == q->queue_front) - return false; - - q->queue_rear = (q->queue_rear + 1) % MAX_SIZE; - q->data[q->queue_rear] = e; - - return true; -} - -static bool de_queue(SeqQueue *&q, ElemType &e) -{ - if(q->queue_front == q->queue_rear) - return false; - - e = q->data[q->queue_front]; //正确返回出队元素 - q->queue_front = (q->queue_front + 1) % MAX_SIZE; - - return true; -} - -int main(int argc, char *argv[]) -{ - ElemType e; - SeqQueue *q; - - printf("环形队列基本运算如下:\n"); - printf(" (1)初始化队列q\n"); - init_queue(q); - printf(" (2)依次进队元素a、b、c\n"); - enter_queue(q, 'a'); - enter_queue(q, 'b'); - enter_queue(q, 'c'); - printf(" (3)队列为%s\n", (queue_empty(q) ? "空" : "非空")); - if(!de_queue(q, e)) - printf("队空,不能出队\n"); - else - printf(" (4)出队一个元素%c\n", e); - - printf(" (5)依次进队元素d、e、f\n"); - enter_queue(q, 'd'); - enter_queue(q, 'e'); - enter_queue(q, 'f'); - - printf(" (6)出队列序列: "); - while(!queue_empty(q)) - { - de_queue(q, e); - printf("%c ", e); - } - printf("\n"); - printf(" (7)释放队列\n"); - destroy_queue(q); - - return 0; -} \ No newline at end of file diff --git "a/2224020103/chap3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2224020103/chap3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index 4ac93a3c4e4484aff3b2c9f31ae82313d25ef4ce..0000000000000000000000000000000000000000 --- "a/2224020103/chap3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include -#define MaxSize 50 -typedef char ElemType; -char w; -typedef struct -{ - ElemType data[MaxSize]; - int top; -}SqStack; -void InitStack(SqStack* &s) -{ - s=(SqStack*)malloc(sizeof(SqStack)); - s->top=-1; -} -bool StackEmpty(SqStack*s) -{ - return(s->top==-1); -} -bool Push(SqStack* &s,ElemType e) -{ - if(s->top==MaxSize-1) - return false; - if(s->top==-1) - return false; - s->top++; - s->data[s->top]=e; - return true; -} -bool Pop(SqStack* &s,ElemType &e) -{ - if(s->top==-1) - return false; - e=s->data[s->top]; - s->top--; - return true; -} -void DestroyStack(SqStack* &s) -{ - free(s); -} -int main() -{ - SqStack *s; - printf("顺序栈的各种基本运算的算法: \n"); - - printf("(1)初始化栈s \n"); - InitStack(s); - printf("(2)判断栈s是否非空: %s\n",(StackEmpty(s)?"空":"非空")); - - printf("(3)依次进栈元素a、b、c、d、e \n"); - Push(s,'a');Push(s,'b');Push(s,'c');Push(s,'d');Push(s,'e'); - - printf("(4)判断栈s是否非空: %s\n",(StackEmpty(s)?"空":"非空")); - - printf("(5)输出出栈序列: ");while(!StackEmpty(s))Pop(s,w); printf("%c",w); printf("\n"); printf("(6)判断栈s是否非空: %s\n",(StackEmpty(s)?"空":"非空"));DestroyStack(s); return 0; } \ No newline at end of file diff --git "a/2224020103/chap3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" "b/2224020103/chap3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" deleted file mode 100644 index f80cf69dcc9cd9d516e8f028d23a5377920821ac..0000000000000000000000000000000000000000 --- "a/2224020103/chap3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include -using namespace std; - -#define maxn 20 -int n, num = 1; - -typedef struct node { - int col[maxn]; // 记录每列皇后所在的行 - int row; // 已经放置的皇后数 -} stacktype; - -void display(stacktype st) { - printf(" 第%d个解为:", num++); - for (int i = 1; i <= n; i++) - printf("(%d,%d) ", st.col[i], i); - printf("\n"); -} - -bool place(stacktype st, int k) { - for (int i = 1; i < k; i++) { - if (st.col[i] == st.col[k] || abs(st.col[i] - st.col[k]) == abs(i - k)) - return false; - } - return true; -} - -void queen(int n) { - stacktype st; - st.row = 1; - st.col[1] = 0; // 从第一行的第一列开始放置 - bool find; - while (st.row != 0) { - find = false; - for (int j = st.col[st.row] + 1; j <= n; j++) { - st.col[st.row] = j; - if (place(st, st.row)) { - find = true; - break; - } - } - if (find) { - if (st.row == n) { - display(st); - st.row--; - } else { - st.row++; - st.col[st.row] = 0; - } - } else { - st.row--; - if(st.row == 0 && st.col[st.row] == n) num = 1; // reset num if no solution for current n - } - } - if(num == 1) printf(" 此%d皇后问题无解!\n", n); -} - -int main() { - printf("n皇后问题(n<=20)求解:n="); - scanf("%d", &n); - if (n > 20) printf("n必须小于或等于20\n"); - else { - printf(" %d皇后问题的求解情况如下:\n", n); - queen(n); - } - return 0; -} \ No newline at end of file diff --git "a/2224020104/8.\345\256\236\351\252\2148\357\274\232\351\207\207\347\224\250\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225\346\261\202\345\270\246\346\235\203\346\234\211\345\220\221\345\233\276\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" "b/2224020104/8.\345\256\236\351\252\2148\357\274\232\351\207\207\347\224\250\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225\346\261\202\345\270\246\346\235\203\346\234\211\345\220\221\345\233\276\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" deleted file mode 100644 index a8c9a32ce01738b95ab576f49a7350d2317e513e..0000000000000000000000000000000000000000 --- "a/2224020104/8.\345\256\236\351\252\2148\357\274\232\351\207\207\347\224\250\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225\346\261\202\345\270\246\346\235\203\346\234\211\345\220\221\345\233\276\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" +++ /dev/null @@ -1,57 +0,0 @@ -#include -#define V 4 -#define INF 65535 -int P[V][V] = { 0 }; -void printMatrix(int matrix[][V]); -void printPath(int i, int j); -void floydWarshall(int graph[][V]); -int main() { - int graph[V][V] = { {0, 3, INF, 5}, - {2, 0, INF, 4}, - {INF, 1, 0, INF}, - {INF, INF, 2, 0} }; - floydWarshall(graph); -} -void printPath(int i, int j) -{ - int k = P[i][j]; - if (k == 0) - return; - printPath(i, k); - printf("%d-", k + 1); - printPath(k, j); -} -void printMatrix(int graph[][V]) { - int i, j; - for (i = 0; i < V; i++) { - for (j = 0; j < V; j++) { - if (j == i) { - continue; - } - printf("%d - %d: 最短路径为:", i + 1, j + 1); - if (graph[i][j] == INF) - printf("%s\n", "INF"); - else { - printf("%d", graph[i][j]); - printf(",依次经过:%d-", i + 1); - printPath(i, j); - printf("%d\n", j + 1); - } - } - } -} -void floydWarshall(int graph[][V]) { - int i, j, k; - for (k = 0; k < V; k++) { - for (i = 0; i < V; i++) { - for (j = 0; j < V; j++) { - if (graph[i][k] + graph[k][j] < graph[i][j]) { - graph[i][j] = graph[i][k] + graph[k][j]; - P[i][j] = k; - } - } - } - } - printMatrix(graph); -} - diff --git "a/2224020104/8.\345\256\236\351\252\214\351\242\2301\357\274\232\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.cpp" "b/2224020104/8.\345\256\236\351\252\214\351\242\2301\357\274\232\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.cpp" deleted file mode 100644 index 2d91dc71f41f728c2fb994ed7b65a9e66b88996a..0000000000000000000000000000000000000000 --- "a/2224020104/8.\345\256\236\351\252\214\351\242\2301\357\274\232\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.cpp" +++ /dev/null @@ -1,145 +0,0 @@ -#include -#include -#define INF 32767 -#define Vmax 100 -typedef char VType; -typedef int EType; - -typedef struct -{ - VType vexs[Vmax]; - EType edges[Vmax][Vmax]; - int Vnum, Enum; -}MGraph; -typedef struct node -{ - EType adjvex; - struct node *next; - int weight; -}EdgeNode; -typedef struct -{ - VType vertex; - EdgeNode *firstedge; -}VertexNode; -typedef struct -{ - VertexNode vexlist[Vmax]; - int vexnum,arcnum; -}AdjGraph; -/*创建邻接矩阵*/ -void creatGraph(MGraph &G) -{ - int i,j,from,to; - printf("请输入顶点的个数和边的条数:\n"); - scanf("%d %d",&G.Vnum,&G.Enum); - getchar(); - printf("请输入顶点:\n"); - for(i=0;ivexlist[i].firstedge=NULL; - for(i=0;i=0;j--) - if(G.edges[i][j]!=INF) - { - p=(EdgeNode *)malloc(sizeof(EdgeNode)); - p->adjvex=j; - p->next=g->vexlist[i].firstedge; - g->vexlist[i].firstedge=p; - } - g->vexnum=G.Vnum; G.Enum=g->arcnum; - printf("图G的邻接表:\n"); - for(i=0;ivexnum;i++) - { - p=g->vexlist[i].firstedge; - printf("%c->",G.vexs[i]); - while(p!=NULL) - { - printf("%d->",p->adjvex); - p=p->next; - } - printf("NULL\n"); - } -} - -void DestroyAdj(AdjGraph *&G) -{ int i; - EdgeNode *pre,*p; - for (i=0;ivexnum;i++) - { pre=G->vexlist[i].firstedge; - if (pre!=NULL) - { p=pre->next; - while (p!=NULL) - { free(pre); - pre=p; p=p->next; - } - free(pre); - } - } - free(G); -} - -int main() -{ - MGraph G; - AdjGraph *g; - creatGraph(G); - - printf("将g转换为邻接表G\n"); - MatToList(G,g); - DestroyAdj(g); -return 1; -} - - - - \ No newline at end of file diff --git "a/2224020104/\345\256\236\351\252\214\351\242\23014\357\274\232\346\261\202\350\247\243\345\205\254\350\267\257\351\227\256\351\242\230.cpp" "b/2224020104/\345\256\236\351\252\214\351\242\23014\357\274\232\346\261\202\350\247\243\345\205\254\350\267\257\351\227\256\351\242\230.cpp" deleted file mode 100644 index 8be7fc68255594dd3e409f1fa8a636af065e1db0..0000000000000000000000000000000000000000 --- "a/2224020104/\345\256\236\351\252\214\351\242\23014\357\274\232\346\261\202\350\247\243\345\205\254\350\267\257\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include - -using namespace std; - -const int MAXN = 105; -const int MAXM = 10005; - -struct Edge { - int u, v, w; -} e[MAXM]; - -int f[MAXN]; - -bool cmp(Edge a, Edge b) { - return a.w < b.w; -} - -int find(int x) { - if (f[x] == x) return x; - return f[x] = find(f[x]); -} - -int main() { - int c, n, m; - while (cin >> c >> n >> m) { - if (c == 0 && n == 0 && m == 0) break; - for (int i = 1; i <= m; i++) { - cin >> e[i].u >> e[i].v >> e[i].w; - } - sort(e + 1, e + m + 1, cmp); - for (int i = 1; i <= n; i++) { - f[i] = i; - } - int ans = 0, cnt = 0; - for (int i = 1; i <= m; i++) { - int u = e[i].u, v = e[i].v, w = e[i].w; - int fu = find(u), fv = find(v); - if (fu != fv) { - f[fu] = fv; - ans += w; - cnt++; - } - if (cnt == n - 1) break; - } - if (ans <= c) { - cout << "Yes" << endl; - } else { - cout << "No" << endl; - } - } - return 0; -} \ No newline at end of file diff --git "a/2224020104/\347\254\2547\345\215\225\345\205\203\357\274\232\345\256\236\351\252\214\351\242\2301\357\274\232\345\256\236\347\216\260\344\272\214\345\217\211\346\240\221\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2224020104/\347\254\2547\345\215\225\345\205\203\357\274\232\345\256\236\351\252\214\351\242\2301\357\274\232\345\256\236\347\216\260\344\272\214\345\217\211\346\240\221\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index 5f7e9e480f82e7e2063f33afbbeef462b22885ae..0000000000000000000000000000000000000000 --- "a/2224020104/\347\254\2547\345\215\225\345\205\203\357\274\232\345\256\236\351\252\214\351\242\2301\357\274\232\345\256\236\347\216\260\344\272\214\345\217\211\346\240\221\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,125 +0,0 @@ -#include -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct node -{ - ElemType data; - struct node *lchild; - struct node *rchild; - -}BTNode; -void CreateBTree(BTNode *&b,char *str) -{ - BTNode * St[MaxSize],*p; - int top=-1,k,j=0;char ch; - b=NULL; - ch=str[j]; - while(ch!='\0') - { - switch(ch) - { - case'(':top++;St[top]=p;k=1;break; - case')':top--;break; - case',':k=2;break; - - default:p=(BTNode *)malloc(sizeof(BTNode)); - p->data=ch;p->lchild=p->rchild=NULL; - if(b==NULL) - b=p; - else - { - switch(k) - { - case 1:St[top]->lchild=p;break; - case 2:St[top]->rchild=p;break; - } - } - } - j++;ch=str[j]; - } -} -void DestroyBTree(BTNode *&b) -{ - if(b!=NULL) - { - DestroyBTree(b->lchild); - DestroyBTree(b->rchild); - free(b); - } -} -BTNode *FindNode(BTNode *b,ElemType x) -{ - BTNode *p; - if(b==NULL) - return NULL; - else if(b->data==x) - return b; - else - { - p=FindNode(b->lchild,x); - if(p!=NULL) - return p; - else - return FindNode(b->rchild,x); - } -} -BTNode *LchildNode(BTNode *p) -{ - return p->lchild; - -} -BTNode *RchildNode(BTNode *p) -{ - return p->rchild; -} -int BTHeight(BTNode *b) -{ - int lchildh,rchildh; - if(b==NULL)return(0); - else - { - lchildh=BTHeight(b->lchild); - rchildh=BTHeight(b->rchild); - return(lchildh>rchildh)?(lchildh+1):(rchildh+1); - } -} -void DispBTree(BTNode *b) -{ - if(b!=NULL) - { - printf("%c",b->data); - if(b->lchild!=NULL||b->rchild!=NULL) - { - printf("("); - DispBTree(b->lchild); - if(b->rchild!=NULL)printf(","); - DispBTree(b->rchild); - printf(")"); - } - } -} -int main() -{ - BTNode *b,*p,*lp,*rp;; - printf("二叉树的基本运算如下:\n"); - printf(" (1)创建二叉树\n"); - CreateBTree(b,"A(B(D,E(H(J,k(L,M(,N))))),C(F,G(,I)))"); - printf(" (2)输出二叉树:");DispBTree(b);printf("\n"); - printf(" (3)H结点:"); - p=FindNode(b,'H'); - if(p!=NULL) - { lp= LchildNode(p); - if(lp!=NULL)printf("左孩子为%c",lp->data); - else printf("无左孩子"); - rp=RchildNode(p); - if(rp!=NULL) printf("右孩子为%c",rp->data); - else printf("无右孩子"); - - } - printf("\n"); - printf(" (4)二叉树b的高度:%d\n",BTHeight(b)); - printf(" (5)释放二叉树b\n"); - DestroyBTree(b); - return 1; -} diff --git "a/2224020104/\347\254\254\344\270\203\345\215\225\345\205\203\357\274\232\345\256\236\351\252\214\351\242\2307\357\274\232\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\350\267\257\345\276\204.cpp" "b/2224020104/\347\254\254\344\270\203\345\215\225\345\205\203\357\274\232\345\256\236\351\252\214\351\242\2307\357\274\232\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\350\267\257\345\276\204.cpp" deleted file mode 100644 index fec9181dbab7adde58a6d61684cc36f5b0ef2e8a..0000000000000000000000000000000000000000 --- "a/2224020104/\347\254\254\344\270\203\345\215\225\345\205\203\357\274\232\345\256\236\351\252\214\351\242\2307\357\274\232\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\350\267\257\345\276\204.cpp" +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include -using namespace std; - -struct TreeNode { - int val; - TreeNode* left; - TreeNode* right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -void binaryTreePathsHelper(TreeNode* root, vector& path, vector>& res) { - if (!root) { - return; - } - path.push_back(root->val); - if (!root->left && !root->right) { - res.push_back(path); - } else { - binaryTreePathsHelper(root->left, path, res); - binaryTreePathsHelper(root->right, path, res); - } - path.pop_back(); -} - -vector> binaryTreePaths(TreeNode* root) { - vector> res; - vector path; - binaryTreePathsHelper(root, path, res); - return res; -} - -int main() { - TreeNode* root = new TreeNode(1); - root->left = new TreeNode(2); - root->right = new TreeNode(3); - root->left->right = new TreeNode(5); - - vector> res = binaryTreePaths(root); - - for (int i = 0; i < res.size(); i++) { - for (int j = 0; j < res[i].size(); j++) { - cout << res[i][j] << "->"; - } - cout << "NULL" << endl; - } \ No newline at end of file diff --git "a/2224020105/\347\254\254\344\270\211\347\253\240/\345\256\236\351\252\214\344\270\200.cpp" "b/2224020105/\347\254\254\344\270\211\347\253\240/\345\256\236\351\252\214\344\270\200.cpp" index d0399af9611061d62e729170fb3bb60145609a83..bf70595b7f3e40d15d776a916ff80c41efdff784 100644 --- "a/2224020105/\347\254\254\344\270\211\347\253\240/\345\256\236\351\252\214\344\270\200.cpp" +++ "b/2224020105/\347\254\254\344\270\211\347\253\240/\345\256\236\351\252\214\344\270\200.cpp" @@ -43,4 +43,4 @@ bool GetTop(SqStack * s,ElemType &e) return false; e = s -> data[s -> top]; return true; -} \ No newline at end of file +} diff --git "a/2224020105/\347\254\254\344\270\211\347\253\240/\345\256\236\351\252\214\344\270\211.cpp" "b/2224020105/\347\254\254\344\270\211\347\253\240/\345\256\236\351\252\214\344\270\211.cpp" index b43e6d4a6333b9b71ecf2f951a462416b3ef504a..1226e85235546941fb74d3cc2c33277573d0cc1e 100644 --- "a/2224020105/\347\254\254\344\270\211\347\253\240/\345\256\236\351\252\214\344\270\211.cpp" +++ "b/2224020105/\347\254\254\344\270\211\347\253\240/\345\256\236\351\252\214\344\270\211.cpp" @@ -36,4 +36,4 @@ bool deQueue(SqQueue * &q,ElemType &e) q -> front = (q -> front +1 )%MaxSize; e= q -> data[q -> front]; return true; -} +} \ No newline at end of file diff --git "a/2224020105/\347\254\254\344\270\211\347\253\240/\345\256\236\351\252\214\345\205\253.cpp" "b/2224020105/\347\254\254\344\270\211\347\253\240/\345\256\236\351\252\214\345\205\253.cpp" index 913b06ab099113e488a3b80a59db96f1f9a5b86b..ae71350d57191a3ec3561701dabafa8bf94ca9b3 100644 --- "a/2224020105/\347\254\254\344\270\211\347\253\240/\345\256\236\351\252\214\345\205\253.cpp" +++ "b/2224020105/\347\254\254\344\270\211\347\253\240/\345\256\236\351\252\214\345\205\253.cpp" @@ -35,4 +35,4 @@ int main() { scanf("%d", &n); backtrack(0); return 0; -} +} \ No newline at end of file diff --git "a/2224020105/\347\254\254\344\272\224\347\253\240/\345\256\236\351\252\214\344\270\200.cpp" "b/2224020105/\347\254\254\344\272\224\347\253\240/\345\256\236\351\252\214\344\270\200.cpp" deleted file mode 100644 index bf07c20e031f9b567a844fb3fa5db91ffd0fb664..0000000000000000000000000000000000000000 --- "a/2224020105/\347\254\254\344\272\224\347\253\240/\345\256\236\351\252\214\344\270\200.cpp" +++ /dev/null @@ -1,98 +0,0 @@ -//采用递归和非递归方法求解Hanoi问题// -# include -# include -# define MaxSize 100 -//递归法// -void Hanoi1(int n,char a,char b,char c) -{ - if (n==1) - printf ("\t将第%d个盘片从%c移动到%c\n",n,a,c); - else - { - Hanoi1(n-1,a,c,b); - printf ("\t将第%d个盘片从%c移动到%c\n",n,a,c); - Hanoi1(n-1,b,a,c); - } -} -//非递归法// -typedef struct -{ - int n; - char x,y,z; - bool flag; -} ElemType; -typedef struct -{ - ElemType data[MaxSize]; - int top; -} StackType; - -void InitStack(StackType *&s) -{ - s=(StackType * )malloc(sizeof(StackType)); - s - > top = -1; -} -void DestroyStack(StackType * &s) -{ - free(s); -} -bool StackEmpty(StackType * s) -{ - return(s - > top == MaxSize -1); -} -bool Push(StackType * &s,ElemType e) -{ - if (s - > top==MaxSize - 1) - return true; -} -bool Pop(StackType * &s,ElemType &e) -{ - if (s - > top == -1) - return false; - s - > top --; - return true; -} -void Hanoi2(int n,char x,char y,char z) -{ - StackType * st; - ElemType e,e1,e2,e3; - if (n<=0) return; - InitStack(st); - e.n = n; e.x = x; e.y = y; e.z = z; e.flag = false; - Push(st,e); - while (!StackEmpty(st)) - { - Pop(st,e); - if (e.flag == false) - { - e1.n = e.n - 1; e1.x = e.y; e1.y = e.x; e1.z = e.z; - if(e1.n == 1) - e1.flag = true; - else - e1.flag = false; - Push(st,e1); - e2.n = e.n; e2.x = e.x; e2.y = e.y; e2.z = e.z; e2.flag = true; - Push(st,e2); - e3.n = e.n - 1;e3.x = e.x; e3.y = e.z; e3.z = e.y; - if(e3.n == 1) - e3.flag = true; - else - e3.flag = false; - Push(st,e3); - } - else - printf("\t将第%d个盘片从%c移动到%c\n",e.n,e.x,e.z); - } - DestroyStack(st); -} - - -int main() -{ - int n = 3; - printf("递归算法:%d个盘片移动过程:\n",n); - Hanoi1(n,'x','y','z'); - printf("非递归算法:%d个盘片移动过程:\n",n);\ - Hanoi2(n,'x','y','z'); - return 1; -} \ No newline at end of file diff --git "a/2224020105/\347\254\254\344\272\224\347\253\240/\345\256\236\351\252\214\344\270\203.cpp" "b/2224020105/\347\254\254\344\272\224\347\253\240/\345\256\236\351\252\214\344\270\203.cpp" deleted file mode 100644 index e2803e67606505b182b788838ba8508d9fe5646b..0000000000000000000000000000000000000000 --- "a/2224020105/\347\254\254\344\272\224\347\253\240/\345\256\236\351\252\214\344\270\203.cpp" +++ /dev/null @@ -1,56 +0,0 @@ -//用递归方法求解n皇后问题// -#include -#include -const int N = 20; -int q[N]; -int count = 0; -void print(int n) -{ - count++; - int i; - printf("第%d个解:",count); - for (i=1;i<=n;i++) - printf("(%d,%d)",i,q[i]); - printf("\n"); -} -bool place(int k,int j) -{ - int i = 1; - while (in) - print(n); - else - for(j=1;j<=n;j++) - if(place(k,j)) - { - q[k]=j; - queen(k+1,n); - } -} - - -int main() -{ - int n; - printf("皇后问题(n<20) n:"); - scanf("%d",&n); - if (n>20) - printf("n值太大,不能求解\n"); - else - { - printf("%d皇后问题求解如下:\n",n); - queen(1,n); - printf("\n"); - } - return 1; -} \ No newline at end of file diff --git "a/2224020105/\347\254\254\344\272\224\347\253\240/\345\256\236\351\252\214\344\270\211.cpp" "b/2224020105/\347\254\254\344\272\224\347\253\240/\345\256\236\351\252\214\344\270\211.cpp" deleted file mode 100644 index 92ddbe77105c729eb888fa0bfefbb134f3fbfcb1..0000000000000000000000000000000000000000 --- "a/2224020105/\347\254\254\344\272\224\347\253\240/\345\256\236\351\252\214\344\270\211.cpp" +++ /dev/null @@ -1,52 +0,0 @@ -//恢复IP地址// -# include -# define MaxSize 100 -typedef struct -{ - char data[MaxSize]; - int length; -}IP; -void addch(IP &ip,char ch) -{ - ip.data[ip.length] = ch; - ip.length++; -} -IP addot(IP ip) -{ - addch(ip,'.'); - return ip; -} -void solveip(char s[],int n,int start,int step,IP ip) -{ - if(start <= n) - { - if (start == n && step == 4) - { - for (int i = 0; i < ip.length; i++) - printf("%c",ip.data); - printf("\n"); - } - } - int num = 0; - for (int i = start;i -# define N 4 -typedef int ElemType; -# define MaxSize 100 -typedef struct -{ - int r; - int c; - ElemType d; -} TupNode; -typedef struct -{ - int rows; - int cols; - int nums; - TupNode data[MaxSize]; -} TSMatrix; -void CreatMat(TSMatrix &t,ElemType A[N][N]) -{ - int i,j; - t.rows=N;t.cols=N;t.nums=0; - for (i=0;ib.data[j].c) - { - c.data[k].r=a.data[j].r; - c.data[k].c=a.data[j].c; - c.data[k].d=a.data[j].d; - k++;j++; - } - else - { - v=a.data[i].d+b.data[j].d; - if (v!=0) - { - c.data[k].r=a.data[i].r; - c.data[k].c=a.data[i].c; - c.data[k].d=v; - k++; - } - i++;j++; - } - } - else if(a.data[i].r -# define MaxLen 10 -void fun(int a[MaxLen][MaxLen],int n) -{ - int i,j,k=0,m; - if (n%2==0) m=n/2; - else m=n/2 +1; - for (i=0;i=i;j--) - { - k++; - a[n-i-1][j]=k; - } - for (j=n-i-2;j>=i+1;j--) - { - k++; - a[j][i]=k; - } - } -} -int main() -{ - int n,i,j; - int a[MaxLen][MaxLen]; - printf("输入n(n<10):"); - scanf("%d",&n); - fun(a,n); - printf("%d阶数字方阵如下:\n",n); - for (i=0;i -# define N 4 -# define M 10 -int value(int a[],int i,int j) -{ - if (i>=j) - return a[(i*(i-1))/2+j]; - else - return a[(j*(j-1))/2+i]; -} -void madd(int a[],int b[],int c[][N]) -{ - int i,j; - for (i=0;i -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct -{ - ElemType data[MaxSize]; - int top; -}SqStack; -void InitStack(SqStack *&s) -{ - s=(SqStack *)malloc(sizeof(SqStack)); - s->top=-1; -} -void DestroyStack(SqStack *&s) -{ - free(s); -} -bool StackEmpty(SqStack *s) -{ - return(s->top==-1); -} -bool Push(SqStack *&s,ElemType e) -{ - if(s->top==MaxSize-1) - return false; - s->top++; - s->data[s->top]=e; - return true; -} -bool Pop(SqStack *&s,ElemType &e) -{ - if(s->top == -1) - return false; - e=s->data[s->top]; - s->top--; - return true; -} -bool GetTop(SqStack *s,ElemType &e) -{ - if(s->top==-1) - return false; - e=s->data[s->top]; - return true; -} -int main() -{ - ElemType e; - SqStack *s; - printf("顺序栈s的基本运算如下:\n"); - printf("(1)初始化栈s\n"); - InitStack(s); - printf("(2)栈为%s\n",(StackEmpty(s)?"空":"非空")); - printf("(3)依次进栈元素a,b,c,d,e\n"); - Push(s,'a'); - Push(s,'b'); - Push(s,'c'); - Push(s,'d'); - Push(s,'e'); - printf("(4)栈为%s\n",StackEmpty(s)?"空":"非空"); - printf("(5)出栈序列:"); - while(!StackEmpty(s)) - { - Pop(s,e); - printf("%c",e); - } - printf("\n"); - printf("(6)栈为%s\n",(StackEmpty(s)?"空":"非空")); - printf("(7)释放栈\n"); - DestroyStack(s); - return 1; -} \ No newline at end of file diff --git a/2224020106/chapter3/exp3.3.cpp b/2224020106/chapter3/exp3.3.cpp deleted file mode 100644 index c6a1b4c92e9085096f4d0d99fbdbbf457af44f75..0000000000000000000000000000000000000000 --- a/2224020106/chapter3/exp3.3.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -#define Maxsize 100 -typedef char ElemType; -typedef struct -{ - ElemType data[Maxsize]; - int front,rear; -}SqQueue; -void InitQueue(SqQueue *&q) -{ - q=(SqQueue *)malloc(sizeof(SqQueue)); - q->front=q->rear=0; -} -void DestroyQueue(SqQueue *&q) -{ - free(q); -} -bool QueueEmpty(SqQueue *q) -{ - return(q->front==q->rear); -} -bool enQueue(SqQueue *&q,ElemType e) -{ - if((q->rear+1)%Maxsize==q->front) - return false; - q->rear=(q->rear+1)%Maxsize; - q->data[q->rear]=e; - return true; -} -bool deQueue(SqQueue *&q,ElemType &e) -{ - if(q->front==q->rear) - return false; - q->front=(q->front+1)%Maxsize; - e=q->data[q->front]; - return true; -} -int main() -{ - ElemType e; - SqQueue *q; - printf("环形队列基本运算如下:\n"); - printf("(1)初始化队列q\n"); - InitQueue(q); - printf("(2)依次进队列元素a,b,c\n"); - if(!enQueue(q,'a'))printf("\t提示:队满,不能进队\n"); - if(!enQueue(q,'b'))printf("\t提示:队满,不能进队\n"); - if(!enQueue(q,'c'))printf("\t提示:队满,不能进队\n"); - printf("(3)队列为%s\n",(QueueEmpty(q)?"空":"非空")); - if(deQueue(q,e)==0) - printf("队空,不能出队\n"); - else - printf("(4)出队一个元素%c\n",e); - printf("(5)依次进队列元素d,e,f\n"); - if(!enQueue(q,'d'))printf("\t提示:队满,不能进队\n"); - if(!enQueue(q,'e'))printf("\t提示:队满,不能进队\n"); - if(!enQueue(q,'f'))printf("\t提示:队满,不能进队\n"); - printf("(6)出队列序列:"); - while(!QueueEmpty(q)); - { deQueue(q,e); - printf("%c",e); - } - printf("\n"); - printf("(7)释放队列\n"); - DestroyQueue(q); - return 1;; -} \ No newline at end of file diff --git a/2224020107/exp6-1.cpp b/2224020107/exp6-1.cpp deleted file mode 100644 index 01213b1e045a4124567e40d7e5e35ababb1251ec..0000000000000000000000000000000000000000 --- a/2224020107/exp6-1.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include - -using namespace std; - -struct SparseMatrix { - int row; - int col; - vector> values; -}; - -SparseMatrix add(SparseMatrix a, SparseMatrix b) { - SparseMatrix result; - result.row = a.row; - result.col = a.col; - result.values.resize(a.values.size()); - - for (int i = 0; i < a.values.size(); i++) { - for (int j = 0; j < a.values[i].size(); j++) { - if (a.values[i][j] != 0 && b.values[i][j] != 0) { - result.values[i][j] = a.values[i][j] + b.values[i][j]; - } - } - } - return result; -} - -SparseMatrix multiply(SparseMatrix a, SparseMatrix b) { - SparseMatrix result; - result.row = a.row; - result.col = b.col; - result.values.resize(a.row * b.col); - - for (int i = 0; i < a.row; i++) { - for (int j = 0; j < b.col; j++) { - int temp = 0; - for (int k = 0; k < a.col; k++) { - if (a.values[i][k] != 0 && b.values[k][j] != 0) { - temp += a.values[i][k] * b.values[k][j]; - } - } - result.values[i][j] = temp; - } - } - return result; -} \ No newline at end of file diff --git a/2224020107/exp6-3.cpp b/2224020107/exp6-3.cpp deleted file mode 100644 index df2df8ed64f2f3be6995508fc4ba4747336cceed..0000000000000000000000000000000000000000 --- a/2224020107/exp6-3.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include -using namespace std; - -int main() { - int n = 5; - int a[n][n]; - - // 初始化数组 - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - a[i][j] = 0; - } - } - - // 设置螺旋方阵的元素值 - a[0][0] = 1; - int row = 0, col = 0, count = 2; - while (count < n * n) { - if (col == n - 1) { - row++; - col = 0; - } else { - col++; - } - a[row][col] = count++; - } - for (int i = 1; i < n; i++) { - for (int j = 1; j < n; j++) { - if (a[i][j] == 0) { - a[i][j] = count++; - } - } - } - - // 输出螺旋方阵 - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - cout << a[i][j] << " "; - } - cout << endl; - } - - return 0; -} \ No newline at end of file diff --git a/2224020107/exp6-5.cpp b/2224020107/exp6-5.cpp deleted file mode 100644 index c0ea83c6d2e13f349b74b78c22f790a9f0eff4f5..0000000000000000000000000000000000000000 --- a/2224020107/exp6-5.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include -using namespace std; - -const int MAXN = 1005; - -int a[MAXN][MAXN], b[MAXN][MAXN], c[MAXN][MAXN]; - -int main() { - int n; - cin >> n; - for (int i = 1; i <= n; i++) { - for (int j = 1; j <= n; j++) { - cin >> a[i][j]; - } - } - for (int i = 1; i <= n; i++) { - for (int j = 1; j <= n; j++) { - cin >> b[i][j]; - } - } - - // 求两个对称矩阵之和 - for (int i = 1; i <= n; i++) { - for (int j = 1; j <= n; j++) { - c[i][j] = a[i][j] + b[i][j]; - } - } - cout << "矩阵之和为:" << endl; - for (int i = 1; i <= n; i++) { - for (int j = 1; j <= n; j++) { - cout << c[i][j] << " "; - } - cout << endl; - } - - // 求两个对称矩阵的乘积 - for (int i = 1; i <= n; i++) { - for (int j = 1; j <= n; j++) { - int sum = 0; - for (int k = 1; k <= n; k++) { - sum += a[i][k] * b[k][j]; - } - c[i][j] = sum; - } - } - cout << "矩阵乘积为:" << endl; - for (int i = 1; i <= n; i++) { - for (int j = 1; j <= n; j++) { - cout << c[i][j] << " "; - } - cout << endl; - } - - return 0; -} \ No newline at end of file diff --git a/2224020107/exp7-1.cpp b/2224020107/exp7-1.cpp deleted file mode 100644 index dbff302bab63b55a13d5e333043a424edd50f15b..0000000000000000000000000000000000000000 --- a/2224020107/exp7-1.cpp +++ /dev/null @@ -1,86 +0,0 @@ -#include -using namespace std; - -// 定义二叉树节点结构体 -struct TreeNode { - int val; - TreeNode* left; - TreeNode* right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -// 插入节点 -void insert(TreeNode*& root, int val) { - if (root == NULL) { - root = new TreeNode(val); - return; - } - if (val < root->val) { - insert(root->left, val); - } else { - insert(root->right, val); - } -} - -// 查找最小节点 -TreeNode* findMin(TreeNode* root) { - while (root->left != NULL) { - root = root->left; - } - return root; -} - -// 删除节点 -void deleteNode(TreeNode*& root, int val) { - if (root == NULL) { - return; - } - if (val < root->val) { - deleteNode(root->left, val); - } else if (val > root->val) { - deleteNode(root->right, val); - } else { - if (root->left == NULL) { - TreeNode* temp = root->right; - delete root; - root = temp; - } else if (root->right == NULL) { - TreeNode* temp = root->left; - delete root; - root = temp; - } else { - TreeNode* minNode = findMin(root->right); - root->val = minNode->val; - deleteNode(root->right, minNode->val); - } - } -} - -// 中序遍历二叉树(递归实现) -void inorderTraversal(TreeNode* root) { - if (root == NULL) { - return; - } - inorderTraversal(root->left); - cout << root->val << " "; - inorderTraversal(root->right); -} - -// 中序遍历二叉树(迭代实现) -void inorderTraversalIterative(TreeNode* root) { - if (root == NULL) { - return; - } - stack nodeStack; - TreeNode* current = root; - while (current != NULL || !nodeStack.empty()) { - while (current != NULL) { - nodeStack.push(current); - current = current->left; - } - current = nodeStack.top(); - nodeStack.pop(); - cout << current->val << " "; - current = current->right; - } -} \ No newline at end of file diff --git a/2224020107/exp7-7.cpp b/2224020107/exp7-7.cpp deleted file mode 100644 index 27d900db94eb36d9d612b6c13a8ed080434e6b4b..0000000000000000000000000000000000000000 --- a/2224020107/exp7-7.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include -using namespace std; - -struct TreeNode { - int val; - TreeNode* left; - TreeNode* right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -void getPath(TreeNode* root, vector& path) { - if (root == NULL) { - return; - } - path.push_back(root->val); - if (root->left == NULL && root->right == NULL) { - // 叶子结点,返回路径 - for (int i = 0; i < path.size(); i++) { - cout << path[i] << " "; - } - cout << endl; - return; - } - // 递归遍历左子树和右子树 - getPath(root->left, path); - getPath(root->right, path); -} - -int main() { - TreeNode* root = new TreeNode(1); - root->left = new TreeNode(2); - root->right = new TreeNode(3); - root->left->left = new TreeNode(4); - root->left->right = new TreeNode(5); - root->right->left = new TreeNode(6); - root->right->right = new TreeNode(7); - - vector path; - getPath(root, path); - return 0; -} \ No newline at end of file diff --git a/2224020107/exp8-1.cpp b/2224020107/exp8-1.cpp deleted file mode 100644 index efd39182a59dd778ab42bbea4ba09bc06aa6f248..0000000000000000000000000000000000000000 --- a/2224020107/exp8-1.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include -using namespace std; - -class Graph { - int V; // 顶点数量 - vector> adjMatrix; // 邻接矩阵 - vector> adjList; // 邻接表 - -public: - Graph(int V); // 构造函数 - void addEdge(int v, int w); // 添加边 - void printGraph(); // 打印图 -}; -Graph::Graph(int V) { - this->V = V; - adjMatrix = vector>(V, vector(V, 0)); // 初始化邻接矩阵 - adjList = vector>(V, vector(0)); // 初始化邻接表 -} - -void Graph::addEdge(int v, int w) { - adjMatrix[v][w] = 1; // 在邻接矩阵中添加边 - adjMatrix[w][v] = 1; // 因为是无向图,所以反向也要添加 - adjList[v].push_back(w); // 在邻接表中添加边 - adjList[w].push_back(v); // 因为是无向图,所以反向也要添加 -} - -void Graph::printGraph() { - cout << "Adjacency Matrix: " << endl; - for (int i = 0; i < V; i++) { - for (int j = 0; j < V; j++) { - cout << adjMatrix[i][j] << " "; - } - cout << endl; - } - cout << "Adjacency List: " << endl; - for (int i = 0; i < V; i++) { - for (int j = 0; j < adjList[i].size(); j++) { - cout << adjList[i][j] << " "; - } - cout << endl; - } -} -int main() { - Graph g(5); // 创建一个有5个顶点的图 - g.addEdge(0, 1); // 添加边(0, 1) - g.addEdge(0, 4); // 添加边(0, 4) - g.addEdge(1, 2); // 添加边(1, 2) - g.addEdge(1, 3); // 添加边(1, 3) - g.addEdge(1, 4); // 添加边(1, 4) - g.addEdge(2, 3); // 添加边(2, 3) - g.addEdge(3, 4); // 添加边(3, 4) - g.printGraph(); // 打印图 - return 0; -} \ No newline at end of file diff --git a/2224020107/exp8-14,cpp b/2224020107/exp8-14,cpp deleted file mode 100644 index b06cc52c8e93286d70b7156f6d1cd5e4b4f87d93..0000000000000000000000000000000000000000 --- a/2224020107/exp8-14,cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include -using namespace std; - -int main() { - int n, m; - cin >> n >> m; - vector> a(n, vector(n, 0)); - for (int i = 0; i < m; i++) { - int x, y, z; - cin >> x >> y >> z; - a[x-1][y-1] = z; - } - vector> dp(n, vector(n, INT_MAX)); - dp[0][0] = 0; - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - for (int k = 0; k < n; k++) { - if (a[i][j] != 0 && a[j][k] != 0 && a[k][i] != 0) { - dp[i][k] = min(dp[i][k], dp[i][j] + a[j][k]); - } - } - } - } - cout << dp[n-1][n-1] << endl; - return 0; -} \ No newline at end of file diff --git a/2224020107/exp8-8.cpp b/2224020107/exp8-8.cpp deleted file mode 100644 index 2e65c8174ffbdfb433d8f9a74a2c4b467f2fc0f2..0000000000000000000000000000000000000000 --- a/2224020107/exp8-8.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include -using namespace std; - -void floydWarshall(vector>& graph) { - int V = graph.size(); - - // 初始化距离矩阵为输入图的邻接矩阵 - vector> dist(V, vector(V)); - for (int i = 0; i < V; i++) { - for (int j = 0; j < V; j++) { - dist[i][j] = graph[i][j]; - } - } - - // 对于每个顶点,考虑所有的对(i,j)的顶点对,看看是否可以通过 k 获得更短的距离 - for (int k = 0; k < V; k++) { - for (int i = 0; i < V; i++) { - for (int j = 0; j < V; j++) { - if (dist[i][k] + dist[k][j] < dist[i][j]) { - dist[i][j] = dist[i][k] + dist[k][j]; - } - } - } - } - - // 打印结果 - for (int i = 0; i < V; i++) { - for (int j = 0; j < V; j++) { - if (dist[i][j] == INT_MAX) { - cout << "INF "; - } else { - cout << dist[i][j] << " "; - } - } - cout << endl; - } -} - -int main() { - // 创建带权有向图的邻接矩阵表示形式 - vector> graph = { - {0, 5, INT_MAX, 10}, - {INF, 0, 3, INF}, - {INF, INF, 0, 1}, - {INF, INF, INF, 0} - }; - - // 运行弗洛伊德算法并打印结果 - floydWarshall(graph); - - return 0; -} \ No newline at end of file diff --git a/2224020111/exp2-1.cpp b/2224020111/exp2-1.cpp deleted file mode 100644 index 49f7f910c4e99ae371d020c521d3657803b22a0c..0000000000000000000000000000000000000000 --- a/2224020111/exp2-1.cpp +++ /dev/null @@ -1,104 +0,0 @@ - - -#define MAX_SIZE 100 // 定义顺序表的最大长度 - -typedef struct { - int data[MAX_SIZE]; // 顺序表的数据域 - int length; // 顺序表的当前长度 -} SeqList; - -// 初始化顺序表 -void init(SeqList *list) { - list->length = 0; -} - - - -// 在顺序表的指定位置插入元素 -int insert(SeqList *list, int pos, int element) { - if (pos < 0 || pos > list->length || list->length == MAX_SIZE) { - // 位置非法或顺序表已满 - return 0; - } - - // 将插入位置后的元素后移 - for (int i = list->length - 1; i >= pos; i--) { - list->data[i + 1] = list->data[i]; - } - - // 插入新元素 - list->data[pos] = element; - list->length++; - - return 1; -} - - - -// 删除顺序表的指定位置的元素 -int remove(SeqList *list, int pos) { - if (pos < 0 || pos >= list->length) { - // 位置非法 - return 0; - } - - // 将删除位置后的元素前移 - for (int i = pos + 1; i < list->length; i++) { - list->data[i - 1] = list->data[i]; - } - - list->length--; - - return 1; -} - - - -// 查找顺序表中指定元素的位置 -int search(SeqList *list, int element) { - for (int i = 0; i < list->length; i++) { - if (list->data[i] == element) { - return i; - } - } - - return -1; // 未找到返回-1 -} - - - - -// 获取顺序表的长度 -int length(SeqList *list) { - return list->length; -} - - - - -// 获取顺序表指定位置的元素 -int getElement(SeqList *list, int pos) { - if (pos < 0 || pos >= list->length) { - // 位置非法 - return -1; - } - - return list->data[pos]; -} - - - - -// 修改顺序表指定位置的元素 -int modify(SeqList *list, int pos, int newElement) { - if (pos < 0 || pos >= list->length) { - // 位置非法 - return 0; - } - - list->data[pos] = newElement; - - return 1; -} - - diff --git a/2224020111/exp2-2.cpp b/2224020111/exp2-2.cpp deleted file mode 100644 index 255c3c628f98ceb1c5757dc1ced83cc9de14b853..0000000000000000000000000000000000000000 --- a/2224020111/exp2-2.cpp +++ /dev/null @@ -1,156 +0,0 @@ -#include -#include - -// 定义链表节点结构 -struct Node { - int data; - struct Node* next; -}; -// 创建新节点 -struct Node* createNode(int data) { - struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); - if (newNode == NULL) { - printf("Memory allocation failed.\n"); - exit(1); - } - newNode->data = data; - newNode->next = NULL; - return newNode; -} -// 在链表末尾插入节点 -void insertAtEnd(struct Node** head, int data) { - struct Node* newNode = createNode(data); - - if (*head == NULL) { - *head = newNode; - return; - } - struct Node* current = *head; - while (current->next != NULL) { - current = current->next; - } - current->next = newNode; -} -// 在链表头部插入节点 -void insertAtBeginning(struct Node** head, int data) { - struct Node* newNode = createNode(data); - - newNode->next = *head; - *head = newNode; -} -// 在指定位置插入节点 -void insertAtPosition(struct Node** head, int data, int position) { - struct Node* newNode = createNode(data); - - if (position <= 0) { - printf("Invalid position.\n"); - return; - } - - if (position == 1) { - newNode->next = *head; - *head = newNode; - return; - } - - struct Node* current = *head; - int count = 1; - while (count < position - 1 && current != NULL) { - current = current->next; - count++; - } - - if (current == NULL) { - printf("Invalid position.\n"); - return; - } - - newNode->next = current->next; - current->next = newNode; -} - -// 从链表中删除指定元素 -void deleteNode(struct Node** head, int data) { - struct Node* temp = *head; - struct Node* prev = NULL; - - if (temp != NULL && temp->data == data) { - *head = temp->next; - free(temp); - return; - } - - while (temp != NULL && temp->data != data) { - prev = temp; - temp = temp->next; - } - - if (temp == NULL) { - printf("Element not found.\n"); - return; - } - - prev->next = temp->next; - free(temp); -} - -// 遍历并打印链表的元素 -void printLinkedList(struct Node* head) { - if (head == NULL) { - printf("Linked list is empty.\n"); - return; - } - - printf("Linked list elements: "); - struct Node* current = head; - while (current != NULL) { - printf("%d ", current->data); - current = current->next; - } - printf("\n"); -} - -// 删除整个链表 -void deleteLinkedList(struct Node** head) { - struct Node* current = *head; - struct Node* next; - - while (current != NULL) { - next = current->next; - free(current); - current = next; - } - - *head = NULL; -} - -int main() { - struct Node* head = NULL; - - insertAtEnd(&head, 1); - insertAtEnd(&head, 2); - insertAtEnd(&head, 3); - - printLinkedList(head); // 1 2 3 - - insertAtBeginning(&head, 4); - - printLinkedList(head); // 4 1 2 3 - - insertAtPosition(&head, 5, 3); - - printLinkedList(head); // 4 1 5 2 3 - - deleteNode(&head, 2); - - printLinkedList(head); // 4 1 5 3 - - deleteLinkedList(&head); - - printLinkedList(head); // Linked list is empty. - - return 0; -} - - - diff --git a/2224020111/exp2-6.cpp b/2224020111/exp2-6.cpp deleted file mode 100644 index 5dcbb272490177d1ea710adada76dadb6b3bd999..0000000000000000000000000000000000000000 --- a/2224020111/exp2-6.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#include -#include -#include - -typedef int ElemType; -typedef struct LNode -{ - ElemType data; - struct LNode* next; -}LinkNode; - -bool DivideList(LinkNode*& L, ElemType x); //L为传入的链表头结点,x为传入的基准 -bool InitList(LinkNode*& L); //链表产生 -bool DispList(LinkNode* L); //链表输出 - -int main(int argc, const char* argv[]) -{ - LinkNode* L = NULL; - int x; - bool flag = false; - InitList(L); - printf("请输入基准。\n"); - scanf("%d", &x); - flag = DivideList(L, x); - if (flag) - printf("算法执行成功。\n"); - else - printf("算法执行失败。\n"); - DispList(L); - return 0; -} - -bool InitList(LinkNode*& L) //建立单链表 -{ - while (!L) { - L = (LinkNode*)malloc(sizeof(LNode)); - } - L->next = NULL; - int i, n; - LinkNode* p = NULL, * q = NULL; - q = L; - printf("请输入数据规模:\n"); - scanf("%d", &n); - printf("请输入数据:\n"); - for (i = 0; i < n; i++) { - while (!p) { - p = (LinkNode*)malloc(sizeof(LNode)); - } - scanf("%d", &p->data); - q->next = p; - q = p; - p = q->next = NULL; - } - return true; -} -bool DispList(LinkNode* L) //输出单链表 -{ - LinkNode* p = L->next; - while (p) { - printf("\t%d", p->data); - p = p->next; - } - printf("\n"); - return true; -} -bool DivideList(LinkNode*& L, ElemType x) //L为传入的链表头结点,x为传入的基准 -{ - if (L->next==NULL) { - printf("单链表不存在。\n"); - return false; - } - LinkNode* p = NULL, * q = NULL, * r = NULL; //p为工作指针,q为小于基准的数的头结点,r为尾指针 - p = L; - //直到申请成功,因为malloc可能申请失败 - while (!q) { - q = (LinkNode*)malloc(sizeof(LinkNode)); - } - r = q; //r指向小于基准的链表的尾巴 - //p从第一个有效结点开始扫描整个链表,直到p后无结点为空 - while (p->next) { - if (p->next->data < x) { - r->next = p->next; //将p接到尾巴后面 - r=r->next; //尾巴后移 - p->next= p->next->next; //将该结点从L中截取出去 - r->next = NULL; //将尾巴与原链表之间的关系切断 - } - else - p = p->next; //p后移 - } - //将小于基准的链表接到大于等于基准的链表的前面 - r->next = L->next; //小于基准的链表的尾巴与大于等于基准的链表相连 - L->next = q->next; //将整体接到头结点后 - free(q); //释放小于基准的链表的头结点 - return true; -} diff --git a/2224020111/exp3-1.cpp b/2224020111/exp3-1.cpp deleted file mode 100644 index 34f6e411deb1547e0cc1c3d755c1fb0fc5170802..0000000000000000000000000000000000000000 --- a/2224020111/exp3-1.cpp +++ /dev/null @@ -1,90 +0,0 @@ - -#include -#include - -#define MAX_SIZE 100 - -typedef struct { - int data[MAX_SIZE]; - int top; -} SeqStack; - -// 初始化顺序栈 -void InitStack(SeqStack *stack) { - stack->top = -1; -} - -// 判断栈是否为空 -int IsEmpty(SeqStack *stack) { - return stack->top == -1; -} - -// 判断栈是否已满 -int IsFull(SeqStack *stack) { - return stack->top == MAX_SIZE - 1; -} - -// 入栈 -void Push(SeqStack *stack, int item) { - if (IsFull(stack)) { - printf("Stack is full.\n"); - return; - } - stack->data[++stack->top] = item; -} - -// 出栈 -int Pop(SeqStack *stack) { - if (IsEmpty(stack)) { - printf("Stack is empty.\n"); - return -1; - } - return stack->data[stack->top--]; -} - -// 获取栈顶元素 -int Top(SeqStack *stack) { - if (IsEmpty(stack)) { - printf("Stack is empty.\n"); - return -1; - } - return stack->data[stack->top]; -} - -// 清空栈 -void ClearStack(SeqStack *stack) { - stack->top = -1; -} - -// 获取栈的长度 -int GetLength(SeqStack *stack) { - return stack->top + 1; -} - -int main() { - SeqStack stack; - InitStack(&stack); - - // 入栈操作 - Push(&stack, 1); - Push(&stack, 2); - Push(&stack, 3); - - // 出栈操作 - printf("Pop: %d\n", Pop(&stack)); - - // 获取栈顶元素 - printf("Top: %d\n", Top(&stack)); - - // 判断栈是否为空 - printf("Is empty: %d\n", IsEmpty(&stack)); - - // 获取栈的长度 - printf("Length: %d\n", GetLength(&stack)); - - // 清空栈 - ClearStack(&stack); - - return 0; -} - diff --git a/2224020111/shuzu.cpp b/2224020111/shuzu.cpp deleted file mode 100644 index f4ea5f331c9bd455acaf2f83c535a7ca0c910025..0000000000000000000000000000000000000000 --- a/2224020111/shuzu.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -int main() { - int array1[5] = {1, 2, 3, 4, 5}; - int array2[3] = {6, 7, 8}; - int mergedArray[8]; - int i, j; - - for (i = 0; i < 5; i++) { - mergedArray[i] = array1[i]; - } - for (j = 0; j < 3; j++) { - mergedArray[i++] = array2[j]; - } - for (i = 0; i < 8; i++) { - printf("%d ", mergedArray[i]); - } - return 0; -} - - - - - diff --git "a/2224020112/chapter6/\345\256\236\347\216\260\347\250\200\347\226\217\347\237\251\351\230\265\357\274\210\351\207\207\347\224\250\344\270\211\345\205\203\347\273\204\350\241\250\347\244\272\357\274\211\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2224020112/chapter6/\345\256\236\347\216\260\347\250\200\347\226\217\347\237\251\351\230\265\357\274\210\351\207\207\347\224\250\344\270\211\345\205\203\347\273\204\350\241\250\347\244\272\357\274\211\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" deleted file mode 100644 index f68370c1e1dd683e75c5b2f018d15fdaa8106c6f..0000000000000000000000000000000000000000 --- "a/2224020112/chapter6/\345\256\236\347\216\260\347\250\200\347\226\217\347\237\251\351\230\265\357\274\210\351\207\207\347\224\250\344\270\211\345\205\203\347\273\204\350\241\250\347\244\272\357\274\211\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,206 +0,0 @@ -#include -#define MaxSize 100 -#define N 4 -#define M 4 -typedef int elem; -typedef struct -{ - int r; - int c; - elem d; -}TupNode; - -typedef struct -{ - int row; - int cols; - int nums; - TupNode data[MaxSize]; -}TSMatrix; - -void CreatMat(TSMatrix &t, elem A[N][M]) -{ - int i, j; - t.row = N; - t.cols = M; - t.nums = 0; - for(i = 0; i < N; i++) - { - for(j = 0; j < M; j++) - { - if(A[i][j] != 0) - { - t.data[t.nums].r = i; - t.data[t.nums].c = j; - t.data[t.nums].d = A[i][j]; - t.nums++; - } - } - } -} - -void DipMat(TSMatrix t) -{ - int i; - if(t.nums <= 0) - return; - printf("\t%d\t%d\t%d\n",t.row, t.cols, t.nums); - printf("-------------------------------------\n"); - for(i = 0; i < t.nums; i++) - printf("\t%d\t%d\t%d\n",t.data[i].r, t.data[i].c, t.data[i].d); -} - -void TranTat(TSMatrix t, TSMatrix &tb) -{ - int p, q = 0, v; - tb.row = t.cols; - tb.cols = t.row; - tb.nums = t.nums; - if(t.nums != 0) - { - for(v = 0; v < t.cols; v++) - { - for(p = 0; p < t.nums; p++) - if(t.data[p].c == v) - { - tb.data[q].r = t.data[p].c; - tb.data[q].c = t.data[p].r; - tb.data[q].d = t.data[p].d; - q++; - } - } - } -} - -void MatAdd(TSMatrix a, TSMatrix b, TSMatrix &c) -{ - if(a.row != b.row || a.cols != b.cols) - { - printf("矩阵相加操作失败\n"); - return; - } - c.row = a.row; - c.cols = a.cols; - int i = 0 , j = 0, k = 0; - while(i < a.nums || j < b.nums) - { - if(a.data[i].r < b.data[j].r) - { - c.data[k].r = a.data[i].r; - c.data[k].c = a.data[i].c; - c.data[k].d = a.data[i].d; - i++; - k++; - } - else if(a.data[i].r == b.data[j].r) - { - if(a.data[i].c < b.data[j].c) - { - c.data[k].r = a.data[i].r; - c.data[k].c = a.data[i].c; - c.data[k].d = a.data[i].d; - i++; - k++; - } - else if(a.data[i].c == b.data[j].c) - { - c.data[k].r = b.data[j].r; - c.data[k].c = b.data[j].c; - c.data[k].d = a.data[i].d + b.data[j].d; - i++; - j++; - k++; - } - else - { - c.data[k].r = b.data[j].r; - c.data[k].c = b.data[j].c; - c.data[k].d = b.data[j].d; - j++; - k++; - } - } - else - { - c.data[k].r = b.data[j].r; - c.data[k].c = b.data[j].c; - c.data[k].d = b.data[j].d; - j++; - k++; - } - } - c.nums = k; -} - -int getvalue(TSMatrix t,int i,int j) -{ - for(int k = 0; k < t.nums; k++) - { - if(t.data[k].r == i && t.data[k].c == j) - return t.data[k].d; - } - return 0; -} - -void MatMul(TSMatrix a,TSMatrix b,TSMatrix &c) -{ - if(a.cols != b.row) - { - printf("矩阵相乘操作失败\n"); - return; - } - int i, j, k = 0, s; - c.row = a.row; - c.cols = b.cols; - - for(i = 0; i < a.row; i++) - { - for(j = 0; j < b.cols; j++) - { - s = 0; - for(int m = 0; m < b.cols; m++) - { - s = s + getvalue(a, m, i)*getvalue(b, j, m); - } - if(s != 0) - { - c.data[k].r = i; - c.data[k].c = j; - c.data[k].d = s; - k++; - } - } - } - c.nums = k; -} - -int main() -{ - TSMatrix t1, t2, c; - elem a[4][4] = { - {1,0,3,0}, - {0,1,0,0}, - {0,0,1,0}, - {0,0,1,1}}; - CreatMat(t1, a); - printf("a的三元组:\n"); - DipMat(t1); - elem b[4][4] = { - {3,0,0,0}, - {0,4,0,0}, - {0,0,1,0}, - {0,0,0,2}}; - CreatMat(t2, b); - printf("b的三元组:\n"); - DipMat(t2); - TranTat(t1, c); - printf("a转置后\n"); - DipMat(c); - printf("c = a + b\nc的三元组:\n"); - MatAdd(t1, t2, c); - DipMat(c); - printf("c = a * b\nc的三元组:\n"); - MatMul(t1, t2, c); - DipMat(c); - return 0; -} diff --git "a/2224020112/chapter6/\346\261\2025x5\351\230\266\347\232\204\350\236\272\346\227\213\346\226\271\351\230\265.cpp" "b/2224020112/chapter6/\346\261\2025x5\351\230\266\347\232\204\350\236\272\346\227\213\346\226\271\351\230\265.cpp" deleted file mode 100644 index 9d268a59e53aaa3dc48c5550325c39946562addf..0000000000000000000000000000000000000000 --- "a/2224020112/chapter6/\346\261\2025x5\351\230\266\347\232\204\350\236\272\346\227\213\346\226\271\351\230\265.cpp" +++ /dev/null @@ -1,55 +0,0 @@ -#include -#define MAX_LEN 10 -static void helix_matrix(int a[MAX_LEN][MAX_LEN], int n) -{ - int m; - int i; - int j; - int k = 0; - if(n % 2 == 0) - m = n / 2; - else - m = n / 2 + 1; - for(i = 0; i < m; i++) - { - for(j = i; j < n - i; j++) - { - k++; - a[i][j] = k; - } - for(j = i + 1; j < n - i; j++) - { - k++; - a[j][n - i - 1] = k; - } - for(j = n - i - 2; j >= i; j--) - { - k++; - a[n - i - 1][j] = k; - } - for(j = n - i - 2; j >= i + 1; j--) - { - k++; - a[j][i] = k; - } - } -} - -int main(void) -{ - int n, i, j; - int a[MAX_LEN][MAX_LEN]; - printf("输入n(n<10):"); - scanf("%d", &n); - helix_matrix(a, n); - printf("%d阶数字方阵如下:\n", n); - for(i = 0; i < n; i++) - { - for(j = 0; j < n; j++) - { - printf("%4d", a[i][j]); - } - printf("\n"); - } - return 0; -} diff --git "a/2224020112/chapter6/\346\261\202\344\270\244\344\270\252\345\257\271\347\247\260\347\237\251\351\230\265\344\271\213\345\222\214\344\270\216\344\271\230\347\247\257.cpp" "b/2224020112/chapter6/\346\261\202\344\270\244\344\270\252\345\257\271\347\247\260\347\237\251\351\230\265\344\271\213\345\222\214\344\270\216\344\271\230\347\247\257.cpp" deleted file mode 100644 index 41249223d742f4c4c9ec0edd338df53816942fdd..0000000000000000000000000000000000000000 --- "a/2224020112/chapter6/\346\261\202\344\270\244\344\270\252\345\257\271\347\247\260\347\237\251\351\230\265\344\271\213\345\222\214\344\270\216\344\271\230\347\247\257.cpp" +++ /dev/null @@ -1,86 +0,0 @@ -#include -#define N 4 -#define M 10 -static int value(int a[], int i, int j) -{ - if (i >= j) - return a[(i * (i + 1)) / 2 + j]; - else - return a[(j * (j + 1)) / 2 + i]; -} - -static void madd(int a[], int b[], int c[][N]) -{ - int i, j; - for (i = 0; i < N; i++) - { - for (j = 0; j < N; j++) - { - c[i][j] = value(a, i, j) + value(b, i, j); - } - } -} - -static void mult(int a[], int b[], int c[][N]) -{ - int i, j, k, sum; - for (i = 0; i < N; i++) - { - for (j = 0; j < N; j++) - { - sum = 0; - for (k = 0; k < N; k++) - { - sum = sum + value(a, i, k) * value(b, k, j); - } - c[i][j] = sum; - } - } -} - -static void disp1(int a[]) -{ - int i, j; - for (i = 0; i < N; i++) - { - for (j = 0; j < N; j++) - { - printf("%4d", value(a, i, j)); - } - printf("\n"); - } -} - -static void disp2(int c[][N]) -{ - int i, j; - - for (i = 0; i < N; i++) - { - for (j = 0; j < N; j++) - { - printf("%4d", c[i][j]); - } - printf("\n"); - } -} - -int main(int argc, char* argv[]) -{ - int a[M] = { 0, 1, 0, 1, 1, 0, 1, 1, 1, 0 }; - int b[M] = { 1, 3, 6, 6, 10, 15, 10, 15, 21, 28 }; - int c1[N][N], c2[N][N]; - printf("a矩阵:\n"); - disp1(a); - printf("b矩阵:\n"); - disp1(b); - - madd(a, b, c1); - printf("a+b:\n"); - disp2(c1); - - mult(a, b, c2); - printf("a*b:\n"); - disp2(c2); - return 0; -} diff --git "a/2224020112/chapter7/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" "b/2224020112/chapter7/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" deleted file mode 100644 index 4a4dc9da198c8597e692e74e56d30dc12f0e97f7..0000000000000000000000000000000000000000 --- "a/2224020112/chapter7/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.cpp" +++ /dev/null @@ -1,201 +0,0 @@ -#include -#include -#include - -#define MaxSons 3 -typedef char ElemType; -typedef struct node -{ - ElemType data[15]; - struct node *sons[MaxSons]; -}TSonNode; -typedef struct -{ - char N[15]; - char n[15]; -}array; - -void ReadFile(array R[],FILE *fp,int &n) -{ - while((fscanf(fp,"%s",R[n].N))!=EOF&&(fscanf(fp,"%s",R[n].n))!=EOF) - n++; -} - -TSonNode *CreateTree(char str[],array R[],int n) -{ - TSonNode *t; - int k,i=0,j=0; - t=(TSonNode *)malloc(sizeof(TSonNode)); - strcpy(t->data,str); - for(k=0;ksons[k]=NULL; - while(isons[j]=CreateTree(R[i].n,R,n); - j++; - } - i++; - } - return t; -} - -void DispTree(TSonNode *t) -{ - int i=0; - if(t==NULL) - printf("此树为空树!\n"); - else - { - printf("%s",t->data); - if(t->sons[i]!=NULL) - { - printf("("); - for(i=0;isons[i]); - if(t->sons[i+1]!=NULL) - printf(","); - else - break; - } - printf(")"); - } - } -} - -void DestroyTree(TSonNode *t) -{ - if(t==NULL) - printf("此树为空树!\n"); - else - { - for(int i=0;isons[i]!=NULL) - DestroyTree(t->sons[i]); - else - break; - } - free(t); - } -} - -TSonNode *FindNode(TSonNode *t,char str[]) -{ - TSonNode *p; - if(t==NULL) - return NULL; - else - { - if(strcmp(t->data,str)==0) - return t; - else - { - for(int i=0;isons[i]!=NULL) - { - p=FindNode(t->sons[i],str); - if(p!=NULL) - return p; - } - } - return NULL; - } - } -} - -int ChildCount(TSonNode *p) -{ - int count=0; - for(int i=0;isons[i]!=NULL) - count++; - else - break; - } - return count; -} - -int LeafCount(TSonNode *p) -{ - int count=0; - if(p==NULL) - return 0; - else - { - if(p->sons[0]==NULL) - count++; - else - { - for(int i=0;isons[i]!=NULL) - count=count+LeafCount(p->sons[i]); - else - break; - } - } - } - return count; -} - -int LeafSumOfvalue(TSonNode *p) -{ - int sum=0; - if(p==NULL) - return 0; - else - { - if(p->sons[0]==NULL) - return atoi(p->data); - else - { - for(int i=0;isons[i]!=NULL) - sum+=LeafSumOfvalue(p->sons[i]); - else - break; - } - } - } - return sum; -} - -#define MaxSize 66 -int main() -{ - int n=0; - TSonNode *t; - array R[MaxSize]; - FILE *fp; - if((fp=fopen("table.txt","r"))==NULL) - { - printf("error!cannot open the file!"); - exit(1); - } - printf("读取文件内容存入数组R中\n"); - ReadFile(R,fp,n); - printf("输出数组R:\n"); - for(int i=0;i -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct node -{ - ElemType data; - struct node *lchild; - struct node *rchild; -} -BTNode; -void CreateBTree(BTNode *&b,char *str) -{ - BTNode * St[MaxSize],*p; - int top=-1,k,j=0;char ch; - b=NULL; - ch=str[j]; - while(ch!='\0') - { - switch(ch) - { - case'(':top++;St[top]=p;k=1;break; - case')':top--;break; - case',':k=2;break; - - default:p=(BTNode *)malloc(sizeof(BTNode)); - p->data=ch;p->lchild=p->rchild=NULL; - if(b==NULL) - b=p; - else - { - switch(k) - { - case 1:St[top]->lchild=p;break; - case 2:St[top]->rchild=p;break; - } - } - } - j++;ch=str[j]; - } -} -void DestroyBTree(BTNode *&b) -{ - if(b!=NULL) - { - DestroyBTree(b->lchild); - DestroyBTree(b->rchild); - free(b); - } -} -BTNode *FindNode(BTNode *b,ElemType x) -{ - BTNode *p; - if(b==NULL) - return NULL; - else if(b->data==x) - return b; - else - { - p=FindNode(b->lchild,x); - if(p!=NULL) - return p; - else - return FindNode(b->rchild,x); - } -} -BTNode *LchildNode(BTNode *p) -{ - return p->lchild; -} -BTNode *RchildNode(BTNode *p) -{ - return p->rchild; -} -int BTHeight(BTNode *b) -{ - int lchildh,rchildh; - if(b==NULL)return(0); - else - { - lchildh=BTHeight(b->lchild); - rchildh=BTHeight(b->rchild); - return(lchildh>rchildh)?(lchildh+1):(rchildh+1); - } -} -void DispBTree(BTNode *b) -{ - if(b!=NULL) - { - printf("%c",b->data); - if(b->lchild!=NULL||b->rchild!=NULL) - { - printf("("); - DispBTree(b->lchild); - if(b->rchild!=NULL)printf(","); - DispBTree(b->rchild); - printf(")"); - } - } -} -int main() -{ - BTNode *b,*p,*lp,*rp;; - printf("二叉树的基本运算如下:\n"); - printf(" (1)创建二叉树\n"); - CreateBTree(b,"A(B(D,E(H(J,k(L,M(,N))))),C(F,G(,I)))"); - printf(" (2)输出二叉树:");DispBTree(b);printf("\n"); - printf(" (3)H结点:"); - p=FindNode(b,'H'); - if(p!=NULL) - { lp= LchildNode(p); - if(lp!=NULL)printf("左孩子为%c",lp->data); - else printf("无左孩子"); - rp=RchildNode(p); - if(rp!=NULL) printf("右孩子为%c",rp->data); - else printf("无右孩子"); - } - printf("\n"); - printf(" (4)二叉树b的高度:%d\n",BTHeight(b)); - printf(" (5)释放二叉树b\n"); - DestroyBTree(b); - return 1; -} diff --git "a/2224020112/chapter7/\346\236\204\351\200\240\345\223\210\345\244\253\346\233\274\346\240\221\345\222\214\347\224\237\346\210\220\345\223\210\345\244\253\346\233\274\347\274\226\347\240\201.cpp" "b/2224020112/chapter7/\346\236\204\351\200\240\345\223\210\345\244\253\346\233\274\346\240\221\345\222\214\347\224\237\346\210\220\345\223\210\345\244\253\346\233\274\347\274\226\347\240\201.cpp" deleted file mode 100644 index 1c38361b7aaf5aefe93b6d909f8b71ae765cb10b..0000000000000000000000000000000000000000 --- "a/2224020112/chapter7/\346\236\204\351\200\240\345\223\210\345\244\253\346\233\274\346\240\221\345\222\214\347\224\237\346\210\220\345\223\210\345\244\253\346\233\274\347\274\226\347\240\201.cpp" +++ /dev/null @@ -1,177 +0,0 @@ -#include -#include -using namespace std; -#define MAXSIZE 100 - -typedef struct -{ - int weight; - int parent, lchild, rchild; -}HTNode, * HuffmanTree; - -typedef char** HuffmanCode; -void Select(HuffmanTree HT, int t, int &s1, int &s2) -{ - for (int i = 1; i <= t; i++) - { - if (HT[i].parent == 0) - { - s1 = s2 = i; - break; - } - } - int temp; - for (int i = 1; i <= t; i++) - if (HT[i].parent == 0) - { - if (s1 == s2) - if (HT[i].weight < HT[s1].weight) - s1 = i; - else - s2 = i; - else if (HT[i].weight < HT[s1].weight && HT[i].weight < HT[s2].weight) - { - s2 = s1; - s1 = i; - } - else if (HT[i].weight > HT[s1].weight && HT[i].weight < HT[s2].weight) - s2 = i; - } -} - -void HT_OutPut(HuffmanTree HT, int n) -{ - cout << ">> 该哈夫曼树的存储结构如下:" << endl; - cout << "\t结点\t权重\t父亲\t左孩子\t右孩子" << endl; - for (int i = 1; i < 2*n; i++) - cout << "\t " - << i - << "\t " - << HT[i].weight - << "\t " - << HT[i].parent - << "\t " - << HT[i].lchild - << "\t " - << HT[i].rchild - << endl; -} - -void CreateHuffmanTree(HuffmanTree &HT, int n) -{ - if (n <= 1) - return; - int m = 2 * n - 1; - HT = new HTNode[m + 1]; - for (int i = 0; i <= m; i++) - { - HT[i].weight = 0; - HT[i].parent = 0; - HT[i].lchild = 0; - HT[i].rchild = 0; - } - cout << "依次输入叶子结点的权值: "; - for (int i = 1; i <= n; i++) - cin >> HT[i].weight; - getchar(); - for (int i = n + 1; i <= m; i++) - { - int s1, s2; - Select(HT, i - 1, s1, s2); - - HT[s1].parent = i; - HT[s2].parent = i; - - HT[i].lchild = s1; - HT[i].rchild = s2; - - HT[i].weight = HT[s1].weight + HT[s2].weight; - } -} - -void CreatHuffmanCode(HuffmanTree HT, HuffmanCode& HC, int n) -{ - HC = new char* [n + 1]; - char* cd = new char[n]; - cd[n - 1] = '\0'; - for (int i = 1; i <= n; i++) - { - int start = n - 1; - int c = i; - int f = HT[i].parent; - while (f != 0) - { - start--; - if (HT[f].lchild == c) - cd[start] = '0'; - else - cd[start] = '1'; - c = f; - f = HT[f].parent; - } - HC[i] = new char[n - start]; - strcpy(HC[i], &cd[start]); - } - delete cd; -} - -void HC_OutPut(HuffmanCode HC, int n) -{ - int length = 0; - cout << ">> 下面是哈夫曼编码表: " << endl; - for (int i = 1; i <= n; i++) - { - int j = 0; - cout << "\t编码" << i << ": "; - while (HC[i][j] != '\0') - { - cout << HC[i][j++]; - length++; - } - cout << endl; - } - cout << "\t>> 平均查找长度为: " << (double)length / n << endl; -} - -void TransHuffmanCode(HuffmanTree HT,int n, char* cd) -{ - int start; - for (int i = 1; i <= 2 * n - 1; i++) - if (HT[i].parent == 0) - { - start = i; - break; - } - int p = start; - cout << "译码后的结果: "; - while (*cd != '\0') - { - if (HT[p].lchild == 0 && HT[p].rchild == 0) - { - cout << HT[p].weight << " "; - p = start; - } - if (*cd == '0') - p = HT[p].lchild; - else if (*cd == '1') - p = HT[p].rchild; - cd = cd + 1; - } - cout << endl; -} - -int main() -{ - HuffmanTree HT; - HuffmanCode HC; - char cd[MAXSIZE], ch; - int n; - cout << "请输入您生成的哈夫曼树的叶子结点个数: "; - cin >> n; - CreateHuffmanTree(HT, n); - CreatHuffmanCode(HT, HC, n); - HT_OutPut(HT, n); - HC_OutPut(HC, n); - return 0; -} - diff --git "a/2224020112/chapter7/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\350\267\257\345\276\204.cpp" "b/2224020112/chapter7/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\350\267\257\345\276\204.cpp" deleted file mode 100644 index fc1de402f2d55f7c02a761c59c4957effd0976c2..0000000000000000000000000000000000000000 --- "a/2224020112/chapter7/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\350\267\257\345\276\204.cpp" +++ /dev/null @@ -1,241 +0,0 @@ -#include -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct node -{ - ElemType data; - struct node *lchild; - struct node *rchild; -}BTNode; -void CreateBTree(BTNode *&b,char *str) -{ - BTNode *St[MaxSize],*p; - int top=-1,k,j=0;char ch; - b=NULL; - ch=str[j]; - while(ch!='\0') - { - switch(ch) - { - case '(':top++;St[top]=p;k=1;break; - case ')':top--;break; - case ',':k=2;break; - default:p=(BTNode *)malloc(sizeof(BTNode)); - p->data=ch;p->lchild=p->rchild=NULL; - if(b==NULL) - b=p; - else - { - switch(k) - { - case 1:St[top]->lchild=p;break; - case 2:St[top]->rchild=p;break; - } - } - } - j++;ch=str[j]; - } -} -void DestroyBTree(BTNode *&b) -{ - if(b!=NULL) - { - DestroyBTree(b->lchild); - DestroyBTree(b->rchild); - free(b); - } -} -BTNode *FindNode(BTNode *b,ElemType x) -{ - BTNode *p; - if(b==NULL) - return NULL; - else if(b->data==x) - return b; - else - { - p=FindNode(b->lchild,x); - if(p!=NULL) - return p; - else - return FindNode(b->rchild,x); - } -} -BTNode *LchildNode(BTNode *p) -{ - return p->lchild; -} -BTNode *RchildNode(BTNode *p) -{ - return p->rchild; -} -int BTHeight(BTNode *b) -{ - int lchildh,rchildh; - if(b==NULL)return(0); - else - { - lchildh=BTHeight(b->lchild); - rchildh=BTHeight(b->rchild); - return (lchildh>rchildh)?(lchildh+1):(rchildh+1); - } -} -void DispBTree(BTNode *b) -{ - if(b!=NULL) - { - printf("%c",b->data); - if(b->lchild!=NULL||b->rchild!=NULL) - { - printf("("); - DispBTree(b->lchild); - if(b->rchild!=NULL)printf(","); - DispBTree(b->rchild); - printf(")"); - } - } -} - - - -void AllPath1(BTNode *b,ElemType path[],int pathlen) -{ - if(b!=NULL) - { - if(b->lchild==NULL&&b->rchild==NULL) - { - printf("%c到根结点逆路径:%c->",b->data,b->data); - for(int i=pathlen-1;i>0;i--) - printf("%c->",path[i]); - printf("%c\n",path[0]); - } - else - { - path[pathlen]=b->data; - pathlen++; - AllPath1(b->lchild,path,pathlen); - AllPath1(b->rchild,path,pathlen); - } - } -} -void LongPath1(BTNode *b,ElemType path[],int pathlen,ElemType longpath[],int &longpathlen) -{ - if(b==NULL) - { - if(pathlen>longpathlen) - { - for(int i=pathlen-1;i>=0;i--) - longpath[i]=path[i]; - longpathlen=pathlen; - } - } - else - { - path[pathlen]=b->data; - pathlen++; - LongPath1(b->lchild,path,pathlen,longpath,longpathlen); - LongPath1(b->rchild,path,pathlen,longpath,longpathlen); - } -} -void AllPath2(BTNode *b) -{ - BTNode *st[MaxSize]; - int top=-1; - BTNode *p,*r; - bool flag; - p=b; - do - { - while(p!=NULL) - { - top++; - st[top]=p; - p=p->lchild; - } - r=NULL; - flag=true; - while(top>=-1&&flag) - { - p=st[top]; - if(p->rchild==r) - { - if(p->lchild==NULL&&p->rchild==NULL) - { - printf("%c到根结点逆路径:",p->data); - for(int i=top;i>0;i--) - printf("%c->",st[i]->data); - printf("%c\n",st[0]->data); - } - top--; - r=p; - } - else - { - p=p->rchild; - flag=false; - } - } - }while(top>-1); -} -void AllPath3(BTNode *b) -{ - struct snode - { - BTNode *node; - int parent; - }Qu[MaxSize]; - int front,rear,p; - front=rear=-1; - rear++; - Qu[rear].node=b; - Qu[rear].parent=-1; - while(frontlchild==NULL&&b->rchild==NULL) - { - printf("%c到根结点逆路径:",b->data); - p=front; - while(Qu[p].parent!=-1) - { - printf("%c->",Qu[p].node->data); - p=Qu[p].parent; - } - printf("%c\n",Qu[p].node->data); - } - if(b->lchild!=NULL) - { - rear++; - Qu[rear].node=b->lchild; - Qu[rear].parent=front; - } - if(b->rchild!=NULL) - { - rear++; - Qu[rear].node=b->rchild; - Qu[rear].parent=front; - } - } -} -int main() -{ - BTNode *b; - ElemType path[MaxSize],longpath[MaxSize]; - int i,longpathlen=0; - CreateBTree(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); - printf("二叉树b:");DispBTree(b);printf("\n"); - printf("先序遍历方法:\n");AllPath1(b,path,0); - LongPath1(b,path,0,longpath,longpathlen); - printf("第一条最长逆路径长度:%d\n",longpathlen); - printf("第一条最长逆路径:"); - for(i=longpathlen-1;i>=0;i--) - printf("%c",longpath[i]); - printf("\n"); - printf("后序非递归遍历方法:\n");AllPath2(b); - printf("层次遍历方法:\n");AllPath3(b); - DestroyBTree(b); - return 1; -} - diff --git "a/2224020112/chapter8/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\202\250\345\255\230.cpp" "b/2224020112/chapter8/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\202\250\345\255\230.cpp" deleted file mode 100644 index 96b81e88ea6f9eb6bbf708d7a5bcb89e5c232296..0000000000000000000000000000000000000000 --- "a/2224020112/chapter8/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\202\250\345\255\230.cpp" +++ /dev/null @@ -1,64 +0,0 @@ -#include -#define MAXSIZE 100 -typedef struct -{ - int n,e; - char V[MAXSIZE]; - int E[MAXSIZE][MAXSIZE]; -} -Graph; - -void InitGraph(Graph *G) -{ - int i,j; - for(i=0; in; i++) - for(j=0; jn; j++) - G->E[i][j]=0; -} - -void CreateGraph(Graph *G) - { - int i,j,k; - char ch1,ch2,X; - printf("请输入图的顶点数目:"); - scanf("%d",&G->n); - printf("请输入图的边的数目:"); - scanf("%d",&G->e); - printf("请输入各顶点的信息:\n"); - for(i=0; in; i++) { - scanf("%c",&X); - printf("输入第%d个顶点:",i+1); - scanf("%c",&(G->V[i])); - } - for(k=0; ke; k++) { - scanf("%c",&X); - printf("建立第%d条边(以逗号隔开):",k+1); - scanf("%c,%c",&ch1,&ch2); - for(i=0; in; i++) - for(j=0; jn; j++) - if(ch1==G->V[i]&&ch2==G->V[j]) { - G->E[i][j]=1; - G->E[j][i]=1; - } - } -} - -void PrintGraph(Graph G) { - int i,j; - for(i=0; i -#include -#define maxn 5005 -#define INF 2147483647 -using namespace std; - -using pii = pair; -vector G[maxn]; -int dis[maxn]; - -int main() -{ - int n, m; - scanf("%d%d", &n, &m); - while(m--) - { - int u, v, w; - scanf("%d%d%d", &u, &v, &w); - G[--u].emplace_back(--v, w); - G[v].emplace_back(u, w); - } - for(int i=1; i, greater> q; - q.emplace(0, 0); - int ans = 0, left = n; - while(!q.empty() && left > 0) - { - auto [d, v] = q.top(); q.pop(); - if(d != dis[v]) continue; - dis[v] = -INF, left --, ans += d; - for(auto [u, w]: G[v]) - if(w < dis[u]) - q.emplace(dis[u] = w, u); - } - if(left) puts("orz"); - else printf("%d\n", ans); - return 0; -} diff --git "a/2224020112/chapter8/\351\207\207\347\224\250\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225\346\261\202\345\270\246\346\235\203\346\234\211\345\220\221\345\233\276\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" "b/2224020112/chapter8/\351\207\207\347\224\250\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225\346\261\202\345\270\246\346\235\203\346\234\211\345\220\221\345\233\276\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" deleted file mode 100644 index 351e3e24c09df886f3d218e38fa1dc2ce383a862..0000000000000000000000000000000000000000 --- "a/2224020112/chapter8/\351\207\207\347\224\250\345\274\227\346\264\233\344\274\212\345\276\267\347\256\227\346\263\225\346\261\202\345\270\246\346\235\203\346\234\211\345\220\221\345\233\276\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" +++ /dev/null @@ -1,223 +0,0 @@ -#include -#include - -#define INF 32767 -#define MAXV 100 - -typedef char InfoType; -typedef struct -{ - int no; - InfoType info; -}VertexType; - -typedef struct -{ - int edges[MAXV][MAXV]; - int n; - int e; - VertexType vexs[MAXV]; -}MatGraph; - -typedef struct ArcNode -{ - int adjvex; - struct ArcNode *nextarc; - int weight; -}ArcNode; - -typedef struct VNode -{ - InfoType info; - int cnt; - ArcNode *firstarc; -}VNode; - -typedef struct -{ - VNode adjlist[MAXV]; - int n; - int e; -}AdjGraph; - -void CreateMat(MatGraph &g, int A[MAXV][MAXV], int n, int e) -{ - int i, j; - - g.n = n; - g.e = e; - for(i = 0; i < g.n; i++) - for(j = 0; j < g.n; j++) - g.edges[i][j] = A[i][j]; -} - -void DispMat(MatGraph g) -{ - int i, j; - for(i = 0; i < g.n; i++) - { - for(j = 0; j < g.n; j++) - { - if(g.edges[i][j] != INF) - printf("%4d", g.edges[i][j]); - else - printf("%4s", "∞"); - } - printf("\n"); - } -} - -void CreateAdj(AdjGraph *&G, int A[MAXV][MAXV], int n, int e) -{ - int i, j; - ArcNode *p; - G = (AdjGraph *)malloc(sizeof(AdjGraph)); - for(i = 0; i < n; i++) - { - G->adjlist[i].firstarc = NULL; - } - for(i = 0; i < n; i++) - { - for(j = n - 1; j >= 0; j--) - { - if(A[i][j] != 0 && A[i][j] != INF) - { - p = (ArcNode *)malloc(sizeof(ArcNode)); - p->adjvex = j; - p->weight = A[i][j]; - p->nextarc = G->adjlist[i].firstarc; - G->adjlist[i].firstarc = p; - } - } - } - G->n = n; - G->e = e; -} - -void DispAdj(AdjGraph *G) -{ - ArcNode *p; - for(int i = 0; i < G->n; i++) - { - p = G->adjlist[i].firstarc; - printf("顶点%d: ", i); - while(p != NULL) - { - printf("%3d[%d]->", p->adjvex, p->weight); - p = p->nextarc; - } - printf("∧\n"); - } -} - -void DestroyAdj(AdjGraph *&G) -{ - ArcNode *pre, *p; - - for(int i = 0; i < G->n; i++) - { - pre = G->adjlist[i].firstarc; - if(pre != NULL) - { - p = pre->nextarc; - while(p != NULL) - { - free(pre); - pre = p; - p = p->nextarc; - } - free(pre); - } - } - free(G); -} - -static void Dispath(MatGraph g, int A[][MAXV], int path[][MAXV]) -{ - int i, j, k, s; - int apath[MAXV], d; - - for(i = 0; i < g.n; i++) - { - for(j = 0; j < g.n; j++) - { - if((i != j) && (A[i][j] != INF)) - { - printf(" 从%d到%d的路径为:", i, j); - k = path[i][j]; - d = 0; apath[d] = j; - while((k != -1) && (k != i)) - { - d++; - apath[d] = k; - k = path[i][k]; - } - d++; apath[d] = i; - printf("%d", apath[d]); - for(s = d - 1; s >= 0; s--) - printf(",%d", apath[s]); - printf(" \t路径长度为:%d\n", A[i][j]); - } - } - } -} - -static void Floyd(MatGraph g) -{ - int A[MAXV][MAXV], path[MAXV][MAXV]; - int i, j, k; - - for(i = 0; i < g.n; i++) - { - for(j = 0; j < g.n; j++) - { - A[i][j] = g.edges[i][j]; - if((i != j) && (g.edges[i][j] < INF)) - { - path[i][j] = i; - } - else - { - path[i][j] = -1; - } - } - } - - for(k = 0; k < g.n; k++) - { - for(i = 0; i < g.n; i++) - { - for(j = 0; j < g.n; j++) - { - if(A[i][j] > A[i][k] + A[k][j]) - { - A[i][j] = A[i][k] + A[k][j]; - path[i][j] = path[k][j]; - } - } - } - } - - Dispath(g, A, path); -} - -int main(void) -{ - MatGraph g; - int A[MAXV][MAXV] = { - {0, 5, INF, 7, INF, INF}, - {INF, 0, 4, INF, INF, INF}, - {8, INF, 0, INF, INF, 9}, - {INF, INF, 5, 0, INF, 6}, - {INF, INF, INF, 5, 0, INF}, - {3, INF, INF, INF, 1, 0} - }; - int n = 6; - int e = 10; - CreateMat(g, A, n, e); - printf("有向图G的邻接矩阵:\n"); - DispMat(g); - printf("弗洛伊德算法求解结果:\n"); - Floyd(g); - return 0; -} diff --git "a/2224020112/chapter9/\345\256\236\347\216\260\346\212\230\345\215\212\346\237\245\346\211\276\347\232\204\347\256\227\346\263\225.cpp" "b/2224020112/chapter9/\345\256\236\347\216\260\346\212\230\345\215\212\346\237\245\346\211\276\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index b968a354d3f79a9a0a1ee9d4975150a4f46b83e3..0000000000000000000000000000000000000000 --- "a/2224020112/chapter9/\345\256\236\347\216\260\346\212\230\345\215\212\346\237\245\346\211\276\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,67 +0,0 @@ -#include -#define MAX_LEN (100) - -typedef int key_type; -typedef char info_type; -typedef struct -{ - key_type key; - info_type data; -} -rec_type; - -void create_list(rec_type records[], key_type keys[], int n) -{ - int i; - for(i = 0; i < n; i++) - records[i].key = keys[i]; -} - -void disp_list(rec_type records[], int n) -{ - int i; - for(i = 0; i < n; i++) - printf("%d ", records[i].key); - printf("\n"); -} - -static int bin_search(rec_type records[], int n, key_type key) -{ - int low = 0; - int high = n - 1; - int mid; - int cnt = 0; - while(low <= high) - { - mid = (low + high) / 2; - printf(" 第%d次比较:在[%d,%d]中比较元素records[%d]:%d\n", ++cnt, low, high, mid, records[mid].key); - if(records[mid].key == key) - return mid + 1; - if(records[mid].key > key) - high = mid - 1; - else - low = mid + 1; - } - - return -1; -} - -int main(void) -{ - rec_type records[MAX_LEN]; - int i; - int n = 10; - key_type key = 9; - int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - - create_list(records, a, n); - printf("关键字序列:"); - disp_list(records, n); - printf("查找%d的比较过程如下:\n", key); - i = bin_search(records, n, key); - if(i != -1) - printf("元素%d的位置是%d\n", key, i); - else - printf("元素%d不在表中\n", key); - return 0; -} \ No newline at end of file diff --git a/2224020113/chapter3/exp3_1.cpp b/2224020113/chapter3/exp3_1.cpp deleted file mode 100644 index 2f74631ae38605c9d4c8aeca473d5bd32ea64a67..0000000000000000000000000000000000000000 --- a/2224020113/chapter3/exp3_1.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct -{ - ElemType date[MaxSize]; - int top; -}SqStack; -void InitStack(SqStack *&s) -{ - s=(SqStack * )malloc(sizeof(SqStack)); - s->top=-1; -} -void DestroyStack(SqStack * &s) -{ - free(s); -} -bool StackEmpty(SqStack * s) -{ - return(s->top==-1); -} -bool Push(SqStack * &s,ElemType e) -{ - if(s->top==MaxSize-1) - return false; - s->top++; - s->date[s->top]=e; - return true; -} -bool Pop(SqStack * &s,ElemType &e) -{ - if(s->top==-1) - return false; - e=s->date[s->top]; - s->top--; - return true; -} -bool GetTop(SqStack *s,ElemType &e) -{ - if(s->top==-1) - return false; - e=s->date[s->top]; - return true; \ No newline at end of file diff --git a/2224020113/chapter3/exp3_3.cpp b/2224020113/chapter3/exp3_3.cpp deleted file mode 100644 index 1226e85235546941fb74d3cc2c33277573d0cc1e..0000000000000000000000000000000000000000 --- a/2224020113/chapter3/exp3_3.cpp +++ /dev/null @@ -1,39 +0,0 @@ -//实现环形队列的各种基本运算的算法 -#include -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct -{ - ElemType data[MaxSize]; - int front,rear; -}SqQueue; -void InitQueue(SqQueue * &q) -{ - q=(SqQueue *)malloc(sizeof(SqQueue)); - q -> front = q -> rear = 0; -} -void DestroyQueue(SqQueue * &q) -{ - free(q); -} -bool QueueEmpty(SqQueue * q) -{ - return(q -> front == q -> rear); -} -bool enQueue(SqQueue * &q,ElemType e) -{ - if ((q -> rear + 1)%MaxSize == q -> front) - return false; - q -> rear = (q -> rear + 1)%MaxSize; - q -> data[q -> rear] = e; - return true; -} -bool deQueue(SqQueue * &q,ElemType &e) -{ - if (q -> front == q -> rear) - return false; - q -> front = (q -> front +1 )%MaxSize; - e= q -> data[q -> front]; - return true; -} \ No newline at end of file diff --git a/2224020113/chapter3/exp3_8.cpp b/2224020113/chapter3/exp3_8.cpp deleted file mode 100644 index 29f94acce0b7b99709fa585752d2e64d3450d7e0..0000000000000000000000000000000000000000 --- a/2224020113/chapter3/exp3_8.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include -#define MaxSize 100 -typedef struct -{ int col[MaxSize]; - int top; -} StackType; -void dispasolution(StackType St) -{ static int count=0; - printf(" 第%d个解:",++count); - for (int i=1;i<=St.top;i++) - printf("(%d,%d) ",i,St.col[i]); - printf("\n"); -} -bool place(StackType St,int k,int j) -{ int i=1; - if (k==1) return true; - while (i<=k-1) - { if ((St.col[i]==j) || (abs(j-St.col[i])==abs(i-k))) - return false; - i++; - } - return true; -} -void queen(int n) -{ - int k; - bool find; - StackType St; - St.top=0; - St.top++; St.col[St.top]=0; - while (St.top!=0) - { k=St.top; - find=false; - for (int j=St.col[k]+1;j<=n;j++) - if (place(St,k,j)) - { St.col[St.top]=j; - find=true; - break; - } - if (find) - { if (k==n) - dispasolution(St); - else - { St.top++; - St.col[St.top]=0; - } - } - else - St.top--; - } -} -int main() -{ int n; - printf("皇后问题(n<20) n="); - scanf("%d",&n); - if (n>20) - printf("n值太大\n"); - else - { printf(" %d皇后问题求解如下:\n",n); - queen(n); - } - return 1; -} diff --git a/2224020113/chapter5/exp5-1.cpp b/2224020113/chapter5/exp5-1.cpp deleted file mode 100644 index 8c421b794b5826eff5045a6e8875962f5551529a..0000000000000000000000000000000000000000 --- a/2224020113/chapter5/exp5-1.cpp +++ /dev/null @@ -1,66 +0,0 @@ -//非递归算法 -def minre(a): - if len(a)==0: - return 1000 - else: - return min(a) -def Hanoi(n,a,b,c): - a=list(range(n,0,-1)) - lena=len(a) - i=1 - while len(b)!=lena: - - if(i%2!=0): - if minre(a)==1: - a.remove(1) - b.append(1) - if(len(b)==lena): - break - elif minre(b)==1: - b.remove(1) - c.append(1) - elif min(c)==1: - c.remove(1) - a.append(1) - else: - if minre(a)==1: - if minre(b)0: - Hanoi(n-1, a, c, b) - b=move(a,b) - Hanoi(n-1, c, b, a) -def Hanoire(n,a,b,c): - a=list(range(1,n+1)) - Hanoi(n,a,b,c) -a=list() -b=list() -c=list() -Hanoire(10, a, b, c) diff --git a/2224020113/chapter5/exp5-3.cpp b/2224020113/chapter5/exp5-3.cpp deleted file mode 100644 index e8721bb5da0c6ce73152302b916d243f2ca8d707..0000000000000000000000000000000000000000 --- a/2224020113/chapter5/exp5-3.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#define MaxSize 100 -#include -typedef struct -{ - char data[MaxSize]; - int length; -}IP; -void addch(IP&ip,char ch) -{ - ip.data[ip.length]=ch; - ip.length++; -} -IP addot(IP ip) -{ - addch(ip,'.'); - return ip; -} -void solveip(char s[],int n,int start,int step,IP ip) -{ - if(start<=n) - { - if(start==n&&step==4) - { - for(int i=0;i -#include -#define N 20; -using namespace std; -int q[20]; //存放个皇后所在的列号 -int count_1 = 0; //累计解个数 - -void dispasolution(int n) {//输出n皇后问题的一个解 - cout << "第" << ++count_1 << "个解:"; - for (int i = 1; i <= n; ++i) { - cout << "(" << i << "," << q[i] << ") "; - } - cout << endl; -} - -bool place(int i, int j) {//测试(i,j)位置能否放置皇后(第i行) - if (i == 1) return true; - int k = 1; - while (k < i) { - if (q[k] == j || (abs(i - k) == abs(j - q[k]))) {//判断当前位置是否可以放置皇后 - return false; - } - k++; - } - return true; -} - -void queen(int i, int n) {//放置第i行的皇后 - if (i > n) dispasolution(n); //递归出口,输出n皇后 - else { - for (int j = 1; j <= n; ++j) {//每次放置皇后从左到右依次遍历并判断 - if (place(i, j)) { //(i,j)可以放置 - q[i] = j; //记录皇后位置 - queen(i + 1, n); //递归放置下一行的皇后 - } - } - } -} - -int main() { - int n; - cout << "皇后问题(n<20)n="; - cin >> n; - if (n >= 20) cout << "n值太大,不能求解" << endl; - else cout << n << "皇后问题求解如下:" << endl; - queen(1, n); //放置1~n行的皇后 - return 0; -} diff --git a/2224020115/chapter1/exp2-10.cpp b/2224020115/chapter1/exp2-10.cpp deleted file mode 100644 index c9c2c53f51d413ea90b576d69e3efa9005fad3b4..0000000000000000000000000000000000000000 --- a/2224020115/chapter1/exp2-10.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include -using namespace std; - -struct PolyNode { - int coef; - int exp; - PolyNode *next; -}; - -PolyNode* polyMultiply(PolyNode *p1, PolyNode *p2) { - PolyNode *result = new PolyNode(); - PolyNode *p = p2; - while (p1 != NULL) { - p = p2; - while (p != NULL) { - int coef = p1->coef * p->coef; - int exp = p1->exp + p->exp; - PolyNode *q = result; - while (q->next != NULL && q->next->exp > exp) { - q = q->next; - } - if (q->next != NULL && q->next->exp == exp) { - q->next->coef += coef; - } - else { - PolyNode *newNode = new PolyNode(); - newNode->coef = coef; - newNode->exp = exp; - newNode->next = q->next; - q->next = newNode; - } - p = p->next; - } - p1 = p1->next; - } - return result->next; -} - -void printPoly(PolyNode *p) { - if (p == NULL) { - cout << "0 0" << endl; - return; - } - while (p != NULL) { - cout << p->coef << " " << p->exp << " "; - p = p->next; - } - cout << endl; -} - -int main() { - PolyNode *p1 = new PolyNode(); - PolyNode *p1_1 = new PolyNode(); - PolyNode *p1_2 = new PolyNode(); - p1->coef = 3; - p1->exp = 2; - p1->next = p1_1; - p1_1->coef = 1; - p1_1->exp = 1; - p1_1->next = p1_2; - p1_2->coef = 2; - p1_2->exp = 0; - p1_2->next = NULL; - PolyNode *p2 = new PolyNode(); - PolyNode *p2_1 = new PolyNode(); - PolyNode *p2_2 = new PolyNode(); - p2->coef = 4; - p2->exp = 1; - p2->next = p2_1; - p2_1->coef = 1; - p2_1->exp = 0; - p2_1->next = p2_2; - p2_2->coef = 3; - p2_2->exp = 2; - p2_2->next = NULL; - cout << "p1: "; - printPoly(p1); - cout << "p2: "; - printPoly(p2); - PolyNode *result = polyMultiply(p1, p2); - cout << "result: "; - printPoly(result); - return 0; -} \ No newline at end of file diff --git a/2224020115/chapter1/exp2-6.cpp b/2224020115/chapter1/exp2-6.cpp deleted file mode 100644 index 233fdcd22be2fb450e7cc3f1c3872e561de4a481..0000000000000000000000000000000000000000 --- a/2224020115/chapter1/exp2-6.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include -using namespace std; - -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - -class Solution { -public: - ListNode* partition(ListNode* head, int x) { - ListNode* lessHead = new ListNode(0); - ListNode* greaterHead = new ListNode(0); - ListNode* less = lessHead; - ListNode* greater = greaterHead; - while (head) { - if (head->val < x) { - less->next = head; - less = less->next; - } else { - greater->next = head; - greater = greater->next; - } - head = head->next; - } - greater->next = NULL; - less->next = greaterHead->next; - return lessHead->next; - } -}; - -int main() { - ListNode* head = new ListNode(1); head->next = new ListNode(4); - head->next->next = new ListNode(3); - head->next->next->next = new ListNode(2); - head->next->next->next->next = new ListNode(5); - head->next->next->next->next->next = new ListNode(2); - Solution s; - ListNode* res = s.partition(head, 3); - while (res) { - cout << res->val << " "; - res = res->next; - } - return 0; -} \ No newline at end of file diff --git a/2224020115/chapter1/lc-88.cpp b/2224020115/chapter1/lc-88.cpp deleted file mode 100644 index 306e297fae5a1ef8576cee63c1b217f62445f961..0000000000000000000000000000000000000000 --- a/2224020115/chapter1/lc-88.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -using namespace std; - -void merge(vector& nums1, int m, vector& nums2, int n) { - int i = m - 1, j = n - 1, k = m + n - 1; - while (i >= 0 && j >= 0) { - if (nums1[i] > nums2[j]) { - nums1[k--] = nums1[i--]; - } else { - nums1[k--] = nums2[j--]; - } - } - while (j >= 0) { - nums1[k--] = nums2[j--]; - } -} - -int main() { - vector nums1 = {1, 2, 3, 0, 0, 0}; - vector nums2 = {2, 5, 6}; - int m = 3, n = 3; - merge(nums1, m, nums2, n); - for (int i = 0; i < m + n; i++) { - cout << nums1[i] << " "; - } - return 0; -} \ No newline at end of file diff --git a/2224020116/exp2-6.cpp b/2224020116/exp2-6.cpp deleted file mode 100644 index 8934279d606415420bd08a6d5efe6b804ceccca2..0000000000000000000000000000000000000000 --- a/2224020116/exp2-6.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include -using namespace std; - -// 定义单链表结点 -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(NULL) {} -}; - -// 将单链表按基准划分 -ListNode* partition(ListNode* head, int x) { - ListNode *small = new ListNode(0); // 小于x的结点链表 - ListNode *large = new ListNode(0); // 大于等于x的结点链表 - ListNode *p = head, *p1 = small, *p2 = large; - while (p != NULL) { - if (p->val < x) { - p1->next = p; - p1 = p1->next; - } else { - p2->next = p; - p2 = p2->next; - } - p = p->next; - } - p2->next = NULL; - p1->next = large->next; - return small->next; -} - -// 测试 -int main() { - ListNode *head = new ListNode(1); - head->next = new ListNode(4); head->next->next = new ListNode(3); - head->next->next->next = new ListNode(2); - head->next->next->next->next = new ListNode(5); - head = partition(head, 3); - while (head != NULL) { - cout << head->val << " "; - head = head->next; - } - return 0; -} \ No newline at end of file diff --git "a/2224020117/10.27\345\210\251\347\224\250KMP\347\256\227\346\263\225\346\261\202\345\255\220\344\270\262\345\234\250\344\270\273\344\270\262\344\270\255\344\270\215\351\207\215\345\217\240\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" "b/2224020117/10.27\345\210\251\347\224\250KMP\347\256\227\346\263\225\346\261\202\345\255\220\344\270\262\345\234\250\344\270\273\344\270\262\344\270\255\344\270\215\351\207\215\345\217\240\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" deleted file mode 100644 index b80aa1392420d4db6d0cfdd566a4215cb3ee338e..0000000000000000000000000000000000000000 --- "a/2224020117/10.27\345\210\251\347\224\250KMP\347\256\227\346\263\225\346\261\202\345\255\220\344\270\262\345\234\250\344\270\273\344\270\262\344\270\255\344\270\215\351\207\215\345\217\240\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" +++ /dev/null @@ -1,81 +0,0 @@ -#include -#include -#include -using namespace std; - -void fail(const char* pat, int* f){ - const int len=strlen(pat); - f[0]=-1; - for (int j=1; j < len; j++){ - if (pat[j] == pat[f[j-1]+1]) - f[j] = f[j-1]+1; - else { - int i=f[j-1]; - while ( i >=0 && pat[j] != pat[i+1] ) - i = f[i]; - if (pat[j] ==pat[i+1]) - f[j] = i+1; - else - f[j] = -1; - } - } -} - -int search(const char* tg, const char *pt, const int* f){ - const int ptLen=strlen(pt); - const int tgLen = strlen(tg); - int tgPos=0,ptPos=0; - for (; tgPos < tgLen && ptPos < ptLen;) { - if (tg[tgPos] == pt[ptPos]){ - ptPos++; - tgPos++; - } - else { - if (ptPos == 0) - tgPos ++; - else - ptPos = f[ptPos-1] + 1; - } - } - return ptPos < ptLen ? -1: tgPos - ptLen; -} - -int count(const char* tg, const char *pt, const int* f){ - const int ptLen=strlen(pt); - const int tgLen = strlen(tg); - int tgPos=0,ptPos=0,count=0; - for (; tgPos < tgLen;) { - if (tg[tgPos] == pt[ptPos]){ - ptPos++; - tgPos++; - } - else { - if (ptPos == 0) - tgPos ++; - else - ptPos = f[ptPos-1] + 1; - } - if (ptPos >= ptLen ){ - count ++; - ptPos=f[ptLen-1] +1; - } - } - return count; -} - -int main() -{ - const char *tag="abcbcbcbc"; - const char *pat="bcbc"; - int len=strlen(pat); - int *f =new int[len]; - fail(pat,f); - cout <<"失效函数位置:\n"; - for(int i=0;i -#include -#define MaxSize 50 -typedef char ElemType; -char w; -typedef struct -{ - ElemType data[MaxSize]; - int front,rear; -}SqQueue; -void InitQueue(SqQueue* &q) -{ - q=(SqQueue*)malloc(sizeof(SqQueue)); - q->front=q->rear=0; -} - -bool enQueue(SqQueue* &q,ElemType e) -{ - if((q->rear+1)%MaxSize==q->front) - return false; - q->rear=(q->rear+1)%MaxSize; - q->data[q->rear]=e; - return true; -} -bool QueueEmpty(SqQueue *q) -{ - return(q->front==q->rear); -} - -bool deQueue(SqQueue* &q,ElemType&e) -{ - if(q->front==q->rear) - return false; - q->front=(q->front+1)%MaxSize; - e=q->data[q->front]; - return true; -} - -void DestroyQueue(SqQueue* &q) -{ - free(q); -} - - -int main() -{ - SqQueue *q; - printf("计算机 小淇在敲代码 \n"); - printf("环形队列的各种基本运算的算法 \n"); - - printf("(1)初始化环形队列 \n"); - InitQueue(q); - - printf("(2)依次进队元素a、b、c \n"); - if(!enQueue(q,'a')) - printf("\t提示:队满,不能进队\n"); - if(!enQueue(q,'b')) - printf("\t提示:队满,不能进队\n"); - if(!enQueue(q,'c')) - printf("\t提示:队满,不能进队\n"); - - printf("(3)判断环形队列q是否非空: %s\n",(QueueEmpty(q)?"空":"非空")); - if(deQueue(q,w)==0) - printf("队空,不能出列\n"); - else - printf("(4)出队一个元素,输出该元素: %c\n",w); - - printf("(5)依次进队元素d、e、f \n"); - if(!enQueue(q,'d')) - printf("\t提示:队满,不能进队\n"); - if(!enQueue(q,'e')) - printf("\t提示:队满,不能进队\n"); - if(!enQueue(q,'f')) - printf("\t提示:队满,不能进队\n"); - - printf("(6)输出出队序列: \n"); - while(!QueueEmpty(q)) - { - deQueue(q,w); - printf("%c",w); - } - printf("\n"); - printf("(7)释放队列 \n"); - DestroyQueue(q); - return 0; -} \ No newline at end of file diff --git "a/2224020117/10.27\345\256\236\347\216\260\351\241\272\345\272\217\344\270\262\347\232\204\345\220\204\347\247\215\346\250\241\345\274\217\345\214\271\351\205\215\347\256\227\346\263\225.cpp" "b/2224020117/10.27\345\256\236\347\216\260\351\241\272\345\272\217\344\270\262\347\232\204\345\220\204\347\247\215\346\250\241\345\274\217\345\214\271\351\205\215\347\256\227\346\263\225.cpp" deleted file mode 100644 index 7bd128ee3d4378fb84c3fa4c38935cd7ce8bf926..0000000000000000000000000000000000000000 --- "a/2224020117/10.27\345\256\236\347\216\260\351\241\272\345\272\217\344\270\262\347\232\204\345\220\204\347\247\215\346\250\241\345\274\217\345\214\271\351\205\215\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,141 +0,0 @@ -#include -#include - -typedef struct String { - char* data; - int length; -}Str; - -void StrAssign(Str&, char*); -int SubStr(Str, Str); -void GetNext(Str, int*&); -void GetNextval(Str, int*&); -int KMP(Str, Str, int*); -int main(int argc, const char* argv[]) -{ - char s[] = "abcabcdabcdeabcdefabcdefg"; - char t[] = "abcdeabcdefab"; - Str S, T; - int i,idx; - int* next = NULL, * nextval = NULL; - - StrAssign(S, s); - StrAssign(T, t); - printf("简单匹配的结果(从1开始):\n"); - idx = SubStr(S, T); - if (idx != -1) { - printf("匹配成功,位置为%d。\n", idx); - } - else { - printf("匹配失败。\n"); - } - GetNext(T, next); - printf("next数组:\n"); - for (i = 0; i < T.length; i++) { - printf("%d\t", next[i]); - } - printf("\n"); - GetNextval(T, nextval); - printf("nextval数组:\n"); - for (i = 0; i < T.length; i++) { - printf("%d\t", nextval[i]); - } - printf("\n"); - printf("使用KMP的结果(从1开始):\n"); - idx = KMP(S, T, next); - if (idx != -1) { - printf("匹配成功,位置为%d。\n", idx); - } - else { - printf("匹配失败。\n"); - } - printf("使用改进KMP的结果(从1开始):\n"); - idx = KMP(S, T, nextval); - if (idx != -1) { - printf("匹配成功,位置为%d。\n", idx); - } - else { - printf("匹配失败。\n"); - } - return 0; -} -void StrAssign(Str& S, char* s) -{ - int i; - for (i = 0; s[i] != '\0'; i++); - S.length = i; - S.data = s; -} -int SubStr(Str S, Str T) -{ - int i, j; - for (i = 0; i < S.length; i++) { - for (j = 0; j < T.length; j++) { - if (S.data[i + j] != T.data[j]) - break; - } - if (j >= T.length) - return (i+1); - } - return -1; -} -void GetNext(Str T, int*& next) -{ - int i, j; - i = 0; - j = -1; - next = (int*)malloc(T.length * sizeof(int)); - while (next == NULL) { - next = (int*)malloc(T.length * sizeof(int)); - } - next[0] = -1; - while (i < T.length) { - if (j == -1 || T.data[i] == T.data[j]) { - i++; - j++; - next[i] = j; - } - else - j = next[j]; - } -} -void GetNextval(Str T, int*& nextval) -{ - int i, j; - i = 0; - j = -1; - nextval = (int*)malloc(T.length * sizeof(int)); - while (nextval == NULL) { - nextval = (int*)malloc(T.length * sizeof(int)); - } - nextval[0] = -1; - while (i < T.length) { - if (j == -1 || T.data[i] == T.data[j]) { - i++; - j++; - if (T.data[i] != T.data[j]) - nextval[i] = j; - else - nextval[i] = nextval[j]; - } - else - j = nextval[j]; - } -} -int KMP(Str S, Str T, int* N) -{ - int i, j; - i = j = 0; - while (i < S.length && j < T.length) { - if (j == -1 || S.data[i] == T.data[j]) { - i++; - j++; - } - else - j = N[j]; - } - if (j >= T.length) - return (i+1-T.length); - else - return -1; -} diff --git "a/2224020117/10.27\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2224020117/10.27\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index eda18f596a5fbec50178031ae2bff6d9b4eab64f..0000000000000000000000000000000000000000 --- "a/2224020117/10.27\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,75 +0,0 @@ -#include -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct -{ - ElemType data[MaxSize]; - int top; -} SqStack; -void InitStack(SqStack *&s) -{ - s=(SqStack *)malloc(sizeof(SqStack)); - s->top=-1; -} -void DestroyStack(SqStack *&s) -{ - free(s); -} -bool StackEmpty(SqStack *s) -{ - return (s->top == -1); -} - -bool Push(SqStack *&s,ElemType e) -{ - if(s->top==MaxSize-1) - return false; - s->top++; - s->data[s->top]=e; - return true; -} -bool Pop(SqStack *&s,ElemType &e) -{ - if(s->top==MaxSize-1) - return false; - e=s->data[s->top]; - s->top--; - return true; -} - -bool GetTop(SqStack *s,ElemType &e) -{ - if(s->top == -1) - return false; - e=s->data[s->top]; - return true; -} -int main() -{ - ElemType e; - SqStack *s; - printf("(1)初始化栈s\n"); - InitStack(s); - printf("(2)判断栈s是否非空:%s\n",(StackEmpty(s)? "是":"否")); - printf("(3)依次进栈元素a、b、c、d、e\n"); - Push(s,'a'); - Push(s,'b'); - Push(s,'c'); - Push(s,'d'); - Push(s,'e'); - printf("(4)判断栈s是否非空:%s\n",((StackEmpty(s))? "是":"否")); - printf("(5)输出出栈序列"); - while(!StackEmpty(s)) //相当于StackEmpty() != NULL - { - Pop(s,e); - printf("%c ",e); - } - printf("\n"); - printf("(6)判断栈s是否非空:%s\n",((StackEmpty(s))? "是":"否")); - GetTop(s,e); - printf("(7)取栈顶元素:%c\n",e); - printf("(8)释放栈"); - DestroyStack(s); - return 0; -} diff --git "a/2224020117/10.27\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" "b/2224020117/10.27\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" deleted file mode 100644 index 7a9591ebea9e9e228dae586cf0a7a27b1f93a26c..0000000000000000000000000000000000000000 --- "a/2224020117/10.27\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" +++ /dev/null @@ -1,43 +0,0 @@ -function findLongestRepeatedSubstring(str) { - const n = str.length; - const suffixArr = new Array(n); - for (let i = 0; i < n; i++) { - suffixArr[i] = i; - } - suffixArr.sort((a, b) => { - while (a < n && b < n) { - if (str[a] !== str[b]) { - return str[a].charCodeAt() - str[b].charCodeAt(); - } - a++; - b++; - } - return b - a; - }); - const lcpArr = new Array(n); - let k = 0; - for (let i = 0; i < n; i++) { - if (suffixArr[i] === 0) { - lcpArr[suffixArr[i]] = 0; - continue; - } - const j = suffixArr[i - 1]; - while (i + k < n && j + k < n && str[i + k] === str[j + k]) { - k++; - } - lcpArr[suffixArr[i]] = k; - k = Math.max(k - 1, 0); - } - let maxLen = 0; - let maxIndex = 0; - for (let i = 1; i < n; i++) { - if (lcpArr[i] > maxLen) { - maxLen = lcpArr[i]; - maxIndex = i; - } - } - return str.substring(suffixArr[maxIndex], suffixArr[maxIndex] + maxLen); -} - -console.log(findLongestRepeatedSubstring('banana')); - \ No newline at end of file diff --git "a/2224020117/10.27\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" "b/2224020117/10.27\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" deleted file mode 100644 index 842423b2503e2640601f5c1d9bed71f5e4672e9b..0000000000000000000000000000000000000000 --- "a/2224020117/10.27\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include -#include -#define MaxSize 100 -typedef struct -{ - int data[MaxSize]; - int top; -} StType; -int count=0; -int place(StType st,int i,int j) -{ - int a=0; - int k=1; - if (i==1) - { - a=1; - return a; - } - while (k<=i-1) - { - if ((st.data[k]==j)||(fabs(j-st.data[k])==fabs(i-k))) - { - a=0; - return a; - } - else - k++; - } - a=1; - return a; -} -void queen(int n) -{ - int i,j,k; - int find=0; - StType st; - st.top=0; - st.top++; - st.data[st.top]=1; - while (st.top>0) - { - i=st.top; - if (st.top==n) - { - printf("第%d个解:",++count); - for (k=1; k<=st.top; k++) - printf("(%d,%d) ",k,st.data[k]); - printf("\n"); - } - find=0; - for (j=1; j<=n; j++) - if (place(st,i+1,j)) - { - st.top++; - st.data[st.top]=j; - find=1; - break; - } - if (find==0) - { - while (st.top>0) - { - if (st.data[st.top]==n) - st.top--; - for (j=st.data[st.top]+1; j<=n; j++) - if (place(st,st.top,j)) - { - st.data[st.top]=j; - break; - } - if (j>n) - st.top--; - else - break; - } - } - } -} - -int main() -{ - int n; - printf(" 皇后问题(n<20) n="); - scanf("%d",&n); - printf(" %d皇后问题求解如下:\n",n); - queen(n); - printf("\n"); - return 0; -} \ No newline at end of file diff --git "a/2224020117/11.10\346\201\242\345\244\215IP\345\234\260\345\235\200.cpp" "b/2224020117/11.10\346\201\242\345\244\215IP\345\234\260\345\235\200.cpp" deleted file mode 100644 index 34c844b83e35bac9381a6ae29ce0b4ae7939c068..0000000000000000000000000000000000000000 --- "a/2224020117/11.10\346\201\242\345\244\215IP\345\234\260\345\235\200.cpp" +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; -string s; -set result; -void getIP(string temp,int st,int en,int time)//获取IP地址 -{ - if(en>s.size()) - return ; - if(time>3) - return ; - string sub=s.substr(st,en-st); - if(sub[0]=='0'&&sub.size()>1) - return ; - int num=atoi(sub.substr(0,en-st).c_str()); - if(num>255) - return ; - temp+=sub; - if(en!=s.size()) - temp+="."; - else - { - if(time==3) - result.insert(temp); - return ; - } - getIP(temp,en,en+1,time+1); - getIP(temp,en,en+2,time+1); - getIP(temp,en,en+3,time+1); -} -void printfIP()//输出结果 -{ - if(result.empty()) - { - printf("输入不合法\n"); - } - else - { - set ::iterator a; - printf("可能有效的IP地址有:\n"); - for(a=result.begin();a!=result.end();a++) - cout<<*a<>s; - string temp=""; - getIP(temp,0,1,0); - getIP(temp,0,2,0); - getIP(temp,0,3,0); - printfIP(); - printf("继续请输入1,退出操作请输入0\n"); - cin>>num; - if(num==0) - break; - else - continue; - } - return 0; -} \ No newline at end of file diff --git "a/2224020117/11.10\347\224\250\351\200\222\345\275\222\347\232\204\346\226\271\346\263\225\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" "b/2224020117/11.10\347\224\250\351\200\222\345\275\222\347\232\204\346\226\271\346\263\225\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" deleted file mode 100644 index a61182e7f1a837886f8c5c44ec4b82afef1fa6da..0000000000000000000000000000000000000000 --- "a/2224020117/11.10\347\224\250\351\200\222\345\275\222\347\232\204\346\226\271\346\263\225\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include -#define N 20; -using namespace std; -int q[20]; -int count_1 = 0; - -void dispasolution(int n) - cout << "第" << ++count_1 << "个解:"; - for (int i = 1; i <= n; ++i) { - cout << "(" << i << "," << q[i] << ") "; - } - cout << endl; -} - -bool place(int i, int j) - if (i == 1) return true; - int k = 1; - while (k < i) { - if (q[k] == j || (abs(i - k) == abs(j - q[k]))) { - return false; - } - k++; - } - return true; -} - -void queen(int i, int n) { - if (i > n) dispasolution(n); - else { - - for (int j = 1; j <= n; ++j) { - if (place(i, j)) { - q[i] = j; - queen(i + 1, n); - } - } - } -} - -int main() { - int n; - cout << "皇后问题(n<20)n="; - cin >> n; - if (n >= 20) cout << "n值太大,不能求解" << endl; - else cout << n << "皇后问题求解如下:" << endl; - queen(1, n); - return 0; -} \ No newline at end of file diff --git "a/2224020117/11.10\351\207\207\347\224\250\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243hanoi\351\227\256\351\242\230.cpp" "b/2224020117/11.10\351\207\207\347\224\250\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243hanoi\351\227\256\351\242\230.cpp" deleted file mode 100644 index 82496052dc6e1b5604cb2f953788efc2bcb0b1ef..0000000000000000000000000000000000000000 --- "a/2224020117/11.10\351\207\207\347\224\250\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243hanoi\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,18 +0,0 @@ -include -void hanoi(int n, char a, char b, char c) -{ - if (n > 0) - { - hanoi(n - 1, a, c, b); - printf("move dish %d form pile %c to %c", n, a, b); - printf("\n"); - hanoi(n - 1, c, b, a); - } -} -int main() -{ - int n; - scanf("%d", &n); - hanoi(n, 'A', 'B', 'C'); - return 0; -} \ No newline at end of file diff --git "a/2224020117/11.10\351\207\207\347\224\250\351\235\236\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243hanoi\351\227\256\351\242\230.cpp" "b/2224020117/11.10\351\207\207\347\224\250\351\235\236\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243hanoi\351\227\256\351\242\230.cpp" deleted file mode 100644 index 70dda01537b7b6b5d5a5c52c97554eb6e843c922..0000000000000000000000000000000000000000 --- "a/2224020117/11.10\351\207\207\347\224\250\351\235\236\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243hanoi\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,81 +0,0 @@ -**源代码** -#include -#define MAXSIZE 100 - -typedef struct -{ - int flag; - int num; - char A, B, C; -}Stack; - -void hanoi(int n, char A, char B, char C) -{ - int top = 1; - int n1 = 0; - int a, b, c; - Stack stack[MAXSIZE]; - stack[top].flag = 1; - stack[top].num = n; - stack[top].A = A; - stack[top].B = B; - stack[top].C = C; - - while (top > 0) - { - if (stack[top].flag == 1) - { - /*hanoi(n,a,b,c)退栈,相当于在递归函数中实参传递给形参*/ - n1 = stack[top].num; - a = stack[top].A; - b = stack[top].B; - c = stack[top].C; - /*hanoi(n-1,a,c,b)入栈,相当于在递归函数中的第一个递归调用 - 函数,将编号1->n-1的圆盘从塔座A移到C,B做辅助底座*/ - stack[top].flag = 1; - stack[top].num = n1 - 1; - stack[top].A = c; - stack[top].B = b; - stack[top].C = a; - top++; - /*将第n个圆盘从A移到B*/ - stack[top].flag = 0; - stack[top].num = n1; - stack[top].A = a; - stack[top].B = b; - top++; - /*hanoi(n-1,b,a,c)入栈,相当于在递归函数中的第一个递归函数调用 - 函数,将编号1->n-1的圆盘从塔座C移到B,A做辅助底座*/ - stack[top].flag = 1; - stack[top].num = n1 - 1; - stack[top].A = a; - stack[top].B = c; - stack[top].C = b; - } - while (top > 0 && stack[top].flag == 0 || stack[top].num == 1) - { - /*将第n个圆盘从A移到B并退栈*/ - if (top > 0 && stack[top].flag == 0) - { - /*\ "\+Enter"续行符*/ - printf("Move dish %d form pile %c to %c\n", stack[top].num,\ - stack[top].A, stack[top].B); - top--; - } - /*将第一个圆盘从A移动到B,并退栈*/ - if (top > 0 && stack[top].num == 1) - { - printf("Move dish %d form pile %c to %c\n", stack[top].num,\ - stack[top].A, stack[top].B); - top--; - } - } - } -} -int main() -{ - int n = 0; - scanf("%d", &n); - hanoi(n, 'A', 'B', 'C'); - return 0; -} \ No newline at end of file diff --git "a/2224020117/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2224020117/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index 080dac32e114ea5fed8a132ba9a73ecb4f7bbfc4..0000000000000000000000000000000000000000 --- "a/2224020117/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,36 +0,0 @@ -#include - -void findTwoSum(int arr[], int size, int target) { - for (int i = 0; i < size - 1; i++) { - for (int j = i + 1; j < size; j++) { - if (arr[i] + arr[j] == target) { - printf("找到两数之和为%d的解:%d(下标:%d) + %d(下标:%d)\n", target, arr[i], i, arr[j], j); - return; - } - } - } - printf("未找到满足条件的解。\n"); -} - -int main() { - int size; - - printf("请输入数组的大小:"); - scanf("%d", &size); - - int arr[size]; - - printf("请输入数组元素:"); - for (int i = 0; i < size; i++) { - scanf("%d", &arr[i]); - } - - int target; - - printf("请输入目标和:"); - scanf("%d", &target); - - findTwoSum(arr, size, target); - - return 0; -} \ No newline at end of file diff --git "a/2224020117/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" "b/2224020117/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" deleted file mode 100644 index 7142e948afc7c81be49e274b2508babcb0250290..0000000000000000000000000000000000000000 --- "a/2224020117/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,88 +0,0 @@ -#include -#include -typedef struct PolyItem { - int coef; - int exp; -} PolyItem; - -typedef struct ListNode { - PolyItem *item; - struct ListNode *next; -} ListNode; -PolyItem *createPolyItem(int coef, int exp) { - PolyItem *item = (PolyItem *)malloc(sizeof(PolyItem)); - item->coef = coef; - item->exp = exp; - return item; -} -void insertPolyItem(ListNode **head, PolyItem *item) { - ListNode *node = (ListNode *)malloc(sizeof(ListNode)); - node->item = item; - node->next = NULL; - if (*head == NULL) { - *head = node; - } else { - ListNode *p = *head; - while (p->next != NULL) { - p = p->next; - } - p->next = node; - } -} -void readPoly(ListNode **head) { - int n; - scanf("%d", &n); - for (int i = 0; i < n; i++) { - int coef, exp; - scanf("%d%d", &coef, &exp); - PolyItem *item = createPolyItem(coef, exp); - insertPolyItem(head, item); - } -} -void printPoly(ListNode *head) { - if (head == NULL) { - printf("0\n"); - } else { - while (head != NULL) { - printf("%d %d ", head->item->coef, head->item->exp); - head = head->next; - } - printf("\n"); - } -} -void freePoly(ListNode *head) { - while (head != NULL) { - ListNode *p = head; - head = head->next; - free(p->item); - free(p); - } -} -ListNode *multiplyPoly(ListNode *poly1, ListNode *poly2) { - ListNode *result = NULL; - while (poly1 != NULL) { - ListNode *p = poly2; - while (p != NULL) { - int coef = poly1->item->coef * p->item->coef; - int exp = poly1->item->exp + p->item->exp; - PolyItem *item = createPolyItem(coef, exp); - insertPolyItem(&result, item); - p = p->next; - } - poly1 = poly1->next; - } - return result; -} - -int main() { - ListNode *poly1 = NULL; - ListNode *poly2 = NULL; - readPoly(&poly1); - readPoly(&poly2); - ListNode *result = multiplyPoly(poly1, poly2); - printPoly(result); - freePoly(poly1); - freePoly(poly2); - freePoly(result); - return 0; -} \ No newline at end of file diff --git "a/2224020117/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2224020117/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index 4916e60934f027e1fc9531b4e4798fc44329e1da..0000000000000000000000000000000000000000 --- "a/2224020117/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,160 +0,0 @@ -#include -#include -typedef char ElemType; -typedef struct LNode -{ - ElemType data; - struct LNode *next; -}LinkNode; -void InitList(LinkNode *&L) -{ - L=(LinkNode *)malloc(sizeof(LinkNode)); - L->next=NULL; -} -bool ListInsert(LinkNode *&L,int i,ElemType e) -{ - int j=0; - LinkNode *p=L,*s; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - s=(LinkNode *)malloc(sizeof(LinkNode)); - s->data=e; - s->next=p->next; - p->next=s; - return true; - } -} -void DispList(LinkNode *L) -{ - LinkNode *p=L->next; - while(p!=NULL) - { - printf("%c",p->data); - p=p->next; - } - printf("\n"); -} -int ListLength(LinkNode *L) -{ - int n=0; - LinkNode *p=L; - while (p->next!=NULL) - { - n++; - p=p->next; - } - return (n); -} -bool ListEmpty(LinkNode *L) -{ - return (L->next==NULL); -} -bool GetElem(LinkNode *L,int i,ElemType &e) -{ - int j=0; - LinkNode *p=L; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - e=p->data; - return true; - } -} -int LocateElem(LinkNode *L,ElemType e) -{ - int i=1; - LinkNode *p=L->next; - while(p!=NULL&&p->data!=e) - { - p=p->next; - i++; - } - if(p==NULL) - return (0); - else - return (i); -} -bool ListDelete(LinkNode *&L,int i,ElemType &e) -{ - int j=0; - LinkNode *p=L,*q; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - q=p->next; - if(q==NULL) - return false; - e=q->data; - p->next=q->next; - free(q); - return true; - } -} -void DestroyList(LinkNode *&L) -{ - LinkNode *pre=L,*p=L->next; - while(p!=NULL) - { - free(pre); - pre=p; - p=pre->next; - } - free(pre); -} - -int main() -{ - LinkNode *h; - ElemType e; - printf("单链表的基本运算如下:\n"); - printf("(1)初始化单链表h.\n"); - InitList(h); - printf("(2)依次采用尾插法插入a,b,c,d,e元素.\n"); - ListInsert(h,1,'a'); - ListInsert(h,2,'b'); - ListInsert(h,3,'c'); - ListInsert(h,4,'d'); - ListInsert(h,5,'e'); - printf("(3)输出单链表h:"); - DispList(h); - printf("(4)输出单链表h的长度:%d\n",ListLength(h)); - printf("(5)判断单链表h是否为空:%s\n",(ListEmpty(h)?"空":"非空")); - GetElem(h,3,e); - printf("(6)输出单链表h的第3个元素:%c\n",e); - printf("(7)输出元素a的位置:%d\n",LocateElem(h,'a')); - printf("(8)在第4个元素位置上插入f元素.\n"); - ListInsert(h,4,'f'); - printf("(9)输出单链表h:"); - DispList(h); - printf("(10)删除单链表h的第3个元素.\n"); - ListDelete(h,3,e); - printf("(11)输出单链表h:"); - DispList(h); - printf("(12)释放单链表h\n"); - DestroyList(h); -} diff --git "a/2224020117/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" "b/2224020117/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" deleted file mode 100644 index 5e849385c4ed0ef9974fcd4346aff35df38ca074..0000000000000000000000000000000000000000 --- "a/2224020117/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" +++ /dev/null @@ -1,19 +0,0 @@ -ListNode* partition(ListNode* head, int x) { - ListNode* small = new ListNode(0); - ListNode* large = new ListNode(0); - ListNode* p1 = small; - ListNode* p2 = large; - while (head) { - if (head->val < x) { - p1->next = head; - p1 = p1->next; - } else { - p2->next = head; - p2 = p2->next; - } - head = head->next; - } - p2->next = nullptr; - p1->next = large->next; - return small->next; -} \ No newline at end of file diff --git "a/2224020117/\346\211\200\346\234\211\345\245\207\346\225\260\346\225\260\347\273\204\347\232\204\344\271\213\345\222\214.cpp" "b/2224020117/\346\211\200\346\234\211\345\245\207\346\225\260\346\225\260\347\273\204\347\232\204\344\271\213\345\222\214.cpp" deleted file mode 100644 index 7f7070c1acfa213a1d1e13c8167393c12f780ace..0000000000000000000000000000000000000000 --- "a/2224020117/\346\211\200\346\234\211\345\245\207\346\225\260\346\225\260\347\273\204\347\232\204\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,39 +0,0 @@ -#include - -int sumOfSubarrays(int arr[], int size) { - int sum = 0; - - for (int i = 0; i < size; i++) { - for (int len = 1; len <= size; len += 2) { - if (i + len > size) { - break; - } - - for (int j = i; j < i + len; j++) { - sum += arr[j]; - } - } - } - - return sum; -} - -int main() { - int size; - - printf("请输入数组的大小:"); - scanf("%d", &size); - - int arr[size]; - - printf("请输入数组元素:"); - for (int i = 0; i < size; i++) { - scanf("%d", &arr[i]); - } - - int sum = sumOfSubarrays(arr, size); - - printf("所有奇数长度子数组的和为:%d\n", sum); - - return 0; -} \ No newline at end of file diff --git "a/2224020117/\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" "b/2224020117/\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" deleted file mode 100644 index c1e6862711291cb597a516cff3feaf333e1ed3cf..0000000000000000000000000000000000000000 --- "a/2224020117/\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" +++ /dev/null @@ -1,56 +0,0 @@ -//文件名:exp1-1.cpp - -//求1+2+...+n -#include -#include // clock_t, clock, CLOCKS_PER_SEC -#include - -//方法1:老实累加 -long add1(long n) -{ - long i, sum = 0; - for (i = 1; i <= n; i++) - sum += i; - return(sum); -} - - -void AddTime1(long n) /* 采用方法1的耗时统计 */ -{ - clock_t t = clock(); - long sum = add1(n); - t = clock() - t; - printf("方法1:\n"); - printf(" 结果:1~%d之和:%ld\n", n, sum); - printf(" 用时:%lf秒\n", ((float)t) / CLOCKS_PER_SEC); -} - - -//方法2:用公式 -long add2(long n) /* 方法2:求1+2+...+n */ -{ - return(n * (n + 1) / 2); -} - - -void AddTime2(long n) /* 采用方法2的耗时统计 */ -{ - clock_t t = clock(); - long sum = add2(n); - t = clock() - t; - printf("方法2:\n"); - printf(" 结果:1~%d之和:%ld\n", n, sum); - printf(" 用时:%lf秒\n", ((float)t) / CLOCKS_PER_SEC); -} - -int main() -{ - int n; - printf("n(大于1000000):"); - scanf("%d", &n); - if (n < 1000000) - return(0); - AddTime1(n); - AddTime2(n); - return(1); -} diff --git "a/2224020117/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2224020117/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index e12ffe58a4ee56cd428356b97dbbb603738e10b6..0000000000000000000000000000000000000000 --- "a/2224020117/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -int main() -{ - int a, b, n, i, k = 0, f; - scanf("%d %d", &a, &b); - for (n = a; n <= b; n++) -{ - f = 1; - for (i = 2; i <= sqrt(n); i++) - if (n % i == 0) { - f = 0; - break; - } - if (f == 1) - k++; - if (n == b) - printf("%d", k); - -} - -} \ No newline at end of file diff --git "a/2224020117/\347\254\254\345\205\253\347\253\240\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250" "b/2224020117/\347\254\254\345\205\253\347\253\240\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250" deleted file mode 100644 index c963775db31d9f89227eae70f5206449f9dd114e..0000000000000000000000000000000000000000 --- "a/2224020117/\347\254\254\345\205\253\347\253\240\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250" +++ /dev/null @@ -1,154 +0,0 @@ -#include -#include -#define max 100 -#define INF 32767 -#define InfoType char -typedef struct -{ - int no; - InfoType info; -}VertexType; - -typedef struct -{ - int edges[max][max]; - int n,e; - VertexType vexs[max]; -}MatGraph; - - -typedef struct ANode -{ - int adjvex; - struct ANode *nextarc; - int weight; -}ArcNode; -typedef struct Vnode -{ - InfoType info; - ArcNode *firstarc; -}VNode; -typedef struct -{ - VNode adjlist[max]; - int n,e; -}AdjGraph; - -void Creatadj(AdjGraph *&G,int A[max][max],int n,int e) //图 -{ - int i,j; - ArcNode *p; - G=(AdjGraph *)malloc(sizeof(AdjGraph)); - for(i=0;iadjlist[i].firstarc=NULL; - } - for(i=0;i=0;j--) - { - if(A[i][j]!=0&&A[i][j]!=INF) - { - p=(ArcNode *)malloc(sizeof(ArcNode)); - p->adjvex=j; - p->weight=A[i][j]; - p->nextarc=G->adjlist[i].firstarc; - G->adjlist[i].firstarc=p; - } - } - - } - G->n=n; - G->e=e; - -} - -void Creatmat(MatGraph &g,int A[max][max],int n,int e) //矩阵 -{ - int i,j; - g.n=n; - g.e=e; - for(i=0;in;i++) - { - p=G->adjlist[i].firstarc; - printf("顶点%d ",i); - while(p!=NULL) - { - printf("%d[%d]->",p->adjvex,p->weight); - p=p->nextarc; - - } - printf("^\n"); - - } -} - - - -void DedtoryAdj(AdjGraph *&G) //销毁 -{ - int i; - ArcNode *pre,*p; - for(i=0;in;i++) - { - pre=G->adjlist[i].firstarc; - while(pre!=NULL) - { - p=pre; - pre=pre->nextarc; - free(p); - } - } - free(G);G=NULL; -} -int main() -{ - AdjGraph *G; - MatGraph g; - int n=6; - int e=10; - int A[max][max]={{0,5,INF,7,INF,INF},{INF,0,4,INF,INF,INF},{8,INF,0,INF,9,INF},{INF,INF,5,0,INF,6},{INF,INF,INF,5,0,INF},{3,INF,INF,INF,1,0}}; - Creatmat(g,A,n,e); - printf("邻接矩阵为:\n\n"); - Dispmat(g); - Creatadj(G,A,n,e); - printf("邻接表为:\n\n"); - DispAdj(G); - printf("销毁邻接表为:\n\n"); - DedtoryAdj(G); - if(G==NULL) - printf("成功!\n\n"); - system("pause"); - return 0; -} \ No newline at end of file diff --git "a/2224020118/chapter10/\346\234\200\345\260\217k\344\270\252\346\225\260.cpp" "b/2224020118/chapter10/\346\234\200\345\260\217k\344\270\252\346\225\260.cpp" deleted file mode 100644 index 9745ec81dd8cfd633cf46990921e8fd0d45157fb..0000000000000000000000000000000000000000 --- "a/2224020118/chapter10/\346\234\200\345\260\217k\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include -#include - -std::vector GetLeastNumbers(const std::vector& arr, int k) { - std::vector result; - if (k <= 0 || arr.empty() || k > arr.size()) { - return result; - } - - std::priority_queue, std::less> maxHeap; - - for (int i = 0; i < k; ++i) { - maxHeap.push(arr[i]); - } - - for (int i = k; i < arr.size(); ++i) { - if (arr[i] < maxHeap.top()) { - maxHeap.pop(); - maxHeap.push(arr[i]); - } - } - - while (!maxHeap.empty()) { - result.push_back(maxHeap.top()); - maxHeap.pop(); - } - - return result; -} - -int main() { - std::vector arr = {4, 5, 1, 6, 2, 7, 3, 8}; - int k = 4; - std::vector leastNumbers = GetLeastNumbers(arr, k); - - std::cout << "The least " << k << " numbers are: "; - for (auto num : leastNumbers) { - std::cout << num << " "; - } - - return 0; -} diff --git "a/2224020118/chapter10/\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204k\344\270\252\346\225\260.cpp" "b/2224020118/chapter10/\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204k\344\270\252\346\225\260.cpp" deleted file mode 100644 index eca180c049c376a3075f379e284706607ae1403f..0000000000000000000000000000000000000000 --- "a/2224020118/chapter10/\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204k\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -#include -#include - -std::vector> kClosest(std::vector>& points, int k) { - std::vector> result; - - std::priority_queue>> maxHeap; - - for (const auto& point : points) { - int distance = pow(point[0], 2) + pow(point[1], 2); - if (maxHeap.size() < k) { - maxHeap.push({distance, point}); - } else if (distance < maxHeap.top().first) { - maxHeap.pop(); - maxHeap.push({distance, point}); - } - } - - while (!maxHeap.empty()) { - result.push_back(maxHeap.top().second); - maxHeap.pop(); - } - - return result; -} - -int main() { - std::vector> points = {{1, 3}, {-2, 2}, {5, -1}, {0, 2}, {1, -3}}; - int k = 3; - std::vector> closestPoints = kClosest(points, k); - - std::cout << "The closest " << k << " points to the origin are:\n"; - for (const auto& point : closestPoints) { - std::cout << "(" << point[0] << ", " << point[1] << ")\n"; - } - - return 0; -} \ No newline at end of file diff --git "a/2224020118/chapter10/\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.cpp" "b/2224020118/chapter10/\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.cpp" deleted file mode 100644 index bb1720cd137d4ccd3eda008b19cc12348fc2a78f..0000000000000000000000000000000000000000 --- "a/2224020118/chapter10/\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.cpp" +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include -#include - -std::string sortString(std::string str) { - std::vector count(26, 0); - for (char ch : str) { - count[ch - 'a']++; - } - - std::string result; - while (result.length() < str.length()) { - for (int i = 0; i < 26; ++i) { - if (count[i] > 0) { - result += 'a' + i; - count[i]--; - } - } - - for (int i = 25; i >= 0; --i) { - if (count[i] > 0) { - result += 'a' + i; - count[i]--; - } - } - } - - return result; -} - -int main() { - std::string str = "leetcode"; - std::cout << "Original string: " << str << std::endl; - - std::string sortedStr = sortString(str); - std::cout << "Sorted string: " << sortedStr << std::endl; - - return 0; -} \ No newline at end of file diff --git "a/2224020119/chapter2/\345\210\240\351\231\244\351\207\215\345\244\215\345\205\203\347\264\240.cpp" "b/2224020119/chapter2/\345\210\240\351\231\244\351\207\215\345\244\215\345\205\203\347\264\240.cpp" deleted file mode 100644 index efae4a8da99ee1b1ba7fa475bb24d89f345cc7fd..0000000000000000000000000000000000000000 --- "a/2224020119/chapter2/\345\210\240\351\231\244\351\207\215\345\244\215\345\205\203\347\264\240.cpp" +++ /dev/null @@ -1,28 +0,0 @@ -#include -int main(void) -{ - int i,n,a[100]; - int j, temp; - printf("输入元素的个数:"); - scanf("%d",&n); - for(i=0;i nums2[i2]) - tmp[i++] = nums2[i2++]; - - else - tmp[i++] = nums1[i1++]; - } - while (i2 < n) - { - tmp[i++] = nums2[i2++]; - } - - while (i1 < m) - { - tmp[i++] = nums1[i1++]; - } - memcpy(nums1,tmp,sizeof(int)*(m+n)); - free(tmp); - tmp=NULL; -} diff --git "a/2224020119/chapter3/\346\240\210\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2224020119/chapter3/\346\240\210\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" deleted file mode 100644 index fc3a03f8e0b3cd3bfe99167ea4e04724a1a5aa9c..0000000000000000000000000000000000000000 --- "a/2224020119/chapter3/\346\240\210\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,94 +0,0 @@ -#include -#include -#define MaxSize 100 -typedef int ElemType; -typedef struct -{ - ElemType data[MaxSize]; - int top; //栈指针 -} SqStack; //顺序栈类型 -void InitStack(SqStack *&s) //清空,初始化 -{ - s=(SqStack *)malloc(sizeof(SqStack)); - s->top=-1; -} -void DestroyStack(SqStack *&s) //销毁 -{ - free(s); -} - -bool Push(SqStack *&s,ElemType e) //入栈 -{ - if (s->top==MaxSize-1) //栈满的情况,即栈上溢出 - return false; - s->top++; - s->data[s->top]=e; - return true; -} -bool Pop(SqStack *&s,ElemType &e) -{ - if (s->top==-1) //栈为空的情况,即栈下溢出 - return false; - e=s->data[s->top]; - s->top--; - return true; -} -bool GetTop(SqStack *s,ElemType &e) -{ - if (s->top==-1) //栈为空的情况,即栈下溢出 - return false; - e=s->data[s->top]; - return true; -} -int main() -{ - SqStack *s; - InitStack(s); - int L,e,a,n; - printf("请输入栈长和栈的元素值\n"); - scanf("%d",&L); - while(L--) - { - scanf("%d",&e); - Push(s,e); - } - n=20; - while(n--) - { - printf("清空请按1,销毁请按2,进栈请按3,出栈请按4,取栈顶元素请按5,结束请按6\n"); - scanf("%d",&a); - if(a==1) - { - InitStack(s); - printf("清空成功!\n"); - } - if(a==2) - { - DestroyStack(s); - printf("销毁成功!\n"); - } - if(a==3) - { - printf("请输入进栈元素\n"); - scanf("%d",&e); - if(Push(s,e)) - printf("进栈成功!\n"); - } - if(a==4) - { - if(Pop(s,e)) - printf("出栈成功!\n"); - } - if(a==5) - { - if(GetTop(s,e)) - printf("成功!取出元素为:%d\n",e); - } - if(a==6) - { - break; - } - } - -} - diff --git "a/2224020119/chapter3/\347\224\250\346\240\210\350\247\243\345\206\263\347\232\207\345\220\216\351\227\256\351\242\230.cpp" "b/2224020119/chapter3/\347\224\250\346\240\210\350\247\243\345\206\263\347\232\207\345\220\216\351\227\256\351\242\230.cpp" deleted file mode 100644 index 436acdba7303d45551832936d3d88aa32cc8c6ca..0000000000000000000000000000000000000000 --- "a/2224020119/chapter3/\347\224\250\346\240\210\350\247\243\345\206\263\347\232\207\345\220\216\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,89 +0,0 @@ -#include -#include -#include -#define MaxSize 100 -typedef struct -{ - int data[MaxSize]; - int top; -} StType; -int count=0; -int place(StType st,int i,int j) -{ - int a=0; - int k=1; - if (i==1) - { - a=1; - return a; - } - while (k<=i-1) - { - if ((st.data[k]==j)||(fabs(j-st.data[k])==fabs(i-k))) - { - a=0; - return a; - } - else - k++; - } - a=1; - return a; -} -void queen(int n) -{ - int i,j,k; - int find=0; - StType st; - st.top=0; - st.top++; - st.data[st.top]=1; - while (st.top>0) - { - i=st.top; - if (st.top==n) - { - printf("第%d个解:",++count); - for (k=1; k<=st.top; k++) - printf("(%d,%d) ",k,st.data[k]); - printf("\n"); - } - find=0; - for (j=1; j<=n; j++) - if (place(st,i+1,j)) - { - st.top++; - st.data[st.top]=j; - find=1; - break; - } - if (find==0) - { - while (st.top>0) - { - if (st.data[st.top]==n) - st.top--; - for (j=st.data[st.top]+1; j<=n; j++) - if (place(st,st.top,j)) - { - st.data[st.top]=j; - break; - } - if (j>n) - st.top--; - else - break; - } - } - } -} -int main() -{ - int n; - printf(" 皇后问题(n<20) n="); - scanf("%d",&n); - printf(" %d皇后问题求解如下:\n",n); - queen(n); - printf("\n"); - return 0; -} \ No newline at end of file diff --git "a/2224020119/chapter3/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2224020119/chapter3/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" deleted file mode 100644 index 66f016f00aabc8c3bc30c9fc2b2430e92534243a..0000000000000000000000000000000000000000 --- "a/2224020119/chapter3/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct -{ - ElemType data[MaxSize]; - int front,rear; //队头和队尾指针 -} SqQueue; -void InitQueue(SqQueue *&q) -{ q=(SqQueue *)malloc (sizeof(SqQueue)); - q->front=q->rear=-1; -} -void DestroyQueue(SqQueue *&q) //销毁队列 -{ - free(q); -} -bool QueueEmpty(SqQueue *q) //判断队列是否为空 -{ - return(q->front==q->rear); -} -bool enQueue(SqQueue *&q,ElemType e) //进队 -{ if (q->rear==MaxSize-1) //队满上溢出 - return false; //返回假 - q->rear++; //队尾增1 - q->data[q->rear]=e; //rear位置插入元素e - return true; //返回真 -} -bool deQueue(SqQueue *&q,ElemType &e) //出队 -{ if (q->front==q->rear) //队空下溢出 - return false; - q->front++; - e=q->data[q->front]; - return true; -} -int main() -{ - SqQueue *q; //创建队列q - ElemType e; - InitQueue(q); //初始化队 - enQueue(q,'a'); - enQueue(q,'b'); - enQueue(q,'c'); //依次进队a,b,c - deQueue(q,e); - printf("%c\n",e); //出队元素a - deQueue(q,e); - printf("%c\n",e); //出队元素b - deQueue(q,e); - printf("%c\n",e); //出队元素c - DestroyQueue(q); //销毁队 - return 0; -} diff --git "a/2224020119/chapter4/\345\210\251\347\224\250KMP\347\256\227\346\263\225\346\261\202\345\255\220\344\270\262\345\234\250\344\270\273\344\270\262\344\270\255\344\270\215\345\207\272\347\216\260\351\207\215\345\217\240\347\232\204\346\254\241\346\225\260.cpp" "b/2224020119/chapter4/\345\210\251\347\224\250KMP\347\256\227\346\263\225\346\261\202\345\255\220\344\270\262\345\234\250\344\270\273\344\270\262\344\270\255\344\270\215\345\207\272\347\216\260\351\207\215\345\217\240\347\232\204\346\254\241\346\225\260.cpp" deleted file mode 100644 index b0a87e525f82b3a60f41cf41cde02ec3dca65e3d..0000000000000000000000000000000000000000 --- "a/2224020119/chapter4/\345\210\251\347\224\250KMP\347\256\227\346\263\225\346\261\202\345\255\220\344\270\262\345\234\250\344\270\273\344\270\262\344\270\255\344\270\215\345\207\272\347\216\260\351\207\215\345\217\240\347\232\204\346\254\241\346\225\260.cpp" +++ /dev/null @@ -1,24 +0,0 @@ -int kmp(char src[],char ptn[]) -{ - int len_src = strlen(src); - int len_ptn = strlen(ptn); - int i = 0,n = 0,k = 0; - while(i= T.length) - return (i+1); - } - return -1; -} -int KMP(Str S, Str T, int* N) //KMP算法 -{ - int i, j; - i = j = 0; - while (i < S.length && j < T.length) { - if (j == -1 || S.data[i] == T.data[j]) { - i++; - j++; - } - else - j = N[j]; - } - if (j >= T.length) - return (i+1-T.length); - else - return -1; -} - diff --git "a/2224020119/chapter4/\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" "b/2224020119/chapter4/\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" deleted file mode 100644 index 38a1bf0b386a8bb9634a4de6ecf1bf78424374c2..0000000000000000000000000000000000000000 --- "a/2224020119/chapter4/\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.cpp" +++ /dev/null @@ -1,69 +0,0 @@ -#include -#define MaxSize 100 -typedef struct { - char data[MaxSize]; - int length; -} SqString; - -void init(SqString &s) -{ - s.length=0; -} - -int insert(SqString &s,char a) -{ - if(s.length>=MaxSize) - return 0; - s.data[s.length++]=a; -} - -void print(SqString s) -{ - for(int i=0;isub.length) - { - sub.length=length; - loc=i; - } - j+=length; - } - else j++; - } - } - for(i=loc,j=0;i -#include -#define MAXL 100 -typedef int KeyType; -typedef char InfoType; -typedef struct -{ - KeyType key; - InfoType data; - } RecType; - void CreateList(RecType R[],KeyType keys[],int n) - { - for(int i=0;ik) - high=mid-1; - else - low=mid+1; - } - return 0; - } - int main() - { - RecType R[MAXL]; - KeyType k=9; - int a[]={1,2,3,4,5,6,7,8,9,10},i,n=10; - CreateList(R,a,n); - printf("关键字序列:"); - DispList(R,n); - printf("查找%d的比较过程如下:\n",k); - if((i=BinSearch(R,n,k))!=0) - printf("元素%d的位置是%d\n",k,i); - else - printf("元素%d不在表中\n",k); - return 1; - } diff --git "a/2224020123/chapter5/\346\201\242\345\244\215ip\345\234\260\345\235\200.cpp" "b/2224020123/chapter5/\346\201\242\345\244\215ip\345\234\260\345\235\200.cpp" deleted file mode 100644 index 356be7820862e6312c8b3e5922dd71740cc5278c..0000000000000000000000000000000000000000 --- "a/2224020123/chapter5/\346\201\242\345\244\215ip\345\234\260\345\235\200.cpp" +++ /dev/null @@ -1,49 +0,0 @@ -#include -#define MaxSize 100 -typedef struct{ - char data[MaxSize]; - int length; -}IP; -void addch(IP &ip,char ch) -{ - ip.data[ip.length]=ch; - ip.length++; -} -IP addot(IP ip) -{ - addch(ip,'.'); - return ip; -} -void solveip(char s[],int n,int start, int step,IP ip) -{ - if(start<=n) - { - if(start==n && step==4) - { - for(int i=0;i -#include -#define N 20; -using namespace std; -int q[20]; //存放个皇后所在的列号 -int count_1 = 0; //累计解个数 - -void dispasolution(int n) {//输出n皇后问题的一个解 - cout << "第" << ++count_1 << "个解:"; - for (int i = 1; i <= n; ++i) { - cout << "(" << i << "," << q[i] << ") "; - } - cout << endl; -} - -bool place(int i, int j) {//测试(i,j)位置能否放置皇后(第i行) - if (i == 1) return true; - int k = 1; - while (k < i) { - if (q[k] == j || (abs(i - k) == abs(j - q[k]))) {//判断当前位置是否可以放置皇后 - return false; - } - k++; - } - return true; -} - -void queen(int i, int n) {//放置第i行的皇后 - if (i > n) dispasolution(n); //递归出口,输出n皇后 - else { - //注意:当找到一个解后,遇到递归出口,但j会继续向后遍历,寻找其他解,相当于是递归进去直至最后一行,在返回过程中,发现没有其他解会继续返回,有其他解又会继续递归,直到返回至第一行有其他皇后可以放置在继续递归 - for (int j = 1; j <= n; ++j) {//每次放置皇后从左到右依次遍历并判断 - if (place(i, j)) { //(i,j)可以放置 - q[i] = j; //记录皇后位置 - queen(i + 1, n); //递归放置下一行的皇后 - } - } - } -} - -int main() { - int n; - cout << "皇后问题(n<20)n="; - cin >> n; - if (n >= 20) cout << "n值太大,不能求解" << endl; - else cout << n << "皇后问题求解如下:" << endl; - queen(1, n); //放置1~n行的皇后 - return 0; -} \ No newline at end of file diff --git "a/2224020123/chapter5/\351\207\207\347\224\250\351\200\222\345\275\222\345\222\214\351\235\236\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243Hanoi\351\227\256\351\242\230.cpp" "b/2224020123/chapter5/\351\207\207\347\224\250\351\200\222\345\275\222\345\222\214\351\235\236\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243Hanoi\351\227\256\351\242\230.cpp" deleted file mode 100644 index e6db9ec74399468c800d00d888d659320e7d7dac..0000000000000000000000000000000000000000 --- "a/2224020123/chapter5/\351\207\207\347\224\250\351\200\222\345\275\222\345\222\214\351\235\236\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243Hanoi\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,30 +0,0 @@ -#include - -void Hanoi(int num, char Source, char Station, char Target) -{ //Source:源座 Station:中转座 Target:目标座 - - //如果只有一个盘子 - if (1==num) - { - printf("# %d : from %c to %c \n", num,Source,Target); - //将num 号盘子,从Source 移动到 Target上 - } - else //递归调用 - { - Hanoi(num - 1, Source, Target, Station); //第一步 - printf("# %d : from %c to %c \n", num, Source, Target); //第二步 - Hanoi(num - 1, Station, Source, Target); //第三步 - } -} - -int main() -{ - char A = 'A'; - char B = 'B'; - char C = 'C'; - int n=3; //移动盘子的个数,这里设置默认为3个盘子 - - Hanoi(n, 'A', 'B', 'C'); - - return 0; -} diff --git "a/2224020123/chapter7/\345\256\236\347\216\260\344\272\214\345\217\211\346\240\221\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2224020123/chapter7/\345\256\236\347\216\260\344\272\214\345\217\211\346\240\221\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" deleted file mode 100644 index 5f7e9e480f82e7e2063f33afbbeef462b22885ae..0000000000000000000000000000000000000000 --- "a/2224020123/chapter7/\345\256\236\347\216\260\344\272\214\345\217\211\346\240\221\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,125 +0,0 @@ -#include -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct node -{ - ElemType data; - struct node *lchild; - struct node *rchild; - -}BTNode; -void CreateBTree(BTNode *&b,char *str) -{ - BTNode * St[MaxSize],*p; - int top=-1,k,j=0;char ch; - b=NULL; - ch=str[j]; - while(ch!='\0') - { - switch(ch) - { - case'(':top++;St[top]=p;k=1;break; - case')':top--;break; - case',':k=2;break; - - default:p=(BTNode *)malloc(sizeof(BTNode)); - p->data=ch;p->lchild=p->rchild=NULL; - if(b==NULL) - b=p; - else - { - switch(k) - { - case 1:St[top]->lchild=p;break; - case 2:St[top]->rchild=p;break; - } - } - } - j++;ch=str[j]; - } -} -void DestroyBTree(BTNode *&b) -{ - if(b!=NULL) - { - DestroyBTree(b->lchild); - DestroyBTree(b->rchild); - free(b); - } -} -BTNode *FindNode(BTNode *b,ElemType x) -{ - BTNode *p; - if(b==NULL) - return NULL; - else if(b->data==x) - return b; - else - { - p=FindNode(b->lchild,x); - if(p!=NULL) - return p; - else - return FindNode(b->rchild,x); - } -} -BTNode *LchildNode(BTNode *p) -{ - return p->lchild; - -} -BTNode *RchildNode(BTNode *p) -{ - return p->rchild; -} -int BTHeight(BTNode *b) -{ - int lchildh,rchildh; - if(b==NULL)return(0); - else - { - lchildh=BTHeight(b->lchild); - rchildh=BTHeight(b->rchild); - return(lchildh>rchildh)?(lchildh+1):(rchildh+1); - } -} -void DispBTree(BTNode *b) -{ - if(b!=NULL) - { - printf("%c",b->data); - if(b->lchild!=NULL||b->rchild!=NULL) - { - printf("("); - DispBTree(b->lchild); - if(b->rchild!=NULL)printf(","); - DispBTree(b->rchild); - printf(")"); - } - } -} -int main() -{ - BTNode *b,*p,*lp,*rp;; - printf("二叉树的基本运算如下:\n"); - printf(" (1)创建二叉树\n"); - CreateBTree(b,"A(B(D,E(H(J,k(L,M(,N))))),C(F,G(,I)))"); - printf(" (2)输出二叉树:");DispBTree(b);printf("\n"); - printf(" (3)H结点:"); - p=FindNode(b,'H'); - if(p!=NULL) - { lp= LchildNode(p); - if(lp!=NULL)printf("左孩子为%c",lp->data); - else printf("无左孩子"); - rp=RchildNode(p); - if(rp!=NULL) printf("右孩子为%c",rp->data); - else printf("无右孩子"); - - } - printf("\n"); - printf(" (4)二叉树b的高度:%d\n",BTHeight(b)); - printf(" (5)释放二叉树b\n"); - DestroyBTree(b); - return 1; -} diff --git "a/2224020123/chapter7/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204.cpp" "b/2224020123/chapter7/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204.cpp" deleted file mode 100644 index 8a25851e9bd6f9e5a935489984df215f47d92b5f..0000000000000000000000000000000000000000 --- "a/2224020123/chapter7/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204.cpp" +++ /dev/null @@ -1,240 +0,0 @@ -#include -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct node -{ - ElemType data; - struct node *lchild; - struct node *rchild; -}BTNode; -void CreateBTree(BTNode *&b,char *str) -{ - BTNode *St[MaxSize],*p; - int top=-1,k,j=0;char ch; - b=NULL; - ch=str[j]; - while(ch!='\0') - { - switch(ch) - { - case '(':top++;St[top]=p;k=1;break; - case ')':top--;break; - case ',':k=2;break; - default:p=(BTNode *)malloc(sizeof(BTNode)); - p->data=ch;p->lchild=p->rchild=NULL; - if(b==NULL) - b=p; - else - { - switch(k) - { - case 1:St[top]->lchild=p;break; - case 2:St[top]->rchild=p;break; - } - } - } - j++;ch=str[j]; - } -} -void DestroyBTree(BTNode *&b) -{ - if(b!=NULL) - { - DestroyBTree(b->lchild); - DestroyBTree(b->rchild); - free(b); - } -} -BTNode *FindNode(BTNode *b,ElemType x) -{ - BTNode *p; - if(b==NULL) - return NULL; - else if(b->data==x) - return b; - else - { - p=FindNode(b->lchild,x); - if(p!=NULL) - return p; - else - return FindNode(b->rchild,x); - } -} -BTNode *LchildNode(BTNode *p) -{ - return p->lchild; -} -BTNode *RchildNode(BTNode *p) -{ - return p->rchild; -} -int BTHeight(BTNode *b) -{ - int lchildh,rchildh; - if(b==NULL)return(0); - else - { - lchildh=BTHeight(b->lchild); - rchildh=BTHeight(b->rchild); - return (lchildh>rchildh)?(lchildh+1):(rchildh+1); - } -} -void DispBTree(BTNode *b) -{ - if(b!=NULL) - { - printf("%c",b->data); - if(b->lchild!=NULL||b->rchild!=NULL) - { - printf("("); - DispBTree(b->lchild); - if(b->rchild!=NULL)printf(","); - DispBTree(b->rchild); - printf(")"); - } - } -} - - - -void AllPath1(BTNode *b,ElemType path[],int pathlen) -{ - if(b!=NULL) - { - if(b->lchild==NULL&&b->rchild==NULL) - { - printf("%c到根结点逆路径:%c->",b->data,b->data); - for(int i=pathlen-1;i>0;i--) - printf("%c->",path[i]); - printf("%c\n",path[0]); - } - else - { - path[pathlen]=b->data; - pathlen++; - AllPath1(b->lchild,path,pathlen); - AllPath1(b->rchild,path,pathlen); - } - } -} -void LongPath1(BTNode *b,ElemType path[],int pathlen,ElemType longpath[],int &longpathlen) -{ - if(b==NULL) - { - if(pathlen>longpathlen) - { - for(int i=pathlen-1;i>=0;i--) - longpath[i]=path[i]; - longpathlen=pathlen; - } - } - else - { - path[pathlen]=b->data; - pathlen++; - LongPath1(b->lchild,path,pathlen,longpath,longpathlen); - LongPath1(b->rchild,path,pathlen,longpath,longpathlen); - } -} -void AllPath2(BTNode *b) -{ - BTNode *st[MaxSize]; - int top=-1; - BTNode *p,*r; - bool flag; - p=b; - do - { - while(p!=NULL) - { - top++; - st[top]=p; - p=p->lchild; - } - r=NULL; - flag=true; - while(top>=-1&&flag) - { - p=st[top]; - if(p->rchild==r) - { - if(p->lchild==NULL&&p->rchild==NULL) - { - printf("%c到根结点逆路径:",p->data); - for(int i=top;i>0;i--) - printf("%c->",st[i]->data); - printf("%c\n",st[0]->data); - } - top--; - r=p; - } - else - { - p=p->rchild; - flag=false; - } - } - }while(top>-1); -} -void AllPath3(BTNode *b) -{ - struct snode - { - BTNode *node; - int parent; - }Qu[MaxSize]; - int front,rear,p; - front=rear=-1; - rear++; - Qu[rear].node=b; - Qu[rear].parent=-1; - while(frontlchild==NULL&&b->rchild==NULL) - { - printf("%c到根结点逆路径:",b->data); - p=front; - while(Qu[p].parent!=-1) - { - printf("%c->",Qu[p].node->data); - p=Qu[p].parent; - } - printf("%c\n",Qu[p].node->data); - } - if(b->lchild!=NULL) - { - rear++; - Qu[rear].node=b->lchild; - Qu[rear].parent=front; - } - if(b->rchild!=NULL) - { - rear++; - Qu[rear].node=b->rchild; - Qu[rear].parent=front; - } - } -} -int main() -{ - BTNode *b; - ElemType path[MaxSize],longpath[MaxSize]; - int i,longpathlen=0; - CreateBTree(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); - printf("二叉树b:");DispBTree(b);printf("\n"); - printf("先序遍历方法:\n");AllPath1(b,path,0); - LongPath1(b,path,0,longpath,longpathlen); - printf("第一条最长逆路径长度:%d\n",longpathlen); - printf("第一条最长逆路径:"); - for(i=longpathlen-1;i>=0;i--) - printf("%c",longpath[i]); - printf("\n"); - printf("后序非递归遍历方法:\n");AllPath2(b); - printf("层次遍历方法:\n");AllPath3(b); - DestroyBTree(b); - return 1; -} diff --git "a/2224020123/chapter8/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.cpp" "b/2224020123/chapter8/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.cpp" deleted file mode 100644 index 83fc7235bd2d3b735cc45ed50e5f9a921fd81e8d..0000000000000000000000000000000000000000 --- "a/2224020123/chapter8/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.cpp" +++ /dev/null @@ -1,172 +0,0 @@ -#include -#include - -#define INF 32767 //定义∞ -#define MAXV 100 //最大顶点个数 - -typedef char InfoType; -/*-------------------------以下定义邻接矩阵类型---------------------------*/ -typedef struct -{ - int no; //顶点编号 - InfoType info; //顶点信息 -}VertexType; //顶点类型 - -typedef struct -{ - int edges[MAXV][MAXV]; //邻接矩阵数组(用一个二维数组存放顶点间关系(边或弧)的数据) - int n; //顶点数 - int e; //边数 - VertexType vexs[MAXV]; //存放顶点信息(用一个一维数组存放图中所有顶点数据) -}MatGraph; //完整的图邻接矩阵类型 - -//邻接表表示法-将每个顶点的邻接点串成一个单链表 -/*-----------以下定义邻接表类型--------------*/ -typedef struct ArcNode -{ - int adjvex; //该边的邻接点编号 - struct ArcNode *nextarc; //指向下一条边的指针 - int weight; //该边的相关信息,如权值(用整型表示) -}ArcNode; //边结点类型 - -typedef struct VNode -{ - InfoType info; //顶点其他信息 - int cnt; //存放顶点入度,仅用于拓扑排序 - ArcNode *firstarc; //指向第一条边 -}VNode; //邻接表结点类型 - -typedef struct -{ - VNode adjlist[MAXV]; //邻接表头结点数组 - int n; //图中顶点数 - int e; //图中边数 -}AdjGraph; //完整的图邻接表类型 - -/*-------------------------邻接矩阵的基本运算算法---------------------------*/ -/*------------由边数组A、顶点数n和边数e创建图的邻接矩阵g--------------------*/ -void CreateMat(MatGraph &g, int A[MAXV][MAXV], int n, int e) -{ - int i, j; - - g.n = n; - g.e = e; - for(i = 0; i < g.n; i++) - for(j = 0; j < g.n; j++) - g.edges[i][j] = A[i][j]; -} - -/*------------输出邻接矩阵g--------------------*/ -void DispMat(MatGraph g) -{ - int i, j; - - for(i = 0; i < g.n; i++) - { - for(j = 0; j < g.n; j++) - { - if(g.edges[i][j] != INF) - printf("%4d", g.edges[i][j]); - else - printf("%4s", "∞"); - } - printf("\n"); - } -} - -/*-------------------------邻接表的基本运算算法---------------------------*/ -/*-------------------由边数组A、顶点数n和边数e创建图的邻接表G--------------------*/ -void CreateAdj(AdjGraph *&G, int A[MAXV][MAXV], int n, int e) -{ - int i, j; - ArcNode *p; - - G = (AdjGraph *)malloc(sizeof(AdjGraph)); - for(i = 0; i < n; i++) //给邻接表中所有头结点的指针域置初值NULL - { - G->adjlist[i].firstarc = NULL; - } - - for(i = 0; i < n; i++) //检查邻接矩阵中的每个元素 - { - for(j = n - 1; j >= 0; j--) - { - if(A[i][j] != 0 && A[i][j] != INF) //存在一条边 - { - p = (ArcNode *)malloc(sizeof(ArcNode)); //创建一个结点p - p->adjvex = j; //邻接点编号 - p->weight = A[i][j]; //边的权重 - p->nextarc = G->adjlist[i].firstarc; //采用头插法插入结点p - G->adjlist[i].firstarc = p; - } - } - } - G->n = n; - G->e = e; -} - -/*-------------------输出邻接表G--------------------*/ -void DispAdj(AdjGraph *G) -{ - ArcNode *p; - - for(int i = 0; i < G->n; i++) - { - p = G->adjlist[i].firstarc; - printf("顶点%d: ", i); - while(p != NULL) - { - printf("%3d[%d]->", p->adjvex, p->weight); //邻接点编号[权重] - p = p->nextarc; - } - printf("∧\n"); - } -} - -/*-------------------销毁图的邻接表G--------------------*/ -void DestroyAdj(AdjGraph *&G) -{ - ArcNode *pre, *p; - - for(int i = 0; i < G->n; i++) - { - pre = G->adjlist[i].firstarc; //pre指向第i个单链表的首结点 - if(pre != NULL) - { - p = pre->nextarc; - while(p != NULL) //释放第i个单链表的所有边结点 - { - free(pre); - pre = p; - p = p->nextarc; - } - free(pre); - } - } - free(G); //释放头结点数组 -} - -int main(void) -{ - MatGraph g; - AdjGraph *G; - int n = 6; //图中的顶点数 - int e = 10; //图中的边数 - int A[MAXV][MAXV] = { - {0, 5, INF, 7, INF, INF}, {INF, 0, 4, INF, INF, INF}, - {8, INF, 0, INF, INF, 9}, {INF, INF, 5, 0, INF, 6}, - {INF, INF, INF, 5, 0, INF}, {3, INF, INF, INF, 1, 0} - }; - - CreateMat(g, A, n, e); - printf("(1)图的邻接矩阵:\n"); - DispMat(g); - - CreateAdj(G, A, n, e); - printf("(2)图的邻接表:\n"); - DispAdj(G); - printf("(3)销毁图的邻接表\n"); - DestroyAdj(G); - - return 0; -} diff --git "a/2224020123/chapter8/\346\261\202\350\247\243\345\273\272\345\205\254\350\267\257\351\227\256\351\242\230.cpp" "b/2224020123/chapter8/\346\261\202\350\247\243\345\273\272\345\205\254\350\267\257\351\227\256\351\242\230.cpp" deleted file mode 100644 index f2181ec1e6093e89d9e9f1f4ec2cbf2465f22765..0000000000000000000000000000000000000000 --- "a/2224020123/chapter8/\346\261\202\350\247\243\345\273\272\345\205\254\350\267\257\351\227\256\351\242\230.cpp" +++ /dev/null @@ -1,134 +0,0 @@ -#include -#include -#define MAXN 1001 //从1开始 -#define INF 100000 -int Map[MAXN][MAXN],Cost[MAXN],N,M; -int parent[MAXN]; //用于并查集存放父亲下标 -typedef struct Edge //边节点 -{ - int V; - int W; - int Cost; -}Edge; -Edge *E; -void CreateGraph(int n, int e) //创建初始化图 -{ - int v,w,i; - for(v=1;v<=n;v++) - { - Cost[v]=0; //存放各顶点到最小生成树的距离(价格) - for(w=1;w<=n;w++) - { - if(v==w) - Map[v][w]=0; //自己到自己不要钱 - else - Map[v][w]=INF; - } - } - E=(Edge*)malloc(sizeof(struct Edge)*e); //开一个边的动态数组 - for(i=0;iMap[MinCost][i]) //如果价格不等于0,且各还没收入顶点到更新后的最小生成树距离更小 - { - Cost[i]=Map[MinCost][i]; //那么更新距离 - } - } - } - } - if(Count=M; Si++ ) ; - for ( D=Sedgewick[Si]; D>0; D=Sedgewick[++Si] ) - for ( P=D; P=D && E[i-D].Cost >Tmp.Cost ; i-=D ) - E[i] = E[i-D] ; - E[i] = Tmp; - } -} -int FindParent(int v) //查找集合 -{ - if(parent[v]==v) return v; - else return FindParent(parent[v]); -} -int Associate(int v,int w) //合并集合 -{ - int root1,root2; - root1=FindParent(v); //找到父亲 - root2=FindParent(w); //找到父亲 - if(root1!=root2) //如果父亲不一样 - { - parent[root2]=root1; //合并两个集合 - return 1; - } - return 0; -} -int Kruskal() -{ - int i,GrossCost=0,Count=0; - Shell_Sort(); //希尔排序边集 - for(i=1;i<=N;i++)parent[i]=i; //每个顶点一开始自己就是自己的父亲 - for(i=0;i -#include - -#define MAXN 10 -#define INF = 1000 - -typedef struct struct_graph{ - char vexs[MAXN]; - int vexnum;//顶点数 - int edgnum;//边数 - int matirx[MAXN][MAXN];//邻接矩阵 -} Graph; - -int pathmatirx[MAXN][MAXN];//记录对应点的最小路径的前驱点,例如p(1,3) = 2 说明顶点1到顶点3的最小路径要经过2 -int shortPath[MAXN][MAXN];//记录顶点间的最小路径值 - -void short_path_floyd(Graph G, int P[MAXN][MAXN], int D[MAXN][MAXN]){ - int v, w, k; - //初始化floyd算法的两个矩阵 - for(v = 0; v < G.vexnum; v++){ - for(w = 0; w < G.vexnum; w++){ - D[v][w] = G.matirx[v][w]; - P[v][w] = w; - } - } - - //这里是弗洛伊德算法的核心部分 - //k为中间点 - for(k = 0; k < G.vexnum; k++){ - //v为起点 - for(v = 0 ; v < G.vexnum; v++){ - //w为终点 - for(w =0; w < G.vexnum; w++){ - if(D[v][w] > (D[v][k] + D[k][w])){ - D[v][w] = D[v][k] + D[k][w];//更新最小路径 - P[v][w] = P[v][k];//更新最小路径中间顶点 - } - } - } - } - - printf("\n初始化的D矩阵\n"); - for(v = 0; v < G.vexnum; v++){ - for(w = 0; w < G.vexnum; w++){ - printf("%d ", D[v][w]); - } - printf("\n"); - } - - printf("\n初始化的P矩阵\n"); - for(v = 0; v < G.vexnum; v++){ - for(w = 0; w < G.vexnum; w++){ - printf("%d", P[v][w]); - } - printf("\n"); - } - - v = 0; - w = 3; - //求 0 到 3的最小路径 - printf("\n%d -> %d 的最小路径为:%d\n", v, w, D[v][w]); - k = P[v][w]; - printf("path: %d", v);//打印起点 - while(k != w){ - printf("-> %d", k);//打印中间点 - k = P[k][w]; - } - printf("-> %d\n", w); -} - -int main(){ - int v, w; - Graph G; - printf("请输入顶点数:\n"); - scanf("%d", &G.vexnum); - printf("请输入初始矩阵值:\n"); - for(v = 0; v < G.vexnum; v++){ - for(w = 0; w < G.vexnum; w++){ - scanf("%d", &G.matirx[v][w]); - } - } - printf("\n输入的矩阵值:\n"); - for(v = 0; v < G.vexnum; v++){ - for(w = 0; w < G.vexnum; w++){ - printf("%d ", G.matirx[v][w]); - } - printf("\n"); - } - short_path_floyd(G, pathmatirx, shortPath); -} \ No newline at end of file diff --git "a/2224020123/chapter9/\347\273\237\350\256\241\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\344\270\255\345\255\227\347\254\246\345\217\212\345\205\266\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" "b/2224020123/chapter9/\347\273\237\350\256\241\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\344\270\255\345\255\227\347\254\246\345\217\212\345\205\266\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" deleted file mode 100644 index bb30b0f52f1fb7133beb23810fe5bdbcb64e4392..0000000000000000000000000000000000000000 --- "a/2224020123/chapter9/\347\273\237\350\256\241\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\344\270\255\345\255\227\347\254\246\345\217\212\345\205\266\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.cpp" +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include -#include -#define MAX_WORD (100) - -typedef struct tnode -{ - char ch; // 字符 - int cnt; // 出现次数 - struct tnode *lchild; // 左指针 - struct tnode *rchild; // 右指针 -}BSTNode; // 结点类型 - -/*-----------------采用递归方式向二叉排序树bst中插入一个字符----------------*/ -static void create_bst(BSTNode *&bt, char ch) // 指针的引用 -{ - if(bt == NULL) // bt为空,则建立一个新结点 - { - bt = (BSTNode *)malloc(sizeof(BSTNode)); - bt->ch = ch; - bt->cnt = 1; - bt->lchild = bt->rchild = NULL; - } - else if(ch == bt->ch) - bt->cnt++; - else if(ch < bt->ch) - create_bst(bt->lchild, ch); - else - create_bst(bt->rchild, ch); -} - -/*-----------------中序遍历二叉排序树bst----------------*/ -static void inorder(BSTNode *bt) -{ - if(bt != NULL) - { - inorder(bt->lchild); // 中序遍历左子树 - printf(" %c(%d)\n", bt->ch, bt->cnt); // 访问根结点 - inorder(bt->rchild); // 中序遍历右子树 - } -} - -/*-----------------销毁二叉排序树bst----------------*/ -static void destroy_bst(BSTNode *bt) -{ - if(bt != NULL) - { - destroy_bst(bt->lchild); - destroy_bst(bt->rchild); - free(bt); - } -} - -int main(int argc, char *argv[]) -{ - BSTNode *bt = NULL; - int i = 0; - char str[MAX_WORD]; - - printf("输入字符串:"); - gets(str); - while(str[i] != '\0') - { - create_bst(bt, str[i]); - i++; - } - printf("字符及出现次数:\n"); - inorder(bt); - printf("\n"); - destroy_bst(bt); - - return 0; -} diff --git "a/2224020123/chapter9/\350\256\276\350\256\241\351\253\230\346\225\210\346\217\222\345\205\245\345\210\240\351\231\244\345\222\214\346\214\211\345\272\217\345\217\267\346\237\245\346\211\276\347\232\204\346\225\260\346\215\256\347\273\223\346\236\204.cpp" "b/2224020123/chapter9/\350\256\276\350\256\241\351\253\230\346\225\210\346\217\222\345\205\245\345\210\240\351\231\244\345\222\214\346\214\211\345\272\217\345\217\267\346\237\245\346\211\276\347\232\204\346\225\260\346\215\256\347\273\223\346\236\204.cpp" deleted file mode 100644 index cb83d7d1e2f663a0da9e0fca7d2beeaa6e0abc4f..0000000000000000000000000000000000000000 --- "a/2224020123/chapter9/\350\256\276\350\256\241\351\253\230\346\225\210\346\217\222\345\205\245\345\210\240\351\231\244\345\222\214\346\214\211\345\272\217\345\217\267\346\237\245\346\211\276\347\232\204\346\225\260\346\215\256\347\273\223\346\236\204.cpp" +++ /dev/null @@ -1,74 +0,0 @@ -#include -#define MaxSize 10 - -using namespace std; - -typedef struct{ - int data[MaxSize]; - int length; -}SqList; - -//遍历操作 -void PrintList(SqList L){ - for(int i = 0; i < L.length; i++) - cout << L.data[i] << " "; - cout << endl; -} - -// 插入操作 -bool ListInsert(SqList &L, int i, int e){ - if(i<1 || i>L.length+1) //判断i的范围是否有效 - return false; - if(L.length == MaxSize) //判断存储空间是否已满 - return false; - for(int j = L.length; j >= i; j--){ //将第i个元素及之后的元素后移 - L.data[j] = L.data[j-1]; - } - L.data[i-1] = e; //在位置i处放入e - L.length++; //长度+1 - return true; -} - -//删除操作 -bool ListDelete(SqList &L, int i, int &e){ - if(i<1 || i>L.length) //判断i的范围是否有效 - return false; - e=L.data[i-1]; //将被删除的元素赋值给e - for(int j = i; j < L.length; j++){ //将第i个元素及之后的元素前移 - L.data[j-1] = L.data[j]; - } - L.length--; //长度-1 - return true; -} - -//查询操作-按位查找 -int GetElem(SqList L ,int i){ - return L.data[i-1]; -} - -//查询操作-按值查找 -int LocateElem(SqList L ,int e){ - for(int i = 0; i < L.length; i++){ - if(L.data[i] == e){ - return i+1; //数组下标为i的元素值=e,返回其位序i+1 - } - } - return 0; -} - -int main() -{ - SqList L; - L.length=0; //静态分配初始化 - ListInsert(L,1,20); //第一个插入20 - ListInsert(L,2,30); //第二个插入30 - ListInsert(L,3,10); //第三个插入10 - ListInsert(L,4,12); //第四个插入12 - PrintList(L); //遍历 - cout< - int main() - { - int a,target,i,j; - printf("数组个数为:\n"); - scanf("%d",&a); - - printf("请输入数组包含的元素:\n"); - int nums[a]; - for(i=0;i - int main() - { - int a,target,i,j; - printf("数组个数为:\n"); - scanf("%d",&a); - - printf("请输入数组包含的元素:\n"); - int nums[a]; - for(i=0;i -void twosum(int nums[],int *sum1,int *sum2,int size) -{ - *sum1=0; - *sum2=0; - for(int i=0;i -#include - -void exchange(int* x, int* y) -{ - int temp = *x; - *x = *y; - *y = temp; -} - -void mergeArr(int *arr, int len, int *arr1, int len1, int *arr2, int len2) -{ - for (int i = 0; i < len; i++) - { - if (i < len1) - { - arr[i] = arr1[i]; - } - else - { - arr[i] = arr2[i - len1]; - } - } -} - -int partition(int *arr, int start, int end) -{ - int key = arr[end]; - int i = start - 1; - for (int j = start; j < end; j++) - { - if (arr[j] <= key) - { - i = i + 1; - exchange(arr + i, arr + j); - } - } - exchange(arr + i + 1, arr + end); - return i + 1; -} -void quickSort(int *arr, int start, int end) -{ - if (start < end) - { - int mid = partition(arr, start, end); - quickSort(arr, start, mid - 1); - quickSort(arr, mid + 1, end); - } -} - -void mergeArrSorted(int *arr, int len, int *arr1, int len1, int *arr2, int len2) -{ - mergeArr(arr, len, arr1, len1, arr2, len2); - printf("merge Array: %d\n", len); - quickSort(arr, 0, len - 1); -} -int main (void) { - int arr1[] = {1, 9, 3, 4, 79, 27}; - int arr2[] = {2, 56, 35, 7, 19}; - int len1 = sizeof(arr1) / sizeof(*arr1); - int len2 = sizeof(arr2) / sizeof(*arr2); - int len = len1 + len2; - printf("length %d length\n", len); - int* arr = (int*)malloc(len * sizeof(int)); - mergeArrSorted(arr, len, arr1, len1, arr2, len2); - - for (int k = 0; k < len; k++) - { - printf("%d ", arr[k]); - } - printf("\n"); -} diff --git "a/2224020128/chapter2/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" "b/2224020128/chapter2/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" deleted file mode 100644 index f66e3975dfd5bf0194a8be670fc24d0c7953660f..0000000000000000000000000000000000000000 --- "a/2224020128/chapter2/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.cpp" +++ /dev/null @@ -1,57 +0,0 @@ -#define _CRT_SECURE_NO_WARNINGS 1 -#include -#include -struct ListNode { - int val; - struct ListNode *next; - }; - -struct ListNode* removeElements(struct ListNode* head, int val) { - struct ListNode* cur = head; - struct ListNode* precur = NULL; - while (cur) { - if (head->val == val) { - head = cur->next; - free(cur); - cur = head; - } - else { - if (cur->val == val) { - precur->next = cur->next; - free(cur); - cur = precur; - } - else { - precur = cur; - cur = cur->next; - } - } - } - return head; -} -void SListprint(struct ListNode* phead) { - struct ListNode* cur = phead; - while (cur) { - printf("%d ", cur->val); - cur = cur->next; - } - printf("\n"); -} -int main() { - struct ListNode* n1 = (struct ListNode*)malloc(sizeof(struct ListNode)); - struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode)); - struct ListNode* n3 = (struct ListNode*)malloc(sizeof(struct ListNode)); - struct ListNode* n4 = (struct ListNode*)malloc(sizeof(struct ListNode)); - n1->val = 1; - n2->val = 2; - n3->val = 3; - n4->val = 4; - n1->next = n2; - n2->next = n3; - n3->next = n4; - n4->next = NULL; - SListprint(n1); - struct ListNode* newphead = removeElements(n1, 1); - SListprint(newphead); - return 0; -} \ No newline at end of file diff --git "a/2224020130/chapter1/\346\211\200\346\234\2111\342\200\224\342\200\224n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214exp1_1.cpp" "b/2224020130/chapter1/\346\211\200\346\234\2111\342\200\224\342\200\224n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214exp1_1.cpp" deleted file mode 100644 index 2ef62a117dc1328f5cdb633d353d9b3df49eb22a..0000000000000000000000000000000000000000 --- "a/2224020130/chapter1/\346\211\200\346\234\2111\342\200\224\342\200\224n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214exp1_1.cpp" +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include -int main() -{ - long add1(int n); - void addtime1(long n); - long add2(int n); - void addtime2(long n); - int n; - scanf("%d",&n); - addtime1(n); - addtime2(n); - return 0; -} - -long add1(int n) -{ - long a; - a=(1+n)*n/2; - return a ; -} - -void addtime1(long n) -{ - clock_t t; - long sum; - t=clock(); - sum=add1(n); - t=clock()-t; - printf("使用公式方法:\n"); - printf("结果是:%ld\n",sum); - printf("用时:%lf\n",((float)t)/CLOCKS_PER_SEC); -} - -long add2(int n) -{ - long i,sum=0; - for(i=1;i<=n;i++) - sum=sum+i; - return sum; -} - -void addtime2(long n) -{ - clock_t t; - long sum; - t=clock(); - sum=add2(n); - t=clock()-t; - printf("使用累加方法:\n"); - printf("结果是:%ld\n",sum); - } \ No newline at end of file diff --git "a/2224020130/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260exp1_3.cpp" "b/2224020130/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260exp1_3.cpp" deleted file mode 100644 index 483a85863b5f1e60852481ea2db58de687c28822..0000000000000000000000000000000000000000 --- "a/2224020130/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260exp1_3.cpp" +++ /dev/null @@ -1,58 +0,0 @@ -#include -#include -#include - -bool prime1(long n) -{ - long i; - for(i=2;i -#include -#include -typedef char ElemType; -typedef struct LNode -{ - ElemType data; - struct LNode *next; -}LinkNode; - -void CreatlistF(LinkNode *&L,ElemType a[],int n) -{ - LinkNode *s; - L=(LinkNode*)malloc(sizeof(LinkNode)); - L->next=NULL; - for(int i=0;idata=a[i]; - s->next=L->next; - L->next=s; - } -} - -void CreatlistR(LinkNode *&L,ElemType a[],int n) -{ - LinkNode *r,*s; - L=(LinkNode *)malloc(sizeof(LinkNode)); - r=L; - for(int i=0;idata=a[i]; - r->next=s; - r=s; - } - r->next=NULL; -} - -void InitList(LinkNode *&L) -{ - L=(LinkNode*)malloc(sizeof(LinkNode)); - L->next=NULL; -} - -void Destroylist(LinkNode *&L) -{ - LinkNode *pre=L,*p=L->next; - while(p!=NULL) - { - free(pre); - pre=p; - p=pre->next; - } - free(pre); -} - -bool ListEmpty(LinkNode *L) -{ - return(L->next==NULL); -} - -int ListLenght(LinkNode *L) -{ - int n=0; - LinkNode *p=L; - while(p->next!=NULL) - { - n++; - p=p->next; - } - return(n); -} - -void Output(LinkNode *L) -{ - LinkNode *p=L->next; - while(p!=NULL) - { - printf("%d",p->data); - p=p->next; - } - printf("\n"); -} - -bool GetElem(LinkNode *L,int i,ElemType &e) -{ - int j=0; - LinkNode *p=L; - if(i<=0)return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - e=p->data; - return true; - } -} - -int LocateElem(LinkNode *L,ElemType e) -{ - int i=1; - LinkNode *p=L->next; - while(p!=NULL && p->data!=e) - { - p=p->next; - i++; - } - if(p==NULL) - return 0; - else - return (i); -} - -bool Listinsert(LinkNode *&L,int i,ElemType e) -{ - int j=0; - LinkNode *p=L,*s; - if(i<=0)return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - s=(LinkNode * )malloc(sizeof(LinkNode)); - s->data=e; - s->next=p->next; - p->next=s; - return true; - } -} - -bool ListDelet(LinkNode *&L,int i,ElemType e) -{ - int j=0; - LinkNode *p=L,*q; - if(j<=i-1&&p!=NULL) - { - j++; - q=p->next; - } - if(p==NULL) - return false; - else - { - q=p->next; - if(q==NULL) - return false; - e=q->data; - p->next=q->next; - free(q); - return true; - } -} - - -int main() -{ - LinkNode *h; - ElemType e; - printf("单链表的基本运算如下:\n"); - printf("(1)初始化链表和\n"); - InitList(h); - printf("(2)依次采用尾插法插入元素a,b,c,d,e\n"); - Listinsert(h,1, 'a'); - Listinsert(h,2, 'b'); - Listinsert(h,3, 'c'); - Listinsert(h,4, 'd'); - Listinsert(h,5, 'e'); - printf("(3)输出单链表h:");Output(h); - printf("(4)单链表h的长度:%d\n"),ListLenght(h); - printf("(5)单链表h为:%s\n",(ListEmpty(h)?"空":"非空")); - GetElem(h,3,e); - printf("(6)单链表h的第3个元素:%c\n",e); - printf("(7)元素a的位置:%d\n",LocateElem(h,'a')); - printf("(8)在第4个元素位置上插入元素f\n"); - Listinsert(h, 4, 'f'); - printf("(9)输出单链表h:");Output(h); - printf("(10)删除h的第3个元素f\n"); - ListDelet(h, 3, e); - printf("(11)输出单链表h:");Output(h); - printf("(12)释放单链表h\n"); - Destroylist(h); - return 0; -} \ No newline at end of file diff --git a/2224020130/chapter3/sqstack.cpp b/2224020130/chapter3/sqstack.cpp deleted file mode 100644 index 31efcb97c0ff5d98d78cee83244b4a6da2bd18ea..0000000000000000000000000000000000000000 --- a/2224020130/chapter3/sqstack.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include -#include -#define Maxsize 100 -typedef char ElemType; -typedef struct -{ -ElemType data[Maxsize]; -int top ; - }sqstack; - -void InitStack(sqstack *&s) -{ - s=(sqstack*)malloc(sizeof(sqstack)); - s->top=-1; -} - - void Destorystack(sqstack *&s ) -{ - free(s); -} - -bool stackEmpty(sqstack *&s) -{ - return(s->top==-1); -} - -bool Push(sqstack *&s,ElemType e) -{ - if(s->top==Maxsize-1) - return false; - s->top++; - s->data[s->top]=e; - return true; -} - -bool Pop(sqstack *&s,ElemType e) -{ - if(s->top==-1) - return false; - e=s->data[s->top]; - s->top--; - return true; -} - -bool GetTop(sqstack *&s,ElemType e) -{ - if(s->top==-1) - return false; - e=s->data[s->top]; - return true; -} - -int main() -{ - ElemType e; - sqstack *s; - printf("顺序栈是s的基本运算如下:\n"); - printf("(1)初始化栈s\n"); - InitStack(s); - printf("(2)进栈的顺序为%s\n",stackEmpty(s)?"空":"非空"); - printf("(3)依次进栈的元素a,b,c,d,e\n"); - Push(s,'a'); - Push(s,'b'); - Push(s,'c'); - Push(s,'d'); - Push(s,'e'); - printf("(4)栈为%s\n",stackEmpty(s)?"空":"非空"); - printf("(5)出栈序列:"); - while(!stackEmpty(s)) - { - Pop(s,e); - printf("%c",e); - } - printf("\n"); - printf("(6)栈为%s\n",stackEmpty(s)?"空":"非空"); - printf("(7)释放栈\n"); - Destorystack(s); - return 0; -} \ No newline at end of file diff --git "a/2224020135/chap1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2224020135/chap1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index fb4d2bb2ac3c6dd4fc9aaa0f9b42651ac20042a9..0000000000000000000000000000000000000000 --- "a/2224020135/chap1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,14 +0,0 @@ -class Solution { -public: - vector twoSum(vector& nums, int target) { - int n = nums.size(); - for (int i = 0; i < n; ++i) { - for (int j = i + 1; j < n; ++j) { - if (nums[i] + nums[j] == target) { - return {i, j}; - } - } - } - return {}; - } -}; \ No newline at end of file diff --git "a/2224020135/chap1/\345\211\215n\351\241\271\344\271\213\345\222\214" "b/2224020135/chap1/\345\211\215n\351\241\271\344\271\213\345\222\214" deleted file mode 100644 index bb95ce9a855300c54665e735e0dc23dee82321d9..0000000000000000000000000000000000000000 --- "a/2224020135/chap1/\345\211\215n\351\241\271\344\271\213\345\222\214" +++ /dev/null @@ -1,7 +0,0 @@ -long add(long n) -{ - long i,sum=0; - for (i=1;i<=n;i++) - sum+=i; - return sum; -} \ No newline at end of file diff --git "a/2224020135/chap1/\345\211\215n\351\241\271\344\271\213\345\222\214.cpp" "b/2224020135/chap1/\345\211\215n\351\241\271\344\271\213\345\222\214.cpp" deleted file mode 100644 index bb95ce9a855300c54665e735e0dc23dee82321d9..0000000000000000000000000000000000000000 --- "a/2224020135/chap1/\345\211\215n\351\241\271\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,7 +0,0 @@ -long add(long n) -{ - long i,sum=0; - for (i=1;i<=n;i++) - sum+=i; - return sum; -} \ No newline at end of file diff --git "a/2224020135/chap1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" "b/2224020135/chap1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" deleted file mode 100644 index 5d51897398ec9da8d36b375f119735b80124b636..0000000000000000000000000000000000000000 --- "a/2224020135/chap1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - int sumOddLengthSubarrays(vector& arr) { - int n = arr.size(); - vector prefix_sum(n + 1, 0); - for (int i = 0; i < n; ++i) { - prefix_sum[i + 1] = prefix_sum[i] + arr[i]; - } - int res = 0; - for (int i = 0; i < n; ++i) { - for (int sz = 1; sz <= n - i; sz += 2) - res += prefix_sum[i + sz] - prefix_sum[i]; - } - } - return res; - } -}; \ No newline at end of file diff --git "a/2224020136/2224020136\347\216\257\345\275\242\351\230\237\345\210\227.cpp" "b/2224020136/2224020136\347\216\257\345\275\242\351\230\237\345\210\227.cpp" deleted file mode 100644 index a54ac16be4ddb5ac9d0ab97bf60689f9fc31df85..0000000000000000000000000000000000000000 --- "a/2224020136/2224020136\347\216\257\345\275\242\351\230\237\345\210\227.cpp" +++ /dev/null @@ -1,85 +0,0 @@ -#include -#include -#define MaxSize 50 -typedef char ElemType; -char w; -typedef struct -{ - ElemType data[MaxSize]; - int front,rear; -}SqQueue; -void InitQueue(SqQueue* &q) -{ - q=(SqQueue*)malloc(sizeof(SqQueue)); - q->front=q->rear=0; -} -bool enQueue(SqQueue* &q,ElemType e) -{ - if((q->rear+1)%MaxSize==q->front) - return false; - q->rear=(q->rear+1)%MaxSize; - q->data[q->rear]=e; - return true; -} - -bool QueueEmpty(SqQueue *q) -{ - return(q->front==q->rear); -} - - -bool deQueue(SqQueue* &q,ElemType&e) -{ - if(q->front==q->rear) - return false; - q->front=(q->front+1)%MaxSize; - e=q->data[q->front]; - return true; -} - -void DestroyQueue(SqQueue* &q) -{ - free(q); -} - -int main() -{ - SqQueue *q; - printf("环形队列的各种基本运算的算法 \n"); - - printf("(1)初始化环形队列 \n"); - InitQueue(q); - - printf("(2)依次进队元素a、b、c \n"); - if(!enQueue(q,'a')) - printf("\t提示:队满,不能进队\n"); - if(!enQueue(q,'b')) - printf("\t提示:队满,不能进队\n"); - if(!enQueue(q,'c')) - printf("\t提示:队满,不能进队\n"); - - printf("(3)判断环形队列q是否非空: %s\n",(QueueEmpty(q)?"空":"非空")); - if(deQueue(q,w)==0) - printf("队空,不能出列\n"); - else - printf("(4)出队一个元素,输出该元素: %c\n",w); - - printf("(5)依次进队元素d、e、f \n"); - if(!enQueue(q,'d')) - printf("\t提示:队满,不能进队\n"); - if(!enQueue(q,'e')) - printf("\t提示:队满,不能进队\n"); - if(!enQueue(q,'f')) - printf("\t提示:队满,不能进队\n"); - - printf("(6)输出出队序列: \n"); - while(!QueueEmpty(q)) - { - deQueue(q,w); - printf("%c",w); - } - printf("\n"); - printf("(7)释放队列 \n"); - DestroyQueue(q); - return 0; -} \ No newline at end of file diff --git "a/2224020138/\344\270\244\346\225\260\344\271\213\345\222\2141.cpp" "b/2224020138/\344\270\244\346\225\260\344\271\213\345\222\2141.cpp" deleted file mode 100644 index 6845a277277b2fa8cbd54ea6857b0f466bbd51c7..0000000000000000000000000000000000000000 --- "a/2224020138/\344\270\244\346\225\260\344\271\213\345\222\2141.cpp" +++ /dev/null @@ -1,9 +0,0 @@ -#include -int main() -{ - int a,b,sum; - scanf("%d%d",&a,&b); - sum=a+b; - printf("%d",sum); - return 0; -} \ No newline at end of file diff --git "a/2224020138/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2224020138/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index 6919f43de52fbec9adcbf8945693d7ac8509a7f6..0000000000000000000000000000000000000000 --- "a/2224020138/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include - -bool prime1(long n) -{ long i; - for (i = 2;i -#define MAXSIZE 60 -/*函数声明*/ -void Hanoi1(int n, char a, char b, char c); -void Hanoi2(int n, char a, char b, char c); -void PrintHanoi(int no, char from, char to); - -typedef struct -{ - char a,b,c; - int flag,num; -}stack; - -int main() -{ - int n; - printf("请输入移动圆盘的个数:\n"); - scanf("%d",&n); - printf("汉诺塔的递归实现:\n"); - Hanoi1(n,'a','b','c'); - printf("汉诺塔的非递归实现:\n"); - Hanoi2(n,'a','b','c'); - return 0; -} - -void PrintHanoi(int no, char from, char to)//输出汉诺塔的圆盘移动过程 -{ - printf("将第%d个圆盘从%c移动到%c\n",no,from,to); -} - -void Hanoi1(int n, char a, char b, char c) -{ - if (n==1) PrintHanoi(n,a,c); - else - { - Hanoi1(n-1,a,c,b); - PrintHanoi(n,a,c); - Hanoi1(n-1,b,a,c); - } -} - -void Hanoi2(int n, char a, char b, char c) -{ - int top=1,a1,b1,c1,m; - stack s[MAXSIZE]; - //初值入栈 - s[top].num=n; - s[top].flag=1; - s[top].a=a; - s[top].b=b; - s[top].c=c; - while(top>0) - { - if (s[top].flag==1) - { - //退栈hanoi(n,a,b,c),相当于在递归函数中将实参传给形参 - m=s[top].num; - a1=s[top].a; - b1=s[top].b; - c1=s[top].c; - top--; - /*将hanoi(n-1,a,b,c)入栈,相当于在递归函数中的第一个递归调用函数, - 将编号为1~n-1的圆盘从塔座a移动到b,c作为辅助塔座*/ - top++; - s[top].num=m-1; - s[top].flag=1; - s[top].a=b1; - s[top].b=a1; - s[top].c=c1; - //将第n个圆盘从a移动到c - top++; - s[top].num=m; - s[top].flag=0; - s[top].a=a1; - s[top].c=c1; - /*将hanoi(n-1,b,a,c)入栈,相当于在递归函数中的第一个递归调用函数, - 将编号为1~n-1的圆盘从塔座b移动到c,a作为辅助塔座*/ - top++; - s[top].num=m-1; - s[top].flag=1; - s[top].a=a1; - s[top].b=c1; - s[top].c=b1; - } - while(top>0&&(s[top].flag==0||s[top].num==1)) - { - if(top>0)//将第n个圆盘从a移动到c,并退栈 - { - PrintHanoi(s[top].num,s[top].a,s[top].c); - top--; - } - } - } -} - diff --git a/2224020139/chapter1/exp1-3.cpp b/2224020139/chapter1/exp1-3.cpp deleted file mode 100644 index 0852b536272341197cfec6e70cf7362262791e89..0000000000000000000000000000000000000000 --- a/2224020139/chapter1/exp1-3.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include "stdio.h" -#include "time.h" -#include -clock_t start,stop; -double duration; -double f1(int n) -{ - int i,flag,j,sum=0; - for(i=2;i<=n;i++)//i=2时候开运行到n,1不是素数 - //输入n=1的时候,判断条件i>n,不满足条件,不执行循环 - { - flag=1; - for(j=2;j=2)sum+=1; - for(i=3;i<=n;i++) - { - flag=1; - k=sqrt(i); - for(j=2;j<=k;j++) - { - if(i%j==0) - { - flag=0; - break; - } - } - if(flag==1)sum++; - } - return sum; - -} - -int main() -{ - int n; - printf("输入一个正整数n:"); - scanf("%d",&n); - start=clock();f1(n); - stop=clock(); - duration=(double)(stop-start)/CLK_TCK; - printf("ticks1=%f\n",(double)(stop-start)); - printf("duration1=%6.2e\n",duration); - printf("1~%d的素数个数为%.0f\n",n,f1(n)); - - start=clock(); - f2(n); - stop=clock(); - duration=(double)(stop-start)/CLK_TCK; - printf("ticks2=%f\n",(double)(stop-start)); - printf("duration2=%6.2e\n",duration); - printf("1~%d的素数个数为%.0f",n,f2(n)); - return 1; -} \ No newline at end of file diff --git a/2224020139/chapter1/exp2-4.cpp b/2224020139/chapter1/exp2-4.cpp deleted file mode 100644 index f0e0aa0a9008739d4f2495272ac997be8d3e654e..0000000000000000000000000000000000000000 --- a/2224020139/chapter1/exp2-4.cpp +++ /dev/null @@ -1,302 +0,0 @@ -#include -#include -#include - -typedef int ElemType; -typedef struct LNode -{ - ElemType data;//数据域 - struct LNode *next;//指针域 - -}LinkNode;// 声明循环单链表结点类型 - -/*---------------------头插法建立循环单链表------------------------*/ -static void create_list_front(LinkNode *&L, ElemType a[], int n) -{ - int i; - LinkNode *s; - - // 创建头结点 - L = (LinkNode *)malloc(sizeof(LinkNode)); - L->next = NULL; - for(i = 0; i < n; i++) - { - // 创建新结点 - s = (LinkNode *)malloc(sizeof(LinkNode)); // 分配内存 - s->data = a[i]; - // 将结点s插入到原开始结点之前,头结点之后 - s->next = L->next; - L->next = s; - } - s = L->next; - while(s->next != NULL)//查找尾结点,由s指向它 - s = s->next; - //尾结点next域指向头结点 - s->next = L; -} - -/*---------------------尾插法建立循环单链表------------------------*/ -static void create_list_rear(LinkNode *&L, ElemType a[], int n) -{ - int i; - LinkNode *s, *r; - - // 创建头结点 - L = (LinkNode *)malloc(sizeof(LinkNode));// 分配内存 - L->next = NULL; - // r始终指向终端结点,开始时指向头结点 - r = L; - for(i = 0; i < n; i++) - { - // 创建新结点 - s = (LinkNode *)malloc(sizeof(LinkNode)); - s->data = a[i]; - // 将结点s插入到结点r之后 - r->next = s; - r = s; - } - // 尾结点next域指向头结点 - r->next = L; -} - -/*---------------------初始化线性表------------------------*/ -static void init_list(LinkNode *&L)//指针的引用 -{ - L = (LinkNode *)malloc(sizeof(LinkNode));// 创建头结点 - L->next = L; -} - -/*---------------------销毁线性表------------------------*/ -static void destroy_list(LinkNode *&L) -{ - LinkNode *pre = L; - LinkNode *p = pre->next; - - while(p != L) - { - free(pre); - // pre,p同步后移一个结点 - pre = p; - p = pre->next; - } - // 此时p = L,pre指向尾结点,释放它 - free(pre); -} - -/*---------------------判断线性表是否为空表------------------------*/ -static bool list_empty(LinkNode *L) -{ - return (L->next == L); -} - -/*---------------------求线性表的长度------------------------*/ -static int list_length(LinkNode *L) -{ - //p指向头结点,i置为0(头结点的序号为0) - int i = 0; - LinkNode *p = L; - - while(p->next != L) - { - i++; - p = p->next; - } - - return i; -} - -/*---------------------输出线性表------------------------*/ -static void display_list(LinkNode *L) -{ - LinkNode *p = L->next; - - // p不为L,输出p结点的data域 - while(p != L) - { - printf("%c ", p->data); - p = p->next; - } - printf("\n"); -} - -/*---------------------求线性表中第i个元素值------------------------*/ -static bool get_elem(LinkNode *L, int i, ElemType &e) -{ - int j = 1; - LinkNode *p = L->next; - - // i错误或者空表返回假 - if(i <=0 || L->next == L) - return false; - // 求第1个结点值,作为特殊情况处理 - if(i == 1) - { - e = L->next->data;// 提取数据域 - return true; - } - else // i不为1时 - { - // 找到第i个结点p - while(j <= i - 1 && p != L) - { - j++; - p = p->next; - } - // 没有找到返回假 - if(p == L) - { - return false; - } - else - { - //找到提取它的值并返回true - e = p->data; - return true; - } - } -} - -/*---------------------查找第一个值域为e的元素序号------------------------*/ -static int locate_elem(LinkNode *L, ElemType e) -{ - int i = 1; - LinkNode *p = L->next; - - // 查找第1个值域为e的结点p - while(p != L && p->data != e) - { - p = p->next; - i++; - } - if(p == L) - return 0; // 没有找到返回0 - else - return i; // 找到返回其序号 -} - -/*---------------------插入第i个元素------------------------*/ -static bool list_insert(LinkNode *&L, int i, ElemType e) -{ - int j = 1; - LinkNode *p = L, *s; - - // i错误返回假 - if(i <= 0) - return false; - // 原单链表为空表或i = 1作为特殊情况处理 - if(p->next == L || i == 1) - { - // 创建新结点 - s = (LinkNode *)malloc(sizeof(LinkNode)); - s->data = e; - // 将结点s插入到结点p之后 - s->next = p->next; - p->next = s; - return true; - } - else - { - p = L->next; - // 找到第i - 1个结点p - while(j <= i - 2 && p != L) - { - j++; - p = p->next; - } - // 未找到第i - 1个结点 - if(p == L) - return false; - else // 找到第i - 1个结点p - { - // 创建新结点s - s = (LinkNode *)malloc(sizeof(LinkNode)); - s->data = e; - // 将结点s插入到结点p之后 - s->next = p->next; - p->next = s; - return true; - } - } -} - -/*---------------------删除第i个元素------------------------*/ -static bool list_delete(LinkNode *&L, int i, ElemType &e) -{ - int j = 1; - LinkNode *p = L, *q; - - // i错误或者空表返回假 - if(i <= 0 || L->next == L) - return false; - // i = 1作为特殊情况处理 - if(i == 1) - { - // 删除第1个结点 - q = L->next; - e = q->data; - L->next = q->next; - free(q); - return true; - } - else - { - // i不为1时 - p = L->next; - // 找到第i - 1个结点p - while(j <= i - 2 && p != L) - { - j++; - p = p->next; - } - // 未找到第i - 1个结点 - if(p == L) - return false; - // 找到第i - 1个结点p - else - { - // q指向要删除的结点 - q = p->next; - e = q->data; - // 从单链表中删除q结点 - p->next = q->next; - // 释放q结点 - free(q); - return true; - } - } -} - -int main(void) -{ - LinkNode *h; - ElemType e; - - printf("循环单链表的基本运算如下:\n"); - printf("(1)初始化循环单链表h\n"); - init_list(h); - printf("(2)依次采用尾插法插入a,b,c,d,e元素\n"); - list_insert(h, 1, 'a'); - list_insert(h, 2, 'b'); - list_insert(h, 3, 'c'); - list_insert(h, 4, 'd'); - list_insert(h, 5, 'e'); - printf("(3)输出循环单链表h:"); - display_list(h); - printf("(4)循环单链表h长度:%d\n", list_length(h)); - printf("(5)循环单链表h为%s\n",(list_empty(h) ? "空" : "非空")); - get_elem(h, 3, e); - printf("(6)循环单链表h的第3个元素:%c\n", e); - printf("(7)元素a的位置:%d\n", locate_elem(h, 'a')); - printf("(8)在第4个元素位置上插入f元素\n"); - list_insert(h, 4, 'f'); - printf("(9)输出循环单链表h:"); - display_list(h); - printf("(10)删除h的第3个元素\n"); - list_delete(h, 3, e); - printf("(11)输出循环单链表h:"); - display_list(h); - printf("(12)释放循环单链表\h"); - destroy_list(h); - - return 0; -} diff --git a/2224020139/chapter3/exp.3-1.cpp b/2224020139/chapter3/exp.3-1.cpp deleted file mode 100644 index d183e1464bdf2f36c143706f320444ea806ae6ae..0000000000000000000000000000000000000000 --- a/2224020139/chapter3/exp.3-1.cpp +++ /dev/null @@ -1,96 +0,0 @@ -Status iniStack(Sqstack &S) -{ - S.base=(SElemType *)malloc(sizeof(SElemType)*stack_INIT_SIZE); - S.base=new SElemType[stackINCREMENT];//等价定义 - if(!S.base) - return ERROR;//分配错误 - - S.top=S.base;//***top初始为base表示栈为空***顺序!!! - - S.stacksize=stack_INIT_SIZE;//栈空间最大容量为stack——INIT——SIZE - return OK; -}//初始化栈 -Status push(Sqstack &S,SElemType x) -{ - if(S.top-S.base==S.stacksize) - return ERROR;//栈满 -(if(S.top-S.base>=S.stacksize){ - S.base=(SElemType * )realloc(S.base, - (S.stacksize+stackINCREMENT)*sizeof(SElemType)); - if(!S.base)return(ERROR); - S.top=S.base+S.stacksize; - S.stacksize+=stackINCREMENT; - })//重新分配空间 - *S.top++=x;//元素x到栈顶,栈顶指针加一 - return OK; -}//入栈 -Status pop(Sqstack &S,SElemType &e) -{ - if(S.base==S.top) - return ERROR;//栈空 - e=*--S.top;//栈顶指针减一,栈顶元素赋给e - return OK; -}//出栈#include -#include -typedef int Status; -typedef char SElemType; -#define stack_INIT_SIZE 100 -#define stackINCREMENT 10 -#define OK 1 -#define ERROR 0 -#define TRUE 1 -#define FALSE 0 - typedef struct { - SElemType *base; //栈底指针 - SElemType *top; //栈顶指针 - int stacksize; //栈空间 -}Sqstack; - -Status iniStack(Sqstack &S) //初始化栈 -{ - /* 请在这里填写答案 */ - }//InitStack - -Status push(Sqstack &S,SElemType x) -{ - /* 请在这里填写答案 */ -} - -Status pop(Sqstack &S,SElemType &e) -{ - /* 请在这里填写答案 */ -} - -Status print_stack(Sqstack S)//打印栈中元素 -{ - SElemType *p=S.base; - while(p -#include - -int* twoSum(int* nums, int numsSize, int target) { - int* result = (int*)malloc(2 * sizeof(int)); - int i, j; - for (i = 0; i < numsSize - 1; i++) { - for (j = i + 1; j < numsSize; j++) { - if (nums[i] + nums[j] == target) { - result[0] = i; - result[1] = j; - return result; - } - } - } - free(result); - return NULL; -} - -int main() { - int nums[] = {2, 7, 11, 15}; - int target = 9; - int* result = twoSum(nums, sizeof(nums) / sizeof(int), target); - if (result != NULL) { - printf("这两个数的下标分别为:%d 和 %d\n", result[0], result[1]); - } else { - printf("找不到和为 %d 的两个数\n", target); - } - return 0; -} diff --git "a/2224020140/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" "b/2224020140/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" deleted file mode 100644 index 02d05afad6e8f03c0f2317098767bedbbda67c75..0000000000000000000000000000000000000000 --- "a/2224020140/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,106 +0,0 @@ -#include -#include -#include - -using namespace std; - -// 定义多项式项的结构体 -struct PolyTerm { - int coef; // 系数 - int exp; // 指数 - PolyTerm(int c, int e) : coef(c), exp(e) {} -}; - -// 定义单链表节点的结构体 -struct PolyNode { - PolyTerm term; // 多项式项 - PolyNode* next; // 指向下一个节点的指针 - PolyNode(PolyTerm t) : term(t), next(nullptr) {} -}; - -// 定义单链表的类 -class PolyList { -public: - PolyList() : head(nullptr) {} - ~PolyList() { - PolyNode* p = head; - while (p) { - PolyNode* temp = p; - p = p->next; - delete temp; - } - } - - // 在链表尾部插入多项式项 - void insert(PolyTerm t) { - PolyNode* p = new PolyNode(t); - if (head == nullptr) { - head = p; - } else { - PolyNode* cur = head; - while (cur->next != nullptr) { - cur = cur->next; - } - cur->next = p; - } - } - - // 打印链表中的多项式项 - void print() { - PolyNode* p = head; - while (p) { - cout << p->term.coef << "x^" << p->term.exp << " "; - p = p->next; - } - cout << endl; - } - - // 两个多项式相乘 - PolyList* multiply(PolyList& other) { - PolyList* result = new PolyList(); - PolyNode* p = head; - PolyNode* q = other.head; - while (p && q) { - PolyTerm t = {p->term.coef * q->term.coef, p->term.exp + q->term.exp}; - result->insert(t); - p = p->next; - q = q->next; - } - return result; - } -private: - PolyNode* head; -}; - -int main() { - PolyList pl1, pl2; - PolyTerm t1 = {1, 1}; - PolyTerm t2 = {2, 0}; - PolyTerm t3 = {3, 1}; - PolyTerm t4 = {4, 2}; - PolyTerm t5 = {5, 0}; - PolyTerm t6 = {6, 1}; - - pl1.insert(t1); - pl1.insert(t2); - pl1.insert(t3); - pl1.insert(t4); - - pl2.insert(t5); - pl2.insert(t6); - - cout << "多项式 pl1: "; - pl1.print(); - - cout << "多项式 pl2: "; - pl2.print(); - - PolyList* result = pl1.multiply(pl2); - - cout << "乘积多项式: "; - result->print(); - - delete result; - - return 0; -} diff --git "a/2224020140/\345\256\236\347\216\260\344\272\214\345\217\211\346\240\221\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2224020140/\345\256\236\347\216\260\344\272\214\345\217\211\346\240\221\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index fb09bbef2a7f6f289254da94e333452ad32aeeb1..0000000000000000000000000000000000000000 --- "a/2224020140/\345\256\236\347\216\260\344\272\214\345\217\211\346\240\221\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,114 +0,0 @@ -// btree.cpp -#include -using namespace std; - -// 二叉树节点结构体 -struct TreeNode { - int val; - TreeNode* left; - TreeNode* right; - TreeNode(int x) : val(x), left(NULL), right(NULL) {} -}; - -// 二叉树创建函数 -TreeNode* CreateBTree(string str) { - if (str.empty()) { - return NULL; - } - TreeNode* root = NULL; - stack s; - int i = 0; - while (i < str.length()) { - if (str[i] == '(') { - s.push(new TreeNode(0)); - i++; - } else if (str[i] == ')') { - TreeNode* p = s.top(); - s.pop(); - if (p->val == 0) { - p->val = i + 1; - } else { - p->right = new TreeNode(i + 1); - } - i++; - } else if (isdigit(str[i])) { - TreeNode* p = s.top(); - s.pop(); - p->val = str[i] - '0'; - if (p->val == 0) { - p->left = new TreeNode(i); - } else { - p->right = new TreeNode(i); - } - i++; - } - } - return root; -} - -// 二叉树输出函数 -void PrintBTree(TreeNode* root) { - if (root == NULL) { - return; - } - cout << root->val << " "; - PrintBTree(root->left); - PrintBTree(root->right); -} - -// 二叉树查找函数 -TreeNode* SearchBTree(TreeNode* root, int val) { - if (root == NULL) { - return NULL; - } - if (root->val == val) { - return root; - } else if (val < root->val) { - return SearchBTree(root->left, val); - } else { - return SearchBTree(root->right, val); - } -} - -// 二叉树高度计算函数 -int CalculateBTreeHeight(TreeNode* root) { - if (root == NULL) { - return 0; - } - int leftHeight = CalculateBTreeHeight(root->left); - int rightHeight = CalculateBTreeHeight(root->right); - return max(leftHeight, rightHeight) + 1; -} - -// 二叉树释放函数 -void ReleaseBTree(TreeNode* root) { - if (root == NULL) { - return; - } - ReleaseBTree(root->left); - ReleaseBTree(root->right); - delete root; -} - -// exp7-1.cpp -#include -#include "btree.cpp" - -int main() { - string str = "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"; - TreeNode* root = CreateBTree(str); - cout << "二叉树创建成功!" << endl; - cout << "二叉树输出: "; - PrintBTree(root); - cout << endl; - TreeNode* hNode = SearchBTree(root, 'H'); - if (hNode != NULL) { - cout << "H 结点的左孩子结点值: " << hNode->left->val << endl; - cout << "H 结点的右孩子结点值: " << hNode->right->val << endl; - } else { - cout << "未找到 H 结点!" << endl; - } - cout << "二叉树高度: " << CalculateBTreeHeight(root) << endl; - ReleaseBTree(root); - return 0; -} diff --git "a/2224020140/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" "b/2224020140/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" deleted file mode 100644 index 85a7dcbc09bbab3cd6deb060841110b1db945d20..0000000000000000000000000000000000000000 --- "a/2224020140/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,115 +0,0 @@ -#include -using namespace std; - -// 定义单链表节点结构体 -struct ListNode { - char data; // 存储节点数据 - ListNode* next; // 指向下一个节点的指针 -}; - -// 初始化单链表 -ListNode* initList() { - ListNode* head = nullptr; // 初始化头节点为空 - return head; -} - -// 尾插法插入元素 -void insertTail(ListNode* head, char value) { - ListNode* newNode = new ListNode(); // 创建新节点 - newNode->data = value; // 赋值节点数据 - newNode->next = nullptr; // 初始化下一个节点为空 - - if (head == nullptr) { // 如果头节点为空,则将新节点作为头节点 - head = newNode; - } else { // 否则,将新节点插入到链表尾部 - ListNode* cur = head; - while (cur->next != nullptr) { // 找到链表最后一个节点 - cur = cur->next; - } - cur->next = newNode; // 将新节点插入到链表尾部 - } -} - -// 输出单链表 -void printList(ListNode* head) { - ListNode* cur = head; // 初始化当前节点为头节点 - while (cur != nullptr) { // 当当前节点不为空时 - cout << cur->data << " "; // 输出当前节点数据 - cur = cur->next; // 将当前节点指向下一个节点 - } - cout << endl; // 输出换行 -} - -// 输出单链表长度 -int lengthList(ListNode* head) { - int len = 0; // 初始化长度为 0 - ListNode* cur = head; // 初始化当前节点为头节点 - while (cur != nullptr) { // 当当前节点不为空时 - len++; // 长度加 1 - cur = cur->next; // 将当前节点指向下一个节点 - } - return len; // 返回长度 -} - -// 判断单链表是否为空 -bool isEmptyList(ListNode* head) { - return head == nullptr; // 如果头节点为空,则链表为空 -} - -// 输出单链表第 index 个元素 -void printElement(ListNode* head, int index) { - ListNode* cur = head; // 初始化当前节点为头节点 - for (int i = 0; i < index; i++) { // 找到第 index 个节点 - if (cur == nullptr) { // 如果找不到第 index 个节点,则输出"找不到该元素" - cout << "找不到该元素" << endl; - return; - } - cur = cur->next; // 将当前节点指向下一个节点 - } - if (cur != nullptr) { // 如果找到第 index 个节点 - cout << cur->data << endl; // 输出第 index 个节点的数据 - } -} - -// 输出元素 a 的位置 -int searchElement(ListNode* head, char value) { - ListNode* cur = head; // 初始化当前节点为头节点 - int index = 0; // 初始化索引为 0 - while (cur != nullptr) { // 当当前节点不为空时 - if (cur->data == value) { // 如果当前节点数据等于目标元素 - return index; // 返回索引 - } - index++; // 索引加 1 - cur = cur->next; // 将当前节点指向下一个节点 - } - return -1; // 如果找不到目标元素,则返回-1 -} - -int main() { - ListNode* head = initList(); // 初始化单链表 - - // 尾插法插入元素 - insertTail(head, 'a'); - insertTail(head, 'b'); - insertTail(head, 'c'); - insertTail(head, 'd'); - insertTail(head, 'e'); - - // 输出单链表 - cout << "单链表: "; - printList(head); - - // 输出单链表长度 - cout << "单链表长度: " << lengthList(head) << endl; - - // 判断单链表是否为空 - cout << "单链表是否为空: " << (isEmptyList(head)? "是" : "否") << endl; - - // 输出单链表第 3 个元素 - printElement(head, 2); - - // 输出元素 a 的位置 - cout << "元素 'a' 的位置: " << searchElement(head, 'a') << endl; - - return 0; -} diff --git "a/2224020140/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" "b/2224020140/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" deleted file mode 100644 index c626834296c162ecce855e3606a05fac1f6d2170..0000000000000000000000000000000000000000 --- "a/2224020140/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" +++ /dev/null @@ -1,125 +0,0 @@ -#include -using namespace std; - -// 定义单链表节点结构体 -struct ListNode { - int val; // 存储节点数据 - ListNode* next; // 指向下一个节点的指针 -}; - -// 初始化单链表 -ListNode* initList() { - ListNode* head = nullptr; // 初始化头节点为空 - ListNode* cur = nullptr; // 初始化当前节点为空 - - int data; - cout << "请输入数据(以-1 结束): "; - cin >> data; - - while (data != -1) { - ListNode* newNode = new ListNode(); // 创建新节点 - newNode->val = data; // 赋值节点数据 - newNode->next = nullptr; // 初始化下一个节点为空 - - if (head == nullptr) { // 如果头节点为空,则将新节点作为头节点 - head = newNode; - } else { // 否则,将新节点插入到链表尾部 - cur->next = newNode; - } - cur = newNode; // 将当前节点指向下一个节点 - - cout << "请输入数据(以-1 结束): "; - cin >> data; - } - - return head; -} - -// 打印单链表 -void printList(ListNode* head) { - ListNode* cur = head; // 初始化当前节点为头节点 - while (cur != nullptr) { // 当当前节点不为空时 - cout << cur->val << " "; // 输出当前节点数据 - cur = cur->next; // 将当前节点指向下一个节点 - } - cout << endl; // 输出换行 -} - -// 分割链表 -ListNode* split1(ListNode* head, int x) { - ListNode* lessThanX = nullptr; // 小于 x 的链表头节点 - ListNode* greaterThanX = nullptr; // 大于等于 x 的链表头节点 - - ListNode* cur = head; // 初始化当前节点为头节点 - ListNode* prev = nullptr; // 初始化前一个节点为空 - - while (cur != nullptr) { // 当当前节点不为空时 - if (cur->val < x) { // 如果当前节点数据小于 x - if (lessThanX == nullptr) { // 如果小于 x 的链表头节点为空 - lessThanX = cur; // 将当前节点作为小于 x 的链表头节点 - } else { // 否则 - prev->next = cur; // 将前一个节点的下一个节点指向当前节点 - } - prev = cur; // 将前一个节点更新为当前节点 - } else { // 否则 - if (greaterThanX == nullptr) { // 如果大于等于 x 的链表头节点为空 - greaterThanX = cur; // 将当前节点作为大于等于 x 的链表头节点 - } else { // 否则 - prev->next = cur; // 将前一个节点的下一个节点指向当前节点 - } - } - cur = cur->next; // 将当前节点指向下一个节点 - } - - prev->next = nullptr; // 将最后一个小于 x 的节点的下一个节点设置为空 - - return lessThanX; // 返回小于 x 的链表头节点 -} - -ListNode* split2(ListNode* head, int x) { - ListNode* lessThanX = nullptr; // 小于 x 的链表头节点 - ListNode* greaterThanX = nullptr; // 大于等于 x 的链表头节点 - - ListNode* cur = head; // 初始化当前节点为头节点 - ListNode* prev = nullptr; // 初始化前一个节点为空 - - while (cur != nullptr) { // 当当前节点不为空时 - if (cur->val >= x) { // 如果当前节点数据大于等于 x - if (greaterThanX == nullptr) { // 如果大于等于 x 的链表头节点为空 - greaterThanX = cur; // 将当前节点作为大于等于 x 的链表头节点 - } else { // 否则 - prev->next = cur; // 将前一个节点的下一个节点指向当前节点 - } - prev = cur; // 将前一个节点更新为当前节点 - } else { // 否则 - if (lessThanX == nullptr) { // 如果小于 x 的链表头节点为空 - lessThanX = cur; // 将当前节点作为小于 x 的链表头节点 - } else { // 否则 - prev->next = cur; // 将前一个节点的下一个节点指向当前节点 - } - } - cur = cur->next; // 将当前节点指向下一个节点 - } - - prev->next = nullptr; // 将最后一个大于等于 x 的节点的下一个节点设置为空 - - return lessThanX; // 返回小于 x 的链表头节点 -} - -int main() { - ListNode* head = initList(); // 初始化单链表 - - // 分割链表 - ListNode* lessThanX = split1(head, 5); - ListNode* greaterThanX = split2(lessThanX, 5); - - // 打印小于 x 的链表 - printList(lessThanX); - cout << endl; - - // 打印大于等于 x 的链表 - printList(greaterThanX); - cout << endl; - - return 0; -} diff --git "a/2224020140/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" "b/2224020140/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" deleted file mode 100644 index 04314e4220144ea18069377d0faf34e68fb138d8..0000000000000000000000000000000000000000 --- "a/2224020140/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" +++ /dev/null @@ -1,21 +0,0 @@ -#include - -int sum_of_odd_length_subarrays(int arr[], int len) { - int sum = 0; - for (int i = 0; i < len; i++) { - for (int j = i; j < len; j++) { - if (j - i + 1 % 2 != 0) { - sum += arr[j]; - } - } - } - return sum; -} - -int main() { - int arr[] = {1, 2, 3, 4, 5}; - int len = sizeof(arr) / sizeof(arr[0]); - int result = sum_of_odd_length_subarrays(arr, len); - printf("所有奇数长度子数组的和为:%d\n", result); - return 0; -} diff --git "a/2224020140/\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" "b/2224020140/\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" deleted file mode 100644 index b63f5c4195d0f7df58f5708613d4ea588abf27cb..0000000000000000000000000000000000000000 --- "a/2224020140/\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include - -int calculateSumUsingIteration(int n) { - int sum = 0; - for (int i = 1; i <= n; i++) { - sum += i; - } - return sum; -} - -int calculateSumUsingGauss(int n) { - return n * (n + 1) / 2; -} - -int main() { - int n = 1000000; // 你可以根据需要更改 n 的值 - clock_t start, end; - double duration; - - // 逐个累加求解 - start = clock(); - int sumUsingIteration = calculateSumUsingIteration(n); - end = clock(); - duration = (double)(end - start) / CLOCKS_PER_SEC; - printf("逐个累加求解的和为:%d,求解时间为:%f 秒\n", sumUsingIteration, duration); - - // 高斯法求解 - start = clock(); - int sumUsingGauss = calculateSumUsingGauss(n); - end = clock(); - duration = (double)(end - start) / CLOCKS_PER_SEC; - printf("高斯法求解的和为:%d,求解时间为:%f 秒\n", sumUsingGauss, duration); - - return 0; -} diff --git "a/2224020140/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2224020140/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index b63f5c4195d0f7df58f5708613d4ea588abf27cb..0000000000000000000000000000000000000000 --- "a/2224020140/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include - -int calculateSumUsingIteration(int n) { - int sum = 0; - for (int i = 1; i <= n; i++) { - sum += i; - } - return sum; -} - -int calculateSumUsingGauss(int n) { - return n * (n + 1) / 2; -} - -int main() { - int n = 1000000; // 你可以根据需要更改 n 的值 - clock_t start, end; - double duration; - - // 逐个累加求解 - start = clock(); - int sumUsingIteration = calculateSumUsingIteration(n); - end = clock(); - duration = (double)(end - start) / CLOCKS_PER_SEC; - printf("逐个累加求解的和为:%d,求解时间为:%f 秒\n", sumUsingIteration, duration); - - // 高斯法求解 - start = clock(); - int sumUsingGauss = calculateSumUsingGauss(n); - end = clock(); - duration = (double)(end - start) / CLOCKS_PER_SEC; - printf("高斯法求解的和为:%d,求解时间为:%f 秒\n", sumUsingGauss, duration); - - return 0; -} diff --git "a/2224020141/\346\201\242\345\244\215IP\345\234\260\345\235\200.cpp" "b/2224020141/\346\201\242\345\244\215IP\345\234\260\345\235\200.cpp" deleted file mode 100644 index 6e4be565adf85fd2be29273cb617f32bcdbbcde7..0000000000000000000000000000000000000000 --- "a/2224020141/\346\201\242\345\244\215IP\345\234\260\345\235\200.cpp" +++ /dev/null @@ -1,51 +0,0 @@ -#include -#define MaxSize 100 -typedef struct{ - char data[MaxSize]; - int length; -}IP; -void addch(IP&ip,char ch) -{ - ip.data[ip.length]=ch; - ip.length++; -} -IP addot(IP ip) -{ - addch(ip,'.' ); - return ip; -} -void solveip(char s[],int n,int start,int step,IP ip) -{ - if(start<=n) - { - if(start==n&&step==4) - { - for(int i=0;i -#define MaxLen 10 -void fun(int a[MaxLen][MaxLen],int n) -{ - int i,j,k=0,m; - if(n%2==0)m=n/2; - else m=n/2+1 - for(i=0;i=i;j--) - { - k++; - a[n-i-1][j]=k; - } - for(j=n-i-2;j>=i+1;j--) - { - k++; - a[j][i]=k; - } - } -} -int main() -{ - int n,j,i; - int a[MaxLen][MaxLen]; - printf("输入n(n<10):"); - scanf("%d",&n); - fun(a,n); - printf("%d阶数字方阵如下:\n",n) - for(i=0;i -#include -#include -#define MaxSize 100 -void Hanoil(int n,char a,char b,char c) -{ - if(n==1) - { - printf("\t将第%d个盘片从%c中移动到%c\n",n,a,c) ; - } - else { - Hanoil(n-1,a,c,b); - printf("\t将第%d个盘片从%c中移动到%c\n",n,a,c) ; - Hanoil(n-1,b,a,c); - } -} -typedef struct -{ - int n; - char x,y,z: - bool flag; -}ElemType; -typedef struct -{ - ElemType data[MaxSize]; - int top; -}StackType; -void InitStack(StackType*&s) -{ - s=(StackType*)malloc(sizeof(StackType)); - s->top=-1; -} -void DestoryStack(StackType*&s) -{ - free(s); -} -bool StackEmpty(StackType*s) -{ - return(s->top== -1); -} -bool push(StackType *&s,ElemType e) -{ - if(s->top==MaxSize-1) - { - return false; - } - s->top++; - s->data[s->top]=e; - return true; -} -bool pop(StackType*&s,ElemType &e) -{ - if(s->top==-1) - { - return false; - e=s->data[s->top]; - s->top--; - return true; - } -} -void Hanoi2(int n,char x,char y,char z) -{ - StackType *st; - ElemType e,e1,e2,e3; - if(n>0)return; - InitStack(st); - e.n=n;e.x=x;e.y=y;e.z=z;e.flag=false; - push(st,e); - while(!StackEmpty(st)) - { - pop(st,e); - if(e.flag==false) - { - e1.n=e.n-1;e1.x=e.y;e1.y=e.x;e1.z=e.z; - if(e1.n==1) - { - e1.flag=true; - } - else { - e1.flag=false; - - } - push(st,e1); - e2.n=e.n;e2.x=e.x;e2.y=e.y;e2.z=e.z;e2.flag=true; - push(st,e2); - e3.n=e.n-1;e3.x=e.x;e3.y=e.z;e3.z=e.y; - if(e3.n==1) - { - e3.flag=false; - } - else{ - e3.flag=false; - } - push(st,e3); - } - else{ - printf("\t将第%d个盘片从%移动到%c\n",e.n,e.x,e.z); - } - } - DestoryStack(st); -} -int main() -{ - int n=3; - printf("递归算法:%d个盘片移动过程:\n",n); - Hanoil(n,'x','y','z'); - printf("非递归算法:%d个盘片移动过程:\n",n); - Hanoil2(n,'x','y','z'); - return 1; -} \ No newline at end of file diff --git "a/2224020142/\346\212\230\345\215\212\347\256\227\346\263\225.cpp" "b/2224020142/\346\212\230\345\215\212\347\256\227\346\263\225.cpp" deleted file mode 100644 index b74b3c193a1b801700985f48bd523f421b72b68d..0000000000000000000000000000000000000000 --- "a/2224020142/\346\212\230\345\215\212\347\256\227\346\263\225.cpp" +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#define MAXL 100 -typedef int keytype; -typedef char infotype; -typedef struct -{ - keytype key; - infotype data; -}Rectype; - -void CreateList(Rectype R[],keytype keys[],int n) -{ - for(int i=0;ik) - return mid-1; - else - low=mid+1; - } - return 0; - } - int main() - { - Rectype R[MAXL]; - keytype k=9; - int a[]={1,2,3,4,5,6,7,8,9,10},i,n=10; - CreateList(R,a,n); - printf("关键字数列:");displist(R,n); - printf("查找%d的比较过程如下:\n",k); - if((i=binsearch(R,n,k))!=0) - printf("元素%d的位置是%d\n",k,i); - else - printf("元素%d不在表中\n",k); - return 1; - } \ No newline at end of file diff --git "a/2224020142/\346\261\202\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" "b/2224020142/\346\261\202\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" deleted file mode 100644 index 072842aa93e2a7ff4c80c51c53fd364ff97b8df8..0000000000000000000000000000000000000000 --- "a/2224020142/\346\261\202\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -#include -int main(){ - int arr[]={2,3,4,5,6}; - int n=sizeof(arr)/sizeof(arr[0]); - int sum=0; - for(int i=0;i<=n;i++){ - for (int len=1;len<=n-i;len+=2){ - for(int j=i;j -#include -#include -long add1(long n) -{ long i,sum=0; - for(i=1;i<=n;i++) - sum +=i; - return sum; -} -void AddTime1(long n) -{ - clock_t t; - long sum; - t=clock(); - sum=add1(n); - t = clock()-t; - printf("方法1:\n"); - printf("结果:1~%d之和: % 1d\n",n,sum); - printf("用时: % 1f秒\n",((float)t)/CLOCKS_PER_SEC); -} -long add2(long n) -{ - return n*(n+1)/2; -} -void AddTime2(long n) -{ - clock_t t; - long sum; - t=clock(); - sum=add2(n); - t=clock()-t; - printf("方法2:\n"); - printf("结果:1~%d之和:%1d\n",n,sum); - printf("用时:%1f秒\n",((float)t)/CLOCKS_PER_SEC); -} -int main() -{ - int n; - printf("n(大于1000000):"); - scanf("%d",&n); - if(n<1000000) return 0; - AddTime1(n); - AddTime2(n); - return 1; -} \ No newline at end of file diff --git "a/2224020143/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2224020143/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index c4f5483751844431eb595193545531a534b90d91..0000000000000000000000000000000000000000 --- "a/2224020143/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,55 +0,0 @@ -#include(stdio.h) -#include(time.h) -#include(math.h) -bool prime1(long n) -{ - long i; - for(i=2; i -#include -#define MAXL 100 -typedef int KeyType; -typedef char InfoType; -typedef struct -{ - KeyType key; - InfoType data; -} RecType; -void CreateList(RecType R[],KeyType keys[],int n) -{ - for(int i=0;ik) - high=mid-1; - else - low=mid+1; - } - return 0; -} -int main() -{ - RecType R[MAXL]; - KeyType k=9; - int a[]={1,2,3,4,5,6,7,8,9,10},i,n=10; - CreateList(R,a,n); - printf("关键字序列:");DispList(R,n); - printf("查找%d所比较的关键字:\n",k); - if((i=BinSearch(R,n,k))!=0) - printf("元素%d的位置是%d\n",k,i); - else - printf("元素%d不在表中\n",k); - return 1; -} \ No newline at end of file diff --git "a/2224020144/\347\254\254\344\272\214\347\253\240/\345\215\225\351\223\276\350\241\250\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" "b/2224020144/\347\254\254\344\272\214\347\253\240/\345\215\225\351\223\276\350\241\250\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" deleted file mode 100644 index de6f211c20f6c98213c44285fec2250553dcb2fd..0000000000000000000000000000000000000000 --- "a/2224020144/\347\254\254\344\272\214\347\253\240/\345\215\225\351\223\276\350\241\250\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.cpp" +++ /dev/null @@ -1,181 +0,0 @@ -#include -#include -typedef char ElemType; -typedef struct LNode -{ ElemType data; - struct LNode * next; -}LinkNode; -void CreateListF(LinkNode *&L,ElemType a[],int n) -{ - LinkNode *s; - L=(LinkNode *)malloc(sizeof(LinkNode)); - L->next= NULL; - for (int i=0;idata=a[i]; - s->next=L->next; - L->next=s; - } -} -void CreateListR(LinkNode * &L,ElemType a[],int n) -{ - LinkNode *s,*r; - L=(LinkNode *)malloc(sizeof(LinkNode)); - L->next=NULL; - r=L; - for(int i=0;i data=a[ i]; - r ->next=s; - r=s; - } - r->next==NULL; -} -void InitList(LinkNode *&L) -{ - L=(LinkNode *)malloc(sizeof(LinkNode)); - L->next = NULL; -} -void DestroyList(LinkNode * &L) -{ - LinkNode *pre=L,*p=pre->next; - while (p!=NULL) - { - free(pre); - pre=p; - p=pre->next; - } - free(pre); -} -bool ListEmpty(LinkNode *L) -{ - return(L->next==NULL) ; -} -int ListLength(LinkNode *L) -{ - int i=0; - LinkNode *p=L; - while(p->next!=NULL) - { - i++; - p=p->next; - } - return(i); -} -void DispList(LinkNode *L) -{ - LinkNode *p=L->next; - while(p!=NULL) - { - printf("&c",p->data); - p=p->next; - } - printf("\n"); -} -bool GetElem(LinkNode *L,int i,ElemType &e) -{ - int j=0; - LinkNode *p=L; - if (i<=0) return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - e=p->data; - return true; - } -} -int LocateElem(LinkNode *L,ElemType e) -{ - int i=1; - LinkNode*p=L->next; - while(p!=NULL && p->data!=e) - { - p=p->next; - i++; - } - if(p==NULL) - return(0); - else - return(i); -} -bool ListInsert(LinkNode * &L,int i,ElemType e) -{ - int j=0; - LinkNode *p=L,*s; - if(i<=0) return false; - while(jnext; - } - if (p==NULL) - return false; - else - { - s=(LinkNode*)malloc(sizeof(LinkNode)); - s->data=e; - s->next=p->next; - p->next=s; - return true; - } -} -bool ListDelete(LinkNode * &L, int i,ElemType &e) -{ - int j= 0; - LinkNode *p=L,*q; - if(i<=0) return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - q=p->next; - if(q==NULL) - return false; - e=q->data; - p->next=q-> next; - free(q); - return true; - } -} -int main() -{ - LinkNode *h; - ElemType e; - printf("单链表的基本运算如下:\n"); - printf("(1)初始化单链表h\n"); - InitList(h); - printf("(2)依次采用尾插法插入元素a,b,c,d,e\n"); - ListInsert(h,1,'a'); - ListInsert(h,2,'b'); - ListInsert(h,3,'c'); - ListInsert(h,4,'d'); - ListInsert(h,5,'e'); - printf("(3)输出单链表h:");DispList(h); - printf("(4)单链表h长度:8d\n",ListLength(h)); - printf("(5)单链表h为&s\n",(ListEmpty(h)?"空":"非空")); - GetElem(h,3,e); - printf("(6)单链表h的第3个元素:%c\n",e); - printf("(7)元素a的位置:d\n",LocateElem(h,'a')); - printf("(8)在第4个元素位置上插入元素f\n"); - ListInsert(h,4, 'f'); - printf("(9)输出单链表h:");DispList(h); - printf("(10)删除h的第3个元素\n"); - ListDelete(h,3,e); - printf("(11)输出单链表h:");DispList(h); - printf("(12释放单链表h\n"); - DestroyList(h); - return 1; -} diff --git "a/2224020144/\347\254\254\344\272\214\347\253\240/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" "b/2224020144/\347\254\254\344\272\214\347\253\240/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" deleted file mode 100644 index 3f88b256e80197ae24ced6f47a7befa92e22a7a6..0000000000000000000000000000000000000000 --- "a/2224020144/\347\254\254\344\272\214\347\253\240/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.cpp" +++ /dev/null @@ -1,71 +0,0 @@ -#include"linknode.cpp" -#include -void Split1(LinkNode*&L) -{ - LinkNode *pre,*p,*q; - if(L->next== NULL|| L->next==NULL) - return; - int x=L->next->data; - pre=L->next; - p=pre->next; - while(p!=NULL) - { - if(p->datanext=p->next; - p->next=L->next; - L->next=P; - p=pre->next; - } - else - { - pre=p; - p=pre->next; - } - } -} -void Split2(LinkNode*&L) -{ - LinkNode *p=L->next,*r,*L1,*r1; - if(L->next== NULL|| L->next->next==NULL) - return; - int x=L->next->data; - r=L; - L1=(LinkNode*)malloc(sizeof(LinkNode)); - r1=L1; - while(p!=NULL) - { - if(p->datanext=P;r=P; - P=p->next; - } - else - { - r1->next=p;r1=P; - p=p->next; - } - } - r1->next= NULL; - x->next=L1->next; - free(L1); -} -int main() -{ - LinkNode *L; - ElemType a[]="daxgdchaeb"; - int n= strlen(a); - printf("解法1\n"); - CreateListR(L,a,n); - printf("L:");DispList(L); - printf("以首结点值进行划分\n");Split1(L); - printf("L:");DispList(L); - DestroyList(L); - printf("解法2\n"); - CreateListR(Lan); - printf("L:"); DispList(L); - printf("以首结点值进行划分\n");Split2(L); - printf("L:");DispList(L); - DestroyList(L); - return 1; -} \ No newline at end of file diff --git a/2224020147/chapter9/exp.9-2.cpp b/2224020147/chapter9/exp.9-2.cpp deleted file mode 100644 index 520cce94e3fea51bbbdf61a74d5dca7fbef4d36e..0000000000000000000000000000000000000000 --- a/2224020147/chapter9/exp.9-2.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include -int main() -{ - int c; - int a[10]={1,2,3,4,5,6,7,8,9,10}; - int search(int b[],int k,int n); - c=search(a,9,9); - printf("9所在逻辑位置为%d",c); -} -int search(int b[],int k,int n) -{ - int mid,low,high; - low=0;high=n-1;mid=(low+high)/2; - while(low<=high) - if(k==b[mid]) - return mid+1; - if(k>b[mid]) - { - low=mid+1; - mid=(low+high)/2; - } - else - high=mid-1; - mid=(low+high)/2; - return 0; -} \ No newline at end of file diff --git "a/2224020148/2\346\225\260\344\271\213\345\222\214" "b/2224020148/2\346\225\260\344\271\213\345\222\214" deleted file mode 100644 index ea4742e056a4f56215458b435a3b2b54ca2181a0..0000000000000000000000000000000000000000 --- "a/2224020148/2\346\225\260\344\271\213\345\222\214" +++ /dev/null @@ -1,9 +0,0 @@ -#include -int main() -{ -int a,b,sum; -scanf("%d%d",&a,&b); -sum= a + b; -printf("%d\n",sum); -return 0; -} \ No newline at end of file diff --git "a/2224020151/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2224020151/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index 6919f43de52fbec9adcbf8945693d7ac8509a7f6..0000000000000000000000000000000000000000 --- "a/2224020151/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include - -bool prime1(long n) -{ long i; - for (i = 2;i -int main() -{ - //输入数据 - int num1 = 0; int num2 = 0; - //获取数据 - scanf("%d%d", &num1, &num2); - //建立求和代数式 - int sum = num1 + num2; - //输出数据 - printf("%d", sum); - return 0; -} \ No newline at end of file diff --git "a/2224020153/\347\254\254\344\272\214\347\253\240/linklist.cpp" "b/2224020153/\347\254\254\344\272\214\347\253\240/linklist.cpp" deleted file mode 100644 index ca981f7c844c90a09f46ec3d244af283f1b12e09..0000000000000000000000000000000000000000 --- "a/2224020153/\347\254\254\344\272\214\347\253\240/linklist.cpp" +++ /dev/null @@ -1,128 +0,0 @@ -#include -#include -typedef char ElemType; -typedef struct LNode -{ - ElemType data; - struct LNode *next; //指向后继节点 -}LinkNode; -void InitList(LinkNode *&L)//初始化线性表 1) -{ - L=(LinkNode *)malloc(sizeof(LinkNode));//创建头结点 - L->next=NULL; -} -bool ListInsert(LinkNode *&L,int i,ElemType e)//插入数据元素 2) -{ - int j=0; - LinkNode *p=L,*s; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - s=(LinkNode *)malloc(sizeof(LinkNode)); - s->data=e; - s->next=p->next; - p->next=s; - return true; - } -} -void DispList(LinkNode *L)//输出线性表 3) 9) 11) -{ - LinkNode *p=L->next; - while(p!=NULL) - { - printf("%c",p->data); - p=p->next; - } - printf("\n"); -} -int ListLength(LinkNode *L)//求线性表的长度 4) -{ - int n=0; - LinkNode *p=L; - while (p->next!=NULL) - { - n++; - p=p->next; - } - return (n); -} -bool ListEmpty(LinkNode *L)//判断单链表是否为空5) -{ - return (L->next==NULL); -} -bool GetElem(LinkNode *L,int i,ElemType &e)//某个数据元素值6) -{ - int j=0; - LinkNode *p=L; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - e=p->data; - return true; - } -} -int LocateElem(LinkNode *L,ElemType e)//元素的位置7) -{ - int i=1; - LinkNode *p=L->next; - while(p!=NULL&&p->data!=e) - { - p=p->next; - i++; - } - if(p==NULL) - return (0); - else - return (i); -} -bool ListDelete(LinkNode *&L,int i,ElemType &e) -{ - int j=0; - LinkNode *p=L,*q; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - q=p->next; - if(q==NULL) - return false; - e=q->data; - p->next=q->next; - free(q); - return true; - } -} -void DestroyList(LinkNode *&L)//释放单链表12) -{ - LinkNode *pre=L,*p=L->next; - while(p!=NULL) - { - free(pre); - pre=p; - p=pre->next; - } - free(pre); -} \ No newline at end of file diff --git "a/2224020154/\347\254\2542\347\253\240/13\351\241\265\345\256\236\351\252\214\351\242\2302.cpp" "b/2224020154/\347\254\2542\347\253\240/13\351\241\265\345\256\236\351\252\214\351\242\2302.cpp" deleted file mode 100644 index e36b39b7e6673d8cd749a7ff2ced3290c5dbd929..0000000000000000000000000000000000000000 --- "a/2224020154/\347\254\2542\347\253\240/13\351\241\265\345\256\236\351\252\214\351\242\2302.cpp" +++ /dev/null @@ -1,34 +0,0 @@ -#include"linklist.cpp" -int main() -{ - LinkNode *h; - ElemType e; - printf("单链表的基本运算如下:\n"); - printf("(1)初始化单链表h.\n"); - InitList(h); - printf("(2)依次采用尾插法插入a,b,c,d,e元素.\n"); - ListInsert(h,1,'a'); - ListInsert(h,2,'b'); - ListInsert(h,3,'c'); - ListInsert(h,4,'d'); - ListInsert(h,5,'e'); - printf("(3)输出单链表h:"); - DispList(h); - printf("(4)输出单链表h的长度:%d\n",ListLength(h)); - printf("(5)判断单链表h是否为空:%s\n",(ListEmpty(h)?"空":"非空")); - GetElem(h,3,e); - printf("(6)输出单链表h的第3个元素:%c\n",e); - printf("(7)输出元素a的位置:%d\n",LocateElem(h,'a')); - printf("(8)在第4个元素位置上插入f元素.\n"); - ListInsert(h,4,'f'); - printf("(9)输出单链表h:"); - DispList(h); - printf("(10)删除单链表h的第3个元素.\n"); - ListDelete(h,3,e); - printf("(11)输出单链表h:"); - DispList(h); - printf("(12)释放单链表h\n"); - DestroyList(h); -} - - \ No newline at end of file diff --git "a/2224020154/\347\254\2542\347\253\240/linklist.cpp" "b/2224020154/\347\254\2542\347\253\240/linklist.cpp" deleted file mode 100644 index 82aa32c7ca5e80b64a973d8f40728d9531f92733..0000000000000000000000000000000000000000 --- "a/2224020154/\347\254\2542\347\253\240/linklist.cpp" +++ /dev/null @@ -1,128 +0,0 @@ -#include -#include -typedef char ElemType; -typedef struct LNode -{ - ElemType data; - struct LNode *next; //指向后继节点 -}LinkNode; -void InitList(LinkNode *&L)//初始化线性表 1) -{ - L=(LinkNode *)malloc(sizeof(LinkNode));//创建头结点 - L->next=NULL; -} -bool ListInsert(LinkNode *&L,int i,ElemType e)//插入数据元素 2) -{ - int j=0; - LinkNode *p=L,*s; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - s=(LinkNode *)malloc(sizeof(LinkNode)); - s->data=e; - s->next=p->next; - p->next=s; - return true; - } -} -void DispList(LinkNode *L)//输出线性表 3) 9) 11) -{ - LinkNode *p=L->next; - while(p!=NULL) - { - printf("%c",p->data); - p=p->next; - } - printf("\n"); -} -int ListLength(LinkNode *L)//求线性表的长度 4) -{ - int n=0; - LinkNode *p=L; - while (p->next!=NULL) - { - n++; - p=p->next; - } - return (n); -} -bool ListEmpty(LinkNode *L)//判断单链表是否为空5) -{ - return (L->next==NULL); -} -bool GetElem(LinkNode *L,int i,ElemType &e)//某个数据元素值6) -{ - int j=0; - LinkNode *p=L; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - e=p->data; - return true; - } -} -int LocateElem(LinkNode *L,ElemType e)//元素的位置7) -{ - int i=1; - LinkNode *p=L->next; - while(p!=NULL&&p->data!=e) - { - p=p->next; - i++; - } - if(p==NULL) - return (0); - else - return (i); -} -bool ListDelete(LinkNode *&L,int i,ElemType &e) -{ - int j=0; - LinkNode *p=L,*q; - if(i<=0) - return false; - while(jnext; - } - if(p==NULL) - return false; - else - { - q=p->next; - if(q==NULL) - return false; - e=q->data; - p->next=q->next; - free(q); - return true; - } -} -void DestroyList(LinkNode *&L)//释放单链表12) -{ - LinkNode *pre=L,*p=L->next; - while(p!=NULL) - { - free(pre); - pre=p; - p=pre->next; - } - free(pre); -} diff --git "a/2224020154/\347\254\2543\347\253\240/59\351\241\265\345\256\236\351\252\214\351\242\2301.cpp" "b/2224020154/\347\254\2543\347\253\240/59\351\241\265\345\256\236\351\252\214\351\242\2301.cpp" deleted file mode 100644 index 5680db92c49786b1733e4ba014166aedbd58b157..0000000000000000000000000000000000000000 --- "a/2224020154/\347\254\2543\347\253\240/59\351\241\265\345\256\236\351\252\214\351\242\2301.cpp" +++ /dev/null @@ -1,26 +0,0 @@ -#include"SqStack.cpp" -int main() -{ ElemType e; - SqStack*s; - printf("顺序栈s的基本运算如下:\n"); - printf(" (1)初始化栈s\n"); - InitStack(s); - printf(" (2)栈为%s\n",(SqStackEmpty(s)?"空":"非空")); - printf(" (3)依次进栈元素a,b,c,d,e\n"); - Push(s,'a'); - Push(s,'b'); - Push(s,'c'); - Push(s,'d'); - Push(s,'e'); - printf(" (4)栈为%s\n",(SqStackEmpty(s)?"空":"非空")); - printf(" (5)出栈序列:"); - while (!SqStackEmpty(s)) - { Pop(s,e); - printf("%c",e); - } - printf("\n"); - printf(" (6)栈为%s\n",(SqStackEmpty(s)?"空":"非空")); - printf(" (7)释放栈\n"); - DestroyStack(s); - return 1; -} \ No newline at end of file diff --git "a/2224020154/\347\254\2543\347\253\240/sqstack.cpp" "b/2224020154/\347\254\2543\347\253\240/sqstack.cpp" deleted file mode 100644 index c31e6321faf89ba578cdaae9d4565cab627d1245..0000000000000000000000000000000000000000 --- "a/2224020154/\347\254\2543\347\253\240/sqstack.cpp" +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct -{ ElemType data[MaxSize]; - int top; //栈顶指针 -}SqStack; //声明顺序栈类型 -void InitStack(SqStack*&s) //初始化栈类型 -{ s=(SqStack*)malloc(sizeof(SqStack)); - s->top=-1; -} -void DestroyStack(SqStack*&s) //销毁栈 -{ - free(s); -} -bool SqStackEmpty(SqStack*s) //判断栈是否为空栈 -{ - return(s->top==-1); -} -bool Push(SqStack*&s,ElemType e) //进栈 -{ if(s->top==MaxSize-1) - return false; - s->top++; - s->data[s->top]=e; - return true; -} -bool Pop(SqStack*&s,ElemType &e) //出栈 -{ if(s->top==-1) - return false; - e=s->data[s->top]; - s->top--; - return true; -} -bool GeTop(SqStack*s,ElemType&e) //取栈顶元素 -{ if(s->top==-1) - return false; - e=s->data[s->top]; - return true; -} \ No newline at end of file diff --git "a/2224020158/chapter1/\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.c" "b/2224020158/chapter1/\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.c" deleted file mode 100644 index 02b90f4cbefbb5ca5123f2a6413e549b869973ee..0000000000000000000000000000000000000000 --- "a/2224020158/chapter1/\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.c" +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include -long add1(long n) -{ long a,sum=0; - for(a=1;a<=n;a++) - sum=sum+a; - return sum; -} -void Addtime1(long n) -{ - clock_t t; - long sum; - t = clock(); - sum=add1(n); - t = clock()-t; - printf (":\n"); - printf("结果:1~%d之和:%ld\n",n,sum); - printf("用时:%lf秒\n",((float)t)/CLOCK_PER_SEC); -} -int main() -{ int n; - printf("n(大于1000000):"); - scanf("%d",&n); - if (n<1000000)return 0; - Addtime1(n); - return 1; - } \ No newline at end of file diff --git "a/2224020158/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.c" "b/2224020158/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.c" deleted file mode 100644 index 725728eff0458807eb8527f73d2c490b1ff4ecd9..0000000000000000000000000000000000000000 --- "a/2224020158/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.c" +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include -#include -bool prime1(long n) -{ - long x; - for (x=2;x -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#if __cplusplus >= 201103L -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#endif - -using namespace std; - -//leetcode代码 -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode() : val(0), left(nullptr), right(nullptr) {} - * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} - * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} - * }; - */ -struct TreeNode { - int val; - TreeNode *left; - TreeNode *right; - - TreeNode() : val(0), left(nullptr), right(nullptr) {} - TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} - TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} -}; - - -TreeNode* CreateTree(vectorData) { - vectorstr_data(Data.size(), ""); - for (int i = 0; i < Data.size(); i++) { - if (Data[i] != NULL) - str_data[i] = to_string(Data[i]); - else - str_data[i] = "null"; - } - queueq; - TreeNode* head = new TreeNode(stoi(str_data[0])); - q.push(head); - int i = 1; - while (i < str_data.size()) { - TreeNode* tmp = q.front(); - q.pop(); - if (str_data[i] != "null") { - TreeNode* new_node = new TreeNode(stoi(str_data[i])); - tmp->left = new_node; - q.push(new_node); - } - i++; - if (str_data[i] != "null") { - TreeNode* new_node = new TreeNode(stoi(str_data[i])); - tmp->right = new_node; - q.push(new_node); - } - i++; - } - return head; -} - - -void prePrint(TreeNode* Root) {//先序遍历 - if (Root == NULL) return; - cout << Root->val << ','; - prePrint(Root->left); - prePrint(Root->right); - return; -} -void postPrint(TreeNode* Root) {//后序遍历 - if (Root == NULL) return; - postPrint(Root->left); - postPrint(Root->right); - cout << Root->val << ','; - return; -} -void inPrint(TreeNode* Root) {//中序遍历 - if (Root == NULL) return; - inPrint(Root->left); - cout << Root->val << ','; - inPrint(Root->right); - return; -} - - -class Solution { -public: - bool compare(TreeNode* left, TreeNode* right){ - if(left ==nullptr && right !=nullptr) return false; - else if(right == nullptr && left!= nullptr) return false; - else if(right == nullptr && right == nullptr) return true; - else if(right->val != left->val) return false; - else { - return compare(left->left,right->right)&& - compare(left->right,right->left); - } - } - - bool isSymmetric(TreeNode* root) { - if(root == nullptr) return true; - return compare(root->left,root->right); - } -}; - -//主函数 -int main(){ - //在这里定义好函数中的测试数据 - // root = [1,2,2,3,4,4,3] - // 输出:true - vectorData = { 1,2,3, NULL,4,5,6,7, NULL }; - TreeNode* Root = NULL; - Root = CreateTree(Data); - cout << "PreOrder:" << endl; - prePrint(Root); - cout << endl << "InOrder:" << endl; - inPrint(Root); - cout << endl << "PostOrder:" << endl; - postPrint(Root); - //root = [1,2,2,3,4,4,3]; - TreeNode* root_1 = NULL; - vectorroot_test_1 = { 1,2,2,3,4,4,3 }; - root_1 = CreateTree(root_test_1); - cout << endl; - Solution sol; // int i; - cout << sol.isSymmetric(root_1) << endl; - - // root = [1,2,2,null,3,null,3] - TreeNode* root_2 = NULL; - vectorroot_test_2 = { 1,2,2,NULL,3,NULL,3 }; - root_2 = CreateTree(root_test_2); - cout << sol.isSymmetric(root_2) << endl; - - // ........ - - return 0; -} diff --git a/README.md b/README.md index cde46dd532f8df5d2cfeaeadd4b557cbce1403c3..3faee1cb0537113851e96831fc5cf05a52102ef6 100644 --- a/README.md +++ b/README.md @@ -64,11 +64,3 @@ 教材 401 页:第1、3、4、8、11、14题。 上机实验283-300页:第4、5、12题。 - - -## 问题 -1. 克隆出现`error: invalid path`错误: -不符合NTFS策略的文件不会被签出,设置为`false`关闭保护机制: -```shell -git config --global core.protectNTFS false -``` diff --git "a/chap 1\344\270\244\346\225\260\344\271\213\345\222\214" "b/chap 1\344\270\244\346\225\260\344\271\213\345\222\214" new file mode 100644 index 0000000000000000000000000000000000000000..29b9897ed0a3188d37f21c7d0fd5864dd063923b --- /dev/null +++ "b/chap 1\344\270\244\346\225\260\344\271\213\345\222\214" @@ -0,0 +1,16 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) { + int i,j; + for (i=1;i