diff --git a/2020-Autumn/Backend/task_02/Zeppel-Tao/1.cpp b/2020-Autumn/Backend/task_02/Zeppel-Tao/1.cpp new file mode 100644 index 00000000..8db0ce8b --- /dev/null +++ b/2020-Autumn/Backend/task_02/Zeppel-Tao/1.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; +//第一道 +void text01(int m, int n,int* arrA, int* arrB) +{ + int i, j; + i = m - 1; + j = n - 1; + while (i >= 0 && j >= 0) + { + if (arrA[i] >= arrB[j]) //从后向前排序,将较大的值赋给A。 + { + arrA[i + j + 1] = arrA[i]; + i--; + } + else + { + arrA[i + j + 1] = arrB[j]; + j--; + } + } + while (j >= 0)//当B中有剩余的值时,将剩余的值赋到A中 + //若剩余的是A中的值,则直接在数组A中 + { + arrA[j] = arrB[j]; + } +} +int main() +{ + int arrA[7] = { 1, 2, 3, 8, 0, 0, 0 }; + int arrB[3] = { 2,5,6 }; + text01(4, 3, arrA,arrB); + for (int i = 0; i < 7; i++) + { + cout << arrA[i] << " "; + } + + system("pause"); + return 0; +} \ No newline at end of file diff --git a/2020-Autumn/Backend/task_02/Zeppel-Tao/2.cpp b/2020-Autumn/Backend/task_02/Zeppel-Tao/2.cpp new file mode 100644 index 00000000..f6345c0a --- /dev/null +++ b/2020-Autumn/Backend/task_02/Zeppel-Tao/2.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; +void text02(int** matrix, int n) +{ + + cout << "翻转前:" << endl; + for (int i = 0; i < n; i++) + + { + for (int j = 0; j < n; j++) + { + + matrix[i][j] = i * n + j + 1; + cout << matrix[i][j] << "\t"; + } + cout << endl; + } + for (int i = 0; i < n / 2; i++) + { + for (int j = 0; j < n; j++) + { + int temp = matrix[i][j]; + matrix[i][j] = matrix[n - i - 1][j]; + matrix[n - i - 1][j] = temp; + + } + } + //对角线翻转 + for (int i = 0; i < n; i++) + { + for (int j = 0; j < i; j++) + { + int temp = matrix[i][j]; + matrix[i][j] = matrix[j][i]; + matrix[j][i] = temp; + } + } + cout << "翻转后:" << endl; + for (int i = 0; i < n; i++) + + { + for (int j = 0; j < n; j++) + { + + cout << matrix[i][j] << "\t"; + } + cout << endl; + } +} +int main() +{ + int n = 0; + cout << "请输入n:"; + cin >> n; + int** matrix; + matrix = new int* [n]; + for (int i = 0; i < n; i++) + { + matrix[i] = new int[n]; + } + text02(matrix, n); + system("pause"); + return 0; +} \ No newline at end of file diff --git a/2020-Autumn/Backend/task_02/Zeppel-Tao/3.cpp b/2020-Autumn/Backend/task_02/Zeppel-Tao/3.cpp new file mode 100644 index 00000000..f1b93e67 --- /dev/null +++ b/2020-Autumn/Backend/task_02/Zeppel-Tao/3.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; +void text03(int* a, int n) +{ + int size = 10000; + int* arr = new int[size]; + int n = 0; + cin >> n; + for (int i = 0; i < n; i++) + { + arr[i] = i; + cout << arr[i] << " "; + } +} +int main() +{ + + system("pause"); + return 0; +} \ No newline at end of file diff --git a/2020-Autumn/Backend/task_02/Zeppel-Tao/4.cpp b/2020-Autumn/Backend/task_02/Zeppel-Tao/4.cpp new file mode 100644 index 00000000..d8cf45e1 --- /dev/null +++ b/2020-Autumn/Backend/task_02/Zeppel-Tao/4.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +int* text04(int n) +{ + cin >> n; + int* arr = new int[n]; + for (int i = 0; i < n; i++) + { + arr[i] = i + 1; + } + + for (int i = 0; i < n; i++) + { + cout << arr[i] << "\t"; + } + return arr; +} +int main() +{ + int n = 0; + text04(n); + system("pause"); + return 0; +} \ No newline at end of file diff --git a/2020-Autumn/Backend/task_02/Zeppel-Tao/5.cpp b/2020-Autumn/Backend/task_02/Zeppel-Tao/5.cpp new file mode 100644 index 00000000..f60ecc4b --- /dev/null +++ b/2020-Autumn/Backend/task_02/Zeppel-Tao/5.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; + +int** text05(int m, int n) +{ + int m = 0; + int n = 0; + cout << "请输入排数: "; + cin >> m; + cout << "请输入列数: "; + cin >> n; + int** arr = new int* [m]; + for (int i = 0; i < m; i++) + { + arr[i] = new int[n]; + } + for (int i = 0; i < m; i++) + + { + for (int j = 0; j < n; j++) + { + arr[i][j] = i * n + j; //若使用指针则为*(*(arr+i)+j) cin >> arr[i][j] + cout << arr[i][j] << "\t"; + } + cout << endl; + } + for (int i = 0; i < m; i++) + { + delete[] arr[i]; + } + delete[] arr; +} +int main() +{ + int m = 0; + int n = 0; + text05(m, n); + system("pause"); + return 0; +} \ No newline at end of file diff --git a/2020-Autumn/Backend/task_02/Zeppel-Tao/6.cpp b/2020-Autumn/Backend/task_02/Zeppel-Tao/6.cpp new file mode 100644 index 00000000..5260a84e --- /dev/null +++ b/2020-Autumn/Backend/task_02/Zeppel-Tao/6.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +int climbstairs(int n) +{ + if (n == 0) + { + return 0; + } + else if (n == 1) + { + return 1; + } + else if (n == 2) + { + return 2; + } + else { + return climbstairs(n - 1) + climbstairs(n - 2); //使用递归 + } +} +int main() +{ + int n = 0; + cin >> n; + climbstairs(n); + system("pause"); + return 0; +} \ No newline at end of file diff --git a/2020-Autumn/Backend/task_02/Zeppel-Tao/README.md b/2020-Autumn/Backend/task_02/Zeppel-Tao/README.md new file mode 100644 index 00000000..09ffb295 --- /dev/null +++ b/2020-Autumn/Backend/task_02/Zeppel-Tao/README.md @@ -0,0 +1,24 @@ +# 绗簩娆′换鍔℃荤粨 + +**1銆佸涔犲拰浣跨敤鎸囬拡涓庢暟缁** + +**2銆佸涔犱簡鏃堕棿澶嶆潅搴︿笌绌洪棿澶嶆潅搴︾殑姒傚康** + +**3銆佸涔犱簡鍔ㄦ佹暟缁勭殑姒傚康** + +**4銆佷簡瑙d簡閫掑綊鐨勬蹇** + +***** + +## 鍑虹幇鐨勯棶棰 + +**1銆佸紑濮嬫椂鏈紕鏄庣櫧鍔ㄦ佹暟缁勭殑姒傚康** + +**2銆佸閲嶆寚閽堢殑鍚箟** + +**3銆佷笉鐭ヨ鎬庢牱鍘讳紭鍖栫涓棰樼殑澶嶆潅搴﹂棶棰** + + + + + diff --git a/2020-Autumn/Backend/task_03/ZeppelI-Tao/1.cpp b/2020-Autumn/Backend/task_03/ZeppelI-Tao/1.cpp new file mode 100644 index 00000000..d8c40457 --- /dev/null +++ b/2020-Autumn/Backend/task_03/ZeppelI-Tao/1.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + +int findNumberInArray(int* arr, int length, int target, bool isleft) +{ + int position = 0; + int left = 0; + int right = length - 1; + + while (left <= right) + { + int mid = (left + right) / 2; + if (arr[mid] < target) + { + left = mid + 1; + } + else if (arr[mid] > target) + { + right = mid - 1; + } + else + { + position = mid; + if (isleft)//判断左右是否还有同样的数并取其位置 + { + right = mid - 1; + } + else + { + left = mid + 1; + } + } + } + return position; +} + +int main() +{ + int arr[] = { 1,2,3,4,4,4,4,5,6,7 } ; + int length = sizeof(arr) / sizeof(arr[0]); + int target = 0; + cout << "请输入要查找的数字:"; + cin >> target; + int low = findNumberInArray(arr, length, target, true); + int high = findNumberInArray(arr, length, target, false); + int sum = high - low + 1; + cout << "数字共有:" << sum << " 个"; + system("pause"); + return 0; +} \ No newline at end of file diff --git a/2020-Autumn/Backend/task_03/ZeppelI-Tao/2.cpp b/2020-Autumn/Backend/task_03/ZeppelI-Tao/2.cpp new file mode 100644 index 00000000..671e7158 --- /dev/null +++ b/2020-Autumn/Backend/task_03/ZeppelI-Tao/2.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; +//样例: +//输入:nums = [12, 345, 2, 6, 7896] +//输出:2 +//解释: +//因此只有 12 和 7896 是位数为偶数的数字 + +int findNumbers(int* nums, int length) +{ + int sum = 0; + int n = 0; + for (int i = 0; i < length; i++) + { + while (nums[i] > 0) + { + nums[i] /= 10; + n++; + } + if (n % 2 == 0) + { + sum++; + } + } + return sum; +} +int main() +{ + int nums[] = { 12, 345, 2, 6, 7896 }; + int length = sizeof(nums) / sizeof(nums[0]); + cout< +using namespace std; + +bool findNumberIn2DArray(int arr[][5], int n, int m, int target) +{ + int i = 0; + int j = m-1; + while (i <= n-1 && i >= 0 && j <= m-1 && j >= 0) + { + if (arr[i][j] > target) + { + j--; + } + else if (arr[i][j] < target) + { + i++; + } + else if (arr[i][j] == target) + { + return true; + } + return false; + } +} + +int main() +{ + const int m = 5; + const int n = 5; + int target = 0; + cout << "请输入目标数字: "; + cin >> target; + int arr[n][m] = + { + {1, 4, 7, 11, 15}, + {2, 5, 8, 12, 19}, + {3, 6, 9, 16, 22}, + {10, 13, 14, 17, 24}, + {18, 21, 23, 26, 30} + }; + bool ret = findNumberIn2DArray(arr, n, m,target); + if (ret) + { + cout << "true" << endl; + } + else + { + cout << "false" << endl; + } + system("pause"); + return 0; +} +//[ +// [1, 4, 7, 11, 15], +// [2, 5, 8, 12, 19], +// [3, 6, 9, 16, 22], +// [10, 13, 14, 17, 24], +// [18, 21, 23, 26, 30] +//] +//给定 target = 5,返回 true。 +//给定 target = 20,返回 false。 \ No newline at end of file diff --git a/2020-Autumn/Backend/task_03/ZeppelI-Tao/4.cpp b/2020-Autumn/Backend/task_03/ZeppelI-Tao/4.cpp new file mode 100644 index 00000000..4d510cba --- /dev/null +++ b/2020-Autumn/Backend/task_03/ZeppelI-Tao/4.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; +void moveZeroes(int* nums, int length) +{ + int k = length - 1; + int sum0 = 0; + for (int i = 0; i < length-1 ; i++) + { + if (nums[i] == 0) + { + for (int j = i; j < length - i - 1; j++) + { + nums[j] = nums[j + 1]; + } + sum0++; + } + + } + for (int j = 0;j < sum0; j++) + { + nums[k] = 0; + k--; + } +} +int main() +{ + int* arr = new int[5]{ 0,1,0,3,12 }; + moveZeroes(arr,5); + for (int i = 0; i <5 ; i++) + { + cout << arr[i] << endl; + } + system("pause"); + return 0; +} +//void moveZeroes(int* nums, int length){ +//code +//} +//输入: nums = [0, 1, 0, 3, 12], length = 5 + +//输出 : [1, 3, 12, 0, 0] diff --git a/2020-Autumn/Backend/task_03/ZeppelI-Tao/5.cpp b/2020-Autumn/Backend/task_03/ZeppelI-Tao/5.cpp new file mode 100644 index 00000000..3895b32a --- /dev/null +++ b/2020-Autumn/Backend/task_03/ZeppelI-Tao/5.cpp @@ -0,0 +1,60 @@ +#include +#include +using namespace std; + + +int countGoodTriplets(int* arr, int length, int a, int b, int c) +{ + int sum = 0; + for (int i = 0; i < length - 2; i++) + { + for (int j = i+1; j < length - 1; j++) + { + for (int k = j + 1; k < length; k++) + { + if ((i + j + k ) > length) + { + continue; + } + else + { + int tempa = arr[i] - arr[j]; + int tempb = arr[j] - arr[k]; + int tempc = arr[i] - arr[k]; + if ((abs(arr[i] - arr[j]) < a || abs(arr[i] - arr[j]) == a) && (abs(arr[j] - arr[k]) < b || abs(arr[j] - arr[k]))&&(abs(arr[i] - arr[k])> a >> b >> c; + cout << countGoodTriplets(arr, length, a, b, c); + system("pause"); + return 0; +} +//如果三元组(arr[i], arr[j], arr[k]) 满足下列全部条件,则认为它是一个 好三元组 。 +//0 <= i < j < k < arr.length +// | arr[i] - arr[j]| <= a +// | arr[j] - arr[k]| <= b +// | arr[i] - arr[k]| <= c +// 其中 | x | 表示 x 的绝对值。 +// +// 样例: +// 输入:arr = [3, 0, 1, 1, 9, 7], a = 7, b = 2, c = 3 +// 输出:4 +// 解释:一共有 4 个好三元组:[(3, 0, 1), (3, 0, 1), (3, 1, 1), (0, 1, 1)] 。 +// +// 参考提交函数格式 +// int countGoodTriplets(int* arr, int length, int a, int* arr, int lengthb, int c) { +// //code +//} \ No newline at end of file diff --git a/2020-Autumn/Backend/task_03/ZeppelI-Tao/6.cpp b/2020-Autumn/Backend/task_03/ZeppelI-Tao/6.cpp new file mode 100644 index 00000000..ae19362a --- /dev/null +++ b/2020-Autumn/Backend/task_03/ZeppelI-Tao/6.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; +//使用快速排序的方法进行排序,时间复杂度不高于n^2 +void quickSort(int arr[], int left, int right) +{ + if (left >= right) //判断是否成立,若不成立则结束 + { + return; + } + int i, j, k; + i = left; + int base = arr[i]; //设置基准数,大于基准数的值放在其右侧 + j = right; //小于基准书的值放在其左侧 + while (i < j) + { //当设置基准数位于左边时,从数组的右侧开始搜索 + while (arr[j] >= base && i < j) + { + j--; //从右边开始遇到大于其的值则跳过,小于其的值便保留 + } + while (arr[i] <= base && i < j) + { + i++; + } + if (i < j) //将左右保留的值互换,使得基准数左右均为小于||大于其的值 + { + int temp = arr[j]; + arr[j] = arr[i]; + arr[i] = temp; + } + } + arr[left] = arr[i]; //基准数归位 + arr[i] = base; + quickSort(arr, left, i - 1); //递归左边 + quickSort(arr, i + 1, right); //递归右边 +} +int main() +{ + int arr[10] = { 3,15,8,6,22,48,1,96,71,0 }; + quickSort(arr, 0, 9); + for (int i = 0; i < 10; i++) + { + cout << arr[i] << endl; + } + + system("pause"); + return 0; +} \ No newline at end of file diff --git a/2020-Autumn/Backend/task_03/ZeppelI-Tao/7.md b/2020-Autumn/Backend/task_03/ZeppelI-Tao/7.md new file mode 100644 index 00000000..6e9ba91f --- /dev/null +++ b/2020-Autumn/Backend/task_03/ZeppelI-Tao/7.md @@ -0,0 +1,3 @@ +# 1000鐡舵按锛屾湁涓鐡舵槸姣掕嵂锛屼綘鏈10鍙皬鐧介紶锛屽枬浜嗘瘨鑽竴涓ぜ鎷滃悗姣掑彂锛屾庢牱鍦ㄤ竴鍛ㄥ唴鐭ラ亾鍝摱姘存湁姣掋 + +閫氳繃灏嗕竴鍗冪摱姘存瘡鐡舵爣鍙凤紝浣跨敤**浜岃繘鍒**鐨勬柟寮忓皢鍏惰〃绀猴紝灏嗘瘡涓簩杩涘埗浣嶆暟涓1鐨勬贩鍚堝杺缁欏皬鐧介紶锛屾渶鍚庤娴嬫墍鏈夊皬鐧介紶鐨勭姸鍐碉紝鑻ユ浜★紝鍒欒灏忕櫧榧犵殑浣嶆暟涓婄殑鍊间负1锛屽弽涔嬩负0锛屾渶缁堢粨鍚堝崄鍙皬鐧介紶鐨勬儏鍐碉紝寰楃煡鏈夋瘨鐨勩 \ No newline at end of file diff --git a/2020-Autumn/Backend/task_03/ZeppelI-Tao/Readme.md b/2020-Autumn/Backend/task_03/ZeppelI-Tao/Readme.md new file mode 100644 index 00000000..487bdcde --- /dev/null +++ b/2020-Autumn/Backend/task_03/ZeppelI-Tao/Readme.md @@ -0,0 +1,5 @@ +# 绗笁娆′换鍔℃荤粨 + +* 绗竷棰樻櫤鍔涢鏄湪缃戜笂鏌ユ壘鐨勭瓟妗 +* 鏃犳硶鎯冲埌鏇村ソ鐨勬柟娉曞幓闄嶄綆鏃堕棿澶嶆潅搴 +* 绗叓棰樺彧鑳芥兂鍑轰娇鐢ㄥ啋娉℃帓搴忚鏃堕棿澶嶆潅搴︿负n^2鎺掑簭鍚庡啀璁╂瘡涓悗涓椤瑰噺鍓嶄竴椤瑰緱鍒板艰祴鍒癿ax涓紝鐒跺悗浣跨敤if璇彞鍒ゆ柇澶у皬銆 \ No newline at end of file