From 8615933ca3a5c5e8dbb5e4200646a990119234a6 Mon Sep 17 00:00:00 2001
From: qq_44193969 <2608882093@qq.com>
Date: Mon, 27 Dec 2021 17:46:02 +0800
Subject: [PATCH] optimize 15 exercises
---
.../1.cpp/11.exercises/solution.md" | 113 ++++++---
.../1.cpp/12.exercises/solution.md" | 43 +++-
.../1.cpp/13.exercises/solution.md" | 127 +++++++---
.../1.cpp/14.exercises/solution.md" | 40 +++-
.../1.cpp/15.exercises/solution.md" | 97 ++++++--
.../1.cpp/16.exercises/solution.md" | 115 ++++++---
.../1.cpp/17.exercises/solution.md" | 137 ++++++++---
.../1.cpp/18.exercises/solution.md" | 95 +++++---
.../1.cpp/19.exercises/solution.md" | 119 ++++++---
.../1.cpp/20.exercises/solution.md" | 111 ++++++---
.../1.cpp/21.exercises/solution.md" | 226 ++++++++++++------
.../1.cpp/22.exercises/solution.md" | 122 +++++++++-
.../1.cpp/23.exercises/solution.md" | 67 +++++-
.../1.cpp/24.exercises/solution.md" | 67 +++++-
.../1.cpp/25.exercises/solution.md" | 56 ++++-
15 files changed, 1194 insertions(+), 341 deletions(-)
diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/11.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/11.exercises/solution.md"
index 3cf878a9e..f73de102f 100644
--- "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/11.exercises/solution.md"
+++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/11.exercises/solution.md"
@@ -26,43 +26,86 @@
2
```
+以下程序实现了这一功能,请你填补空白处的内容:
+
+```cpp
+#include Ø 问题描述:已知一只家禽得了流感,流感的传播时间为 24 小时,在这 24 小时内最多能将流感传给其它 M 只家禽,农场主需要购买紧急药品,假设发现时已经过了 N 天,那么农场主需要至少买多少包药呢?(一包药为一只家禽用量) Ø 输入:一行,两个整数,第一个表示流感传染家禽的数量 M,第二个表示发现时已过的天数。 Ø 输出:一行,一个整数,表示需要药的包数。 Ø 样例输入: 10 2 Ø 样例输出: 121
利用公式x1 = (-b + sqrt(b*b-4*a*c))/(2*a), x2 = (-b - sqrt(b*b-4*a*c))/(2*a)求一元二次方程ax2 + bx + c =0 的根,其中a不等于0。 -输入一行,包含三个浮点数a, b, c(它们之间以一个空格分开),分别表示方程ax2 + bx + c =0 的系数。输出一行,表示方程的解。 -若两个实根相等,则输出形式为:x1=x2=...。 -若两个实根不等,则输出形式为:x1=...;x2 = ...,其中x1若是两个虚根,则输出:x1=实部+虚部i; x2=实部-虚部i,其中x1,x2满足以下两个条件中的一个: -1. x1的实部大于x2的实部 -2. x1的实部等于x2的实部且x1的虚部大于等于x2的虚部 -所有实数部分要求精确到小数点后5位,数字、符号之间没有空格。 -样例输入:1.0 2.0 8.0 +利用公式x1 = (-b + sqrt(b*b-4*a*c))/(2*a), x2 = (-b - sqrt(b*b-4*a*c))/(2*a)求一元二次方程ax2 + bx + c =0 的根,其中a不等于0。+以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include
+输入一行,包含三个浮点数a, b, c(它们之间以一个空格分开),分别表示方程ax2 + bx + c =0 的系数。输出一行,表示方程的解。
+若两个实根相等,则输出形式为:x1=x2=...。
+若两个实根不等,则输出形式为:x1=...;x2 = ...,其中x1若是两个虚根,则输出:x1=实部+虚部i; x2=实部-虚部i,其中x1,x2满足以下两个条件中的一个:
+1. x1的实部大于x2的实部
+2. x1的实部等于x2的实部且x1的虚部大于等于x2的虚部
+所有实数部分要求精确到小数点后5位,数字、符号之间没有空格。
+样例输入:1.0 2.0 8.0
样例输出:x1=-1.00000+2.64575i;x2=-1.00000-2.64575i+#include +#include +#include +using namespace std; +static const double e = 1e-12; +_______________________________ +int main() +{ + complex a, b, c; + complex x1, x2; + cin >> a >> b >> c; + x1 = (-b + sqrt(b * b - a * c * 4.0)) / (a * 2.0); + x2 = (-b - sqrt(b * b - a * c * 4.0)) / (a * 2.0); + cout << setiosflags(ios::fixed); + cout.precision(6); + if (abs(x1.imag()) < e) + { + if (x1 == x2) + { + cout << "x1=x2=" << x1.real(); + } + else + { + cout << "x1=" << x1.real() << ";x2=" << x1.real(); + } + } + else + { + cout << "x1=" << x1.real() << "+" << x1.imag() << "i;" + << "x2=" << x2.real() << "+" << x2.imag() << "i"; + } + return 0; +} +``` + ## template ```cpp #include -#include +#include #include #include using namespace std; static const double e = 1e-12; -bool operator == (complex c1, complex c2) { return abs(c1-c2) < e;} +bool operator==(complex c1, complex c2) { return abs(c1 - c2) < e; } int main() { - complex a,b,c; - complex x1,x2; - cin >> a >> b >> c; - x1 = (-b + sqrt(b*b-a*c*4.0))/(a*2.0); - x2 = (-b - sqrt(b*b-a*c*4.0))/(a*2.0); - cout << setiosflags(ios::fixed); - cout.precision(6); - if ( abs(x1.imag()) < e ) - { - if (x1 == x2) { - cout << "x1=x2=" << x1.real(); - } else { - cout << "x1=" << x1.real() <<";x2=" << x1.real(); - } - } - else { - cout << "x1=" << x1.real()<<"+"< a, b, c; + complex x1, x2; + cin >> a >> b >> c; + x1 = (-b + sqrt(b * b - a * c * 4.0)) / (a * 2.0); + x2 = (-b - sqrt(b * b - a * c * 4.0)) / (a * 2.0); + cout << setiosflags(ios::fixed); + cout.precision(6); + if (abs(x1.imag()) < e) + { + if (x1 == x2) + { + cout << "x1=x2=" << x1.real(); + } + else + { + cout << "x1=" << x1.real() << ";x2=" << x1.real(); + } + } + else + { + cout << "x1=" << x1.real() << "+" << x1.imag() << "i;" + << "x2=" << x2.real() << "+" << x2.imag() << "i"; + } + return 0; } ``` ## 答案 ```cpp - +bool operator==(complex c1, complex c2) { return abs(c1 - c2) < e; } ``` ## 选项 @@ -56,17 +99,17 @@ int main() ### A ```cpp - +; ``` ### B ```cpp - +bool operator==(complex c1, complex c2) { return abs(c1 - c2) > e; } ``` ### C ```cpp - +bool operator==(complex c1, complex c2) { return abs(c1 - c2) >= e; } ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/21.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/21.exercises/solution.md" index 679ebb869..178095aa9 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/21.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/21.exercises/solution.md" @@ -52,6 +52,87 @@ +以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include +using namespace std; +class Solution +{ +public: + void solveSudoku(vector > &board) + { + int size = board.size(); + vector > rows(size, vector (10)); + vector > cols(size, vector (10)); + vector > boxes(size, vector (10)); + for (int i = 0; i < size; i++) + { + for (int j = 0; j < size; j++) + { + if (board[i][j] != '.') + { + int num = board[i][j] - '0'; + int idx = i / 3 * 3 + j / 3; + rows[i][num] = true; + cols[j][num] = true; + boxes[idx][num] = true; + } + } + } + dfs(board, 0, rows, cols, boxes); + } + +private: + bool valid(int num, int row, int col, int idx, vector > &rows, + vector > &cols, vector > &boxes) + { + return !rows[row][num] && !cols[col][num] && !boxes[idx][num]; + } + bool dfs(vector > &board, int size, vector > &rows, + vector > &cols, vector > &boxes) + { + if (size == 9 * 9) + { + return true; + } + else + { + bool ok = false; + int row = size / 9; + int col = size % 9; + int idx = row / 3 * 3 + col / 3; + if (board[row][col] == '.') + { + for (int i = 1; i <= 9; i++) + { + if (valid(i, row, col, idx, rows, cols, boxes)) + { + board[row][col] = i + '0'; + rows[row][i] = true; + cols[col][i] = true; + boxes[idx][i] = true; + _______________________________ + if (!ok) + { + rows[row][i] = false; + cols[col][i] = false; + boxes[idx][i] = false; + board[row][col] = '.'; + } + } + } + } + else + { + ok = dfs(board, size + 1, rows, cols, boxes); + } + return ok; + } + } +}; +``` + ## template ```cpp @@ -60,82 +141,83 @@ using namespace std; class Solution { public: - void solveSudoku(vector > &board) - { - int size = board.size(); - vector > rows(size, vector (10)); - vector > cols(size, vector (10)); - vector > boxes(size, vector (10)); - for (int i = 0; i < size; i++) - { - for (int j = 0; j < size; j++) - { - if (board[i][j] != '.') - { - int num = board[i][j] - '0'; - int idx = i / 3 * 3 + j / 3; - rows[i][num] = true; - cols[j][num] = true; - boxes[idx][num] = true; - } - } - } - dfs(board, 0, rows, cols, boxes); - } + void solveSudoku(vector > &board) + { + int size = board.size(); + vector > rows(size, vector (10)); + vector > cols(size, vector (10)); + vector > boxes(size, vector (10)); + for (int i = 0; i < size; i++) + { + for (int j = 0; j < size; j++) + { + if (board[i][j] != '.') + { + int num = board[i][j] - '0'; + int idx = i / 3 * 3 + j / 3; + rows[i][num] = true; + cols[j][num] = true; + boxes[idx][num] = true; + } + } + } + dfs(board, 0, rows, cols, boxes); + } + private: - bool valid(int num, int row, int col, int idx, vector > &rows, - vector > &cols, vector > &boxes) - { - return !rows[row][num] && !cols[col][num] && !boxes[idx][num]; - } - bool dfs(vector > &board, int size, vector > &rows, - vector > &cols, vector > &boxes) - { - if (size == 9 * 9) - { - return true; - } - else - { - bool ok = false; - int row = size / 9; - int col = size % 9; - int idx = row / 3 * 3 + col / 3; - if (board[row][col] == '.') - { - for (int i = 1; i <= 9; i++) - { - if (valid(i, row, col, idx, rows, cols, boxes)) - { - board[row][col] = i + '0'; - rows[row][i] = true; - cols[col][i] = true; - boxes[idx][i] = true; - ok = dfs(board, size + 1, rows, cols, boxes); - if (!ok) - { - rows[row][i] = false; - cols[col][i] = false; - boxes[idx][i] = false; - board[row][col] = '.'; - } - } - } - } - else - { - ok = dfs(board, size + 1, rows, cols, boxes); - } - return ok; - } - } + bool valid(int num, int row, int col, int idx, vector > &rows, + vector > &cols, vector > &boxes) + { + return !rows[row][num] && !cols[col][num] && !boxes[idx][num]; + } + bool dfs(vector > &board, int size, vector > &rows, + vector > &cols, vector > &boxes) + { + if (size == 9 * 9) + { + return true; + } + else + { + bool ok = false; + int row = size / 9; + int col = size % 9; + int idx = row / 3 * 3 + col / 3; + if (board[row][col] == '.') + { + for (int i = 1; i <= 9; i++) + { + if (valid(i, row, col, idx, rows, cols, boxes)) + { + board[row][col] = i + '0'; + rows[row][i] = true; + cols[col][i] = true; + boxes[idx][i] = true; + ok = dfs(board, size + 1, rows, cols, boxes); + if (!ok) + { + rows[row][i] = false; + cols[col][i] = false; + boxes[idx][i] = false; + board[row][col] = '.'; + } + } + } + } + else + { + ok = dfs(board, size + 1, rows, cols, boxes); + } + return ok; + } + } }; ``` ## 答案 ```cpp - +ok = dfs(board, size + 1, rows, cols, boxes); ``` ## 选项 @@ -143,17 +225,17 @@ private: ### A ```cpp - +ok = dfs(board, size - 1, rows, cols, boxes); ``` ### B ```cpp - +ok = dfs(board, size, rows, cols, boxes); ``` ### C ```cpp - +ok = dfs(board, size, rows + 1, cols - 1, boxes); ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/22.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/22.exercises/solution.md" index b087ab5e0..6b11b05e6 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/22.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/22.exercises/solution.md" @@ -2,6 +2,28 @@ 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
示例 1:
输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。示例 2:
输入:height = [4,2,0,3,2,5]
输出:9
提示:
+以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include
n == height.length
0 <= n <= 3 * 104
0 <= height[i] <= 105
+using namespace std; +class Solution +{ +public: + int trap(vector &height) + { + int res = 0; + int left = 0, left_max = 0; + int right = height.size() - 1, right_max = 0; + while (left < right) + { + _____________________ + } + return res; + } +}; +``` + ## template ```cpp @@ -50,7 +72,30 @@ public: ## 答案 ```cpp - +if (height[left] < height[right]) +{ + if (height[left] > left_max) + { + left_max = height[left]; + } + else + { + res += left_max - height[left]; + } + left++; +} +else +{ + if (height[right] > right_max) + { + right_max = height[right]; + } + else + { + res += right_max - height[right]; + } + right--; +} ``` ## 选项 @@ -58,17 +103,86 @@ public: ### A ```cpp - +if (height[left] < height[right]) +{ + if (height[left] > left_max) + { + left_max = height[left]; + } + else + { + res += left_max - height[left]; + } + left--; +} +else +{ + if (height[right] > right_max) + { + right_max = height[right]; + } + else + { + res += right_max - height[right]; + } + right++; +} ``` ### B ```cpp - +if (height[left] < height[right]) +{ + if (height[left] > left_max) + { + left_max = height[left]; + } + else + { + res += left_max + height[left]; + } + left++; +} +else +{ + if (height[right] > right_max) + { + right_max = height[right]; + } + else + { + res += right_max + height[right]; + } + right--; +} ``` ### C ```cpp - +if (height[left] < height[right]) +{ + if (height[left] > left_max) + { + left_max = height[left]; + } + else + { + res += left_max + height[left]; + } + left--; +} +else +{ + if (height[right] > right_max) + { + right_max = height[right]; + } + else + { + res += right_max + height[right]; + } + right++; +} ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/23.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/23.exercises/solution.md" index b9b512a30..9074caf70 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/23.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/23.exercises/solution.md" @@ -2,6 +2,49 @@ 给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
求在该柱状图中,能够勾勒出来的矩形的最大面积。
以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为
[2,1,5,6,2,3]
。
图中阴影部分为所能勾勒出的最大矩形面积,其面积为
10
个单位。
示例:
输入: [2,1,5,6,2,3]+以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include
输出: 10+#include +static int largestRectangleArea(int *heights, int heightsSize) +{ + int *indexes = malloc(heightsSize * sizeof(int)); + int *left = malloc(heightsSize * sizeof(int)); + int *right = malloc(heightsSize * sizeof(int)); + int i, pos = 0; + for (i = 0; i < heightsSize; i++) + { + while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) + { + pos--; + } + left[i] = pos == 0 ? -1 : indexes[pos - 1]; + indexes[pos++] = i; + } + pos = 0; + for (i = heightsSize - 1; i >= 0; i--) + { + while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) + { + pos--; + } + right[i] = pos == 0 ? heightsSize : indexes[pos - 1]; + indexes[pos++] = i; + } + int max_area = 0; + _______________________ + return max_area; +} +int main(void) +{ + int nums[] = {2, 1, 5, 6, 2, 3}; + int count = sizeof(nums) / sizeof(*nums); + printf("%d\n", largestRectangleArea(nums, count)); + return 0; +} +``` + ## template ```cpp @@ -52,7 +95,11 @@ int main(void) ## 答案 ```cpp - +for (i = 0; i < heightsSize; i++) +{ + int area = heights[i] * (right[i] - left[i] - 1); + max_area = area > max_area ? area : max_area; +} ``` ## 选项 @@ -60,17 +107,29 @@ int main(void) ### A ```cpp - +for (i = 0; i < heightsSize; i++) +{ + int area = heights[i] * (right[i] - left[i] - 1); + max_area = area > max_area ? max_area : area; +} ``` ### B ```cpp - +for (i = 0; i < heightsSize; i++) +{ + int area = heights[i] * (right[i] - left[i]); + max_area = area > max_area ? max_area : area; +} ``` ### C ```cpp - +for (i = 0; i < heightsSize; i++) +{ + int area = heights[i] * (right[i] - left[i]); + max_area = area > max_area ? area : max_area; +} ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/24.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/24.exercises/solution.md" index 869babd08..4710d206b 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/24.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/24.exercises/solution.md" @@ -2,6 +2,37 @@ 给你一个未排序的整数数组
nums
,请你找出其中没有出现的最小的正整数。
进阶:你可以实现时间复杂度为
O(n)
并且只使用常数级别额外空间的解决方案吗?
示例 1:
输入:nums = [1,2,0]
输出:3示例 2:
输入:nums = [3,4,-1,1]
输出:2示例 3:
输入:nums = [7,8,9,11,12]
输出:1
提示:
+以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include
0 <= nums.length <= 300
-231 <= nums[i] <= 231 - 1
+using namespace std; +class Solution +{ +public: + int firstMissingPositive(vector &nums) + { + if (nums.size() == 0) + { + return 1; + } + int i = 0; + while (i < nums.size()) + { + ____________________________ + } + for (i = 0; i < nums.size(); i++) + { + if (nums[i] != i + 1) + { + break; + } + } + return i + 1; + } +}; +``` + ## template ```cpp @@ -43,7 +74,14 @@ public: ## 答案 ```cpp - +if (nums[i] > 0 && nums[i] != i + 1 && nums[i] - 1 < nums.size() && nums[nums[i] - 1] != nums[i]) +{ + swap(nums[i], nums[nums[i] - 1]); +} +else +{ + i++; +} ``` ## 选项 @@ -51,17 +89,38 @@ public: ### A ```cpp - +if (nums[i] > 0 && nums[i] != i + 1 && nums[i] - 1 < nums.size() && nums[nums[i] - 1] != nums[i]) +{ + swap(nums[i], nums[nums[i] + 1]); +} +else +{ + i++; +} ``` ### B ```cpp - +if (nums[i] != i + 1 && nums[nums[i] - 1] != nums[i]) +{ + swap(nums[i], nums[nums[i] - 1]); +} +else +{ + i++; +} ``` ### C ```cpp - +if (nums[i] != i + 1 && nums[nums[i] - 1] != nums[i]) +{ + swap(nums[i], nums[nums[i] + 1]); +} +else +{ + i++; +} ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/25.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/25.exercises/solution.md" index 4fbd3061d..df412fa0f 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/25.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/25.exercises/solution.md" @@ -35,6 +35,54 @@ +以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include +using namespace std; +class Solution +{ +public: + int totalNQueens(int n) + { + vector stack(n); + return dfs(n, 0, stack); + } +private: + int dfs(int n, int row, vector &stack) + { + int count = 0; + if (row == n) + { + return count + 1; + } + else + { + for (int i = 0; i < n; i++) + { + if (row == 0 || !conflict(stack, row, i)) + { + stack[row] = i; + _______________________ + } + } + return count; + } + } + bool conflict(vector &stack, int row, int col) + { + for (int i = 0; i < row; i++) + { + if (col == stack[i] || abs(row - i) == abs(col - stack[i])) + { + return true; + } + } + return false; + } +} +``` + ## template ```cpp @@ -86,7 +134,7 @@ private: ## 答案 ```cpp - +count += dfs(n, row + 1, stack); ``` ## 选项 @@ -94,17 +142,17 @@ private: ### A ```cpp - +count += dfs(n, row - 1, stack); ``` ### B ```cpp - +count += dfs(n, row, stack); ``` ### C ```cpp - +count += dfs(n + 1, row, stack); ``` \ No newline at end of file -- GitLab