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 +using namespace std; +int main() +{ + int n = 0; + cin >> n; + int *ptr = new (nothrow) int[n]; + for (auto i = 0; i < n; i++) + { + cin >> ptr[i]; + } + int x = 0; + cin >> x; + auto j = 0; + auto status = 0; + for (; j < n; ++j) + { + ______________ + } + if (status == 0) + { + j = -1; + } + cout << j << endl; + delete[] ptr; + cin.get(); + cin.get(); + return 0; +} +``` + ## template ```cpp #include using namespace std; -int main() { - int n = 0; - cin >> n; - int* ptr = new(nothrow) int[n]; - for (auto i = 0; i < n; i++) { - cin >> ptr[i]; - } - int x = 0; - cin >> x; - auto j = 0; - auto status = 0; - for (; j < n; ++j) { - if (ptr[j] == x) { - status = 1; - break; - } - } - if (status == 0) { - j = -1; - } - cout << j << endl; - delete[] ptr; - cin.get(); - cin.get(); - return 0; +int main() +{ + int n = 0; + cin >> n; + int *ptr = new (nothrow) int[n]; + for (auto i = 0; i < n; i++) + { + cin >> ptr[i]; + } + int x = 0; + cin >> x; + auto j = 0; + auto status = 0; + for (; j < n; ++j) + { + if (ptr[j] == x) + { + status = 1; + break; + } + } + if (status == 0) + { + j = -1; + } + cout << j << endl; + delete[] ptr; + cin.get(); + cin.get(); + return 0; } ``` ## 答案 ```cpp - +if (ptr[j] == x) +{ + status = 1; + break; +} ``` ## 选项 @@ -70,17 +113,29 @@ int main() { ### A ```cpp - +if (ptr[j] == x) +{ + status = 1; + continue; +} ``` ### B ```cpp - +if (ptr[j] >= x) +{ + status = 1; + continue; +} ``` ### C ```cpp - +if (ptr[j] <= x) +{ + status = 1; + continue; +} ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/12.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/12.exercises/solution.md" index 8e582d678..44ad81b10 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/12.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/12.exercises/solution.md" @@ -2,6 +2,25 @@

Ø 问题描述:已知一只家禽得了流感,流感的传播时间为 24 小时,在这 24 小时内最多能将流感传给其它 M 只家禽,农场主需要购买紧急药品,假设发现时已经过了 N 天,那么农场主需要至少买多少包药呢?(一包药为一只家禽用量)

Ø 输入:一行,两个整数,第一个表示流感传染家禽的数量 M,第二个表示发现时已过的天数。

Ø 输出:一行,一个整数,表示需要药的包数。

Ø 样例输入:

10 2

Ø 样例输出:

121

+以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include +using namespace std; +int total(int x); +int m; +int main() { + int x; + cin >> m >> x; + int to = total(x); + cout << to; + return 0; +} +int total(int x) { + _________________ +} +``` + ## template ```cpp @@ -28,7 +47,11 @@ int total(int x) { ## 答案 ```cpp - +if (x > 0) +{ + return (m + 1) * total(x - 1); +} +else return 1; ``` ## 选项 @@ -36,17 +59,29 @@ int total(int x) { ### A ```cpp - +if (x < 0) +{ + return (m + 1) * total(x - 1); +} +else return 1; ``` ### B ```cpp - +if (x > 0) +{ + return (m + 1) * total(x + 1); +} +else return 1; ``` ### C ```cpp - +if (x < 0) +{ + return (m + 1) * total(x + 1); +} +else return 1; ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/13.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/13.exercises/solution.md" index 1d0b2be88..f68970779 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/13.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/13.exercises/solution.md" @@ -9,6 +9,45 @@ 样例输出 Positive elegance

+以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include +#include +int main() +{ + char a[100] = {0}; + int i; + int zyy = 1; + int fyy = 1; + printf("请输入字符串:"); + gets(a); + for (i = 1; i < strlen(a); i++) + { + if (a[i] < a[i - 1]) + { + zyy = 0; + break; + } + } + for (i = 1; i < strlen(a); i++) + { + ________________ + } + if (zyy && !fyy) + { + printf("Positive elegance\n"); + } + else if (!zyy && fyy) + { + printf("Negative elegance\n"); + } + else + printf("Non elegance\n"); + return 0; +} +``` + ## template ```cpp @@ -16,44 +55,50 @@ Positive elegance

#include int main() { - char a[100] = {0}; - int i; - int zyy = 1; - int fyy = 1; - printf("请输入字符串:"); - gets(a); - for (i=1;i a[i-1]) - { - fyy = 0; - break; - } - } - if (zyy && !fyy) - { - printf("Positive elegance\n"); - }else if (!zyy && fyy) - { - printf("Negative elegance\n"); - }else - printf("Non elegance\n"); - return 0; + char a[100] = {0}; + int i; + int zyy = 1; + int fyy = 1; + printf("请输入字符串:"); + gets(a); + for (i = 1; i < strlen(a); i++) + { + if (a[i] < a[i - 1]) + { + zyy = 0; + break; + } + } + for (i = 1; i < strlen(a); i++) + { + if (a[i] > a[i - 1]) + { + fyy = 0; + break; + } + } + if (zyy && !fyy) + { + printf("Positive elegance\n"); + } + else if (!zyy && fyy) + { + printf("Negative elegance\n"); + } + else + printf("Non elegance\n"); + return 0; } ``` ## 答案 ```cpp - +if (a[i] > a[i - 1]) +{ + fyy = 0; + break; +} ``` ## 选项 @@ -61,17 +106,29 @@ int main() ### A ```cpp - +if (a[i] < a[i - 1]) +{ + fyy = 0; + break; +} ``` ### B ```cpp - +if (a[i] <= a[i - 1]) +{ + fyy = 0; + break; +} ``` ### C ```cpp - +if (a[i] > a[i - 1]) +{ + fyy = 0; + continue; +} ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/14.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/14.exercises/solution.md" index ef79a3446..beaa91bd3 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/14.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/14.exercises/solution.md" @@ -20,6 +20,34 @@ 输出格式   对于每一个询问,输出一行包含"Case #x: y",x是询问编号(从1开始标号),y是你每天最少需要的盒子数量。

+ +以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include +#include +using namespace std; +int main() +{ + int n, k, count = 1; + long long c; + cin >> n; + while (n--) + { + cin >> k >> c; + int num = k; + long long sum = k; + for (long long i = 2; i <= c; i = sum / k + 1) + { + int t = num; + ________________ + } + cout << "Case #" << count++ <<": " << num << endl; + } + return 0; +} +``` + ## template ```cpp @@ -51,7 +79,8 @@ int main() ## 答案 ```cpp - +num += ceil((k * i - sum) * 1.0 / i); +sum += i * (num - t); ``` ## 选项 @@ -59,17 +88,20 @@ int main() ### A ```cpp - +num += ceil((k * i - sum) * 1.0 / i); +sum += i * (num + t); ``` ### B ```cpp - +num += ceil((k - sum) * 1.0 / i); +sum += i * (num - t); ``` ### C ```cpp - +num += ceil((k * (i - sum)) * 1.0 / i); +sum += i * (num + t); ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/15.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/15.exercises/solution.md" index 1225e48a1..4f0370308 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/15.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/15.exercises/solution.md" @@ -1,9 +1,43 @@ # 寻找孪生素数 数学家希尔伯特在1900年国际数学家大会的报告上提出一个“孪生素数猜想”,即: 存在无穷多个素数p,使得p + 2是素数。p和p+2这一对差为2的素数,被称为“孪生素数”。 + 看起来,这个猜想是成立的,我们总能找到很多对孪生素数,例如:3和5,5和7,11和13…… 这一猜想至今还未被证明。 + 现在,对于给定的整数n, 请寻找大于n的最小的一对孪生素数p和q(q=p+2)。 +以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include +#include +using namespace std; +int sushu(int x) +{ + if (x <= 1) + return 0; + int i, j = 1; + for (i = 2; i <= sqrt(x); i++) + { + _______________ + } + return j; +} +int main() +{ + int x; + cin >> x; + int i = x + 1; + for (;; i++) + { + if (sushu(i) && sushu(i + 2)) + break; + } + cout << i << ' ' << i + 2; + return 0; +} +``` + ## template ```cpp @@ -12,33 +46,42 @@ using namespace std; int sushu(int x) { - if (x <= 1) return 0; - int i,j=1; - for(i=2;i<=sqrt(x);i++) - { - if(x%i==0){j=0;break;} - } - return j; + if (x <= 1) + return 0; + int i, j = 1; + for (i = 2; i <= sqrt(x); i++) + { + if (x % i == 0) + { + j = 0; + break; + } + } + return j; } int main() { - int x; - cin>>x; - int i=x+1; - for(;;i++) - { - if (sushu(i)&&sushu(i+2)) - break; - } - cout<> x; + int i = x + 1; + for (;; i++) + { + if (sushu(i) && sushu(i + 2)) + break; + } + cout << i << ' ' << i + 2; + return 0; } ``` ## 答案 ```cpp - +if (x % i == 0) +{ + j = 0; + break; +} ``` ## 选项 @@ -46,17 +89,29 @@ int main() ### A ```cpp - +if (x % i == 0) +{ + j = 0; + continue; +} ``` ### B ```cpp - +if (x % i == 1) +{ + j = 0; + break; +} ``` ### C ```cpp - +if (x % i == 1) +{ + j = 0; + continue; +} ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/16.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/16.exercises/solution.md" index 6fa58cc9a..7335833fb 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/16.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/16.exercises/solution.md" @@ -1,52 +1,95 @@ # 有序表的折半查找 -问题描述: +**问题描述:** + 用有序表表示静态查找表时,通常检索函数可以用折半查找来实现。 + 折半查找的查找过程是:首先确定待查记录所在的范围,然后逐步缩小范围直到找到或者确定找不到相应的记录为止。而每次需要缩小的范围均为上一次的一半,这样的查找过程可以被称为折半查找。 + 第二行包含n个用空格隔开的正整数,表示n个有序的整数。输入保证这n个整数是从小到大递增的。 + 第三行包含k个用空格隔开的正整数,表示k次查询的目标。 -输出: + +**输出:** + 只有1行,包含k个整数,分别表示每一次的查询结果。如果在查询中找到了对应的整数,则输出其相应的位置,否则输出-1。 + 请在每个整数后输出一个空格,并请注意行尾输出换行。 +以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include +int binary(int *a, int key, int n) +{ + int left = 0, right = n - 1, mid = 0; + mid = (left + right) / 2; + while (left < right && a[mid] != key) + { + _________________ + } + if (a[mid] == key) + return mid; + return -1; +} +int main(void) +{ + int Base_a[20] = {1, 3, 5, 8, 9, 40, 120, 123, 125, 150, 199, 200, 1250, 1255, 1900, 2000, 2001, 3000, 3950, 5000}; + int Search_a[5] = {12, 199, 9, 2001, 3500}; + int result = 0x00; + for (int i = 0; i < sizeof(Search_a) / sizeof(Search_a[0]); i++) + { + result = binary(Base_a, Search_a[i], sizeof(Base_a) / sizeof(Base_a[0])); + printf("[%d %d] ", Search_a[i], result); + } + printf("\n"); + return 0; +} +``` + ## template ```cpp #include -int binary( int *a, int key, int n ) +int binary(int *a, int key, int n) { - int left = 0, right = n - 1, mid = 0; - mid = ( left + right ) / 2; - while( left < right && a[mid] != key ) - { - if( a[mid] < key ) - left = mid + 1; - else if( a[mid] > key ) - right = mid - 1; - mid = ( left + right ) / 2; - } - if( a[mid] == key ) return mid; - return -1; + int left = 0, right = n - 1, mid = 0; + mid = (left + right) / 2; + while (left < right && a[mid] != key) + { + if (a[mid] < key) + left = mid + 1; + else if (a[mid] > key) + right = mid - 1; + mid = (left + right) / 2; + } + if (a[mid] == key) + return mid; + return -1; } -int main (void) +int main(void) { - int Base_a[20] = {1,3,5,8,9,40,120,123,125,150,199,200,1250,1255,1900,2000,2001,3000,3950,5000}; - int Search_a[5] = {12,199,9,2001,3500}; - int result = 0x00; - for(int i = 0;i < sizeof(Search_a)/sizeof(Search_a[0]);i++) - { - result = binary(Base_a,Search_a[i],sizeof(Base_a)/sizeof(Base_a[0])); - printf("[%d %d] ",Search_a[i],result); - } - printf("\n"); - return 0; + int Base_a[20] = {1, 3, 5, 8, 9, 40, 120, 123, 125, 150, 199, 200, 1250, 1255, 1900, 2000, 2001, 3000, 3950, 5000}; + int Search_a[5] = {12, 199, 9, 2001, 3500}; + int result = 0x00; + for (int i = 0; i < sizeof(Search_a) / sizeof(Search_a[0]); i++) + { + result = binary(Base_a, Search_a[i], sizeof(Base_a) / sizeof(Base_a[0])); + printf("[%d %d] ", Search_a[i], result); + } + printf("\n"); + return 0; } ``` ## 答案 ```cpp - +if (a[mid] < key) + left = mid + 1; +else if (a[mid] > key) + right = mid - 1; +mid = (left + right) / 2; ``` ## 选项 @@ -54,17 +97,29 @@ int main (void) ### A ```cpp - +if (a[mid] > key) + left = mid + 1; +else if (a[mid] < key) + right = mid - 1; +mid = (left + right) / 2; ``` ### B ```cpp - +if (a[mid] < key) + left = mid - 1; +else if (a[mid] > key) + right = mid + 1; +mid = (left + right) / 2; ``` ### C ```cpp - +if (a[mid] < key) + left = mid - 1; +else if (a[mid] >= key) + right = mid + 1; +mid = (left + right) / 2; ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/17.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/17.exercises/solution.md" index 30032df0b..71ec2045b 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/17.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/17.exercises/solution.md" @@ -1,12 +1,20 @@ # 最优路线 -题目描述 +**题目描述** + 探险队要穿越泥潭,必须选择可踩踏的落脚点。可是泥潭面积很大,落脚点又实在少得可怜,一不小心就会深陷泥潭而无法脱身。侦查员费尽周折才从老乡手里弄到了一份地图,图中标出了落脚点的位置,而且令人震惊的是:泥潭只有一条穿越路线,且对于 n×m 的地图,路线长度为 n+m-1!请编程为探险队找出穿越路线。 -输入描述 + +**输入描述** + 两个整数 n 和 m,表示泥潭的长和宽。下面 n 行 m 列表示地形(0 表示泥潭,1 表示落脚点) -输出描述 + +**输出描述** + 用坐标表示穿越路线,坐标之间用 > 分隔 -样例输入 + +**样例输入** + +```json 6 9 1 1 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 @@ -14,52 +22,107 @@ 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 -样例输出 +``` + +**样例输出** + +```json (1,1)>(1,2)>(1,3)>(2,3)>(2,4)>(2,5)>(3,5)>(4,5)>(4,6)>(5,6)>(5,7)>(5,8)>(5,9)>(6,9) +``` + +以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include +#include +const int N = 101; +int map[N][N]; +int n, m; +struct point +{ + int l, r; +} node[N]; +int ans; +void DFS(int x, int y) +{ + if (x == n && y == m) + { + for (int i = 1; i < ans; i++) + printf("(%d,%d)>", node[i].l, node[i].r); + printf("(%d,%d)\n", x, y); + } + else + { + if (map[x][y] == 1 && x <= n && y <= m) + { + node[ans].l = x; + node[ans].r = y; + ans++; + DFS(x + 1, y); + DFS(x, y + 1); + } + } +} +int main() +{ + while (scanf("%d%d", &n, &m) != EOF) + { + ans = 1; + memset(map, 0, sizeof(map)); + for (int i = 1; i <= n; i++) + for (int j = 1; j <= m; j++) + scanf("%d", &map[i][j]); + DFS(1, 1); + } + return 0; +} +``` ## template ```cpp #include #include -const int N=101; +const int N = 101; int map[N][N]; -int n,m; -struct point{ - int l,r; -}node[N]; +int n, m; +struct point +{ + int l, r; +} node[N]; int ans; -void DFS(int x,int y) +void DFS(int x, int y) { - if(x==n&&y==m) - { - for(int i=1;i", node[i].l, node[i].r); - printf("(%d,%d)\n", x, y); - } - else{ - if(map[x][y]==1&&x<=n&&y<=m) - { - node[ans].l=x; - node[ans].r=y; - ans++; - DFS(x+1,y); - DFS(x,y+1); - } - } + if (x == n && y == m) + { + for (int i = 1; i < ans; i++) + printf("(%d,%d)>", node[i].l, node[i].r); + printf("(%d,%d)\n", x, y); + } + else + { + if (map[x][y] == 1 && x <= n && y <= m) + { + node[ans].l = x; + node[ans].r = y; + ans++; + DFS(x + 1, y); + DFS(x, y + 1); + } + } } int main() { - while(scanf("%d%d", &n, &m) != EOF) - { - ans=1; - memset(map,0,sizeof(map)); - for(int i=1;i<=n;i++) - for(int j=1;j<=m;j++) - scanf("%d", &map[i][j]); - DFS(1,1); - } - return 0; + while (scanf("%d%d", &n, &m) != EOF) + { + ans = 1; + memset(map, 0, sizeof(map)); + for (int i = 1; i <= n; i++) + for (int j = 1; j <= m; j++) + scanf("%d", &map[i][j]); + DFS(1, 1); + } + return 0; } ``` diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/18.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/18.exercises/solution.md" index d474f0eb3..eade309e5 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/18.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/18.exercises/solution.md" @@ -5,45 +5,80 @@ 输出形式:保留小数点后两位有效数字;输出从大到小排列

+以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include +#include +void paixu(float *p, int n) +{ + int i, j; + for (i = 0; i < n - 1; i++) + { + for (j = 0; j < n - 1 - i; j++) + { + if (fabs(p[j]) < fabs(p[j + 1])) + { + float tmp; + _______________ + } + } + } +} +int main() +{ + float f[10]; + int i; + for (i = 0; i < 10; i++) + scanf("%f", &f[i]); + paixu(f, 10); + for (i = 0; i < 10; i++) + printf("%.2f ", f[i]); + return 0; +} +``` + ## template ```cpp #include #include -void paixu(float *p,int n) +void paixu(float *p, int n) { - int i,j; - for (i = 0; i +#include +using namespace std; +void shuchu(char *a, int m, int n) +{ + if (n <= 0 || m <= 0 || m > n) + { + return; + } + else + { + cout << a[(m + n) / 2]; + _______________________ + } +} +int main() +{ + char a[20000]; + char b[20001]; + cin >> a; + for (int i = 0; i < 20000; i++) + { + b[i + 1] = a[i]; + } + int n = strlen(a); + shuchu(b, 1, n); + return 0; +} +``` + ## template ```cpp #include #include using namespace std; -void shuchu(char *a,int m, int n) +void shuchu(char *a, int m, int n) { - if(n<=0||m<=0||m>n) - { - return; - } - else - { - cout << a[(m+n)/2]; - shuchu(a,m,(m+n)/2-1); - shuchu(a,(m+n)/2+1,n); - } + if (n <= 0 || m <= 0 || m > n) + { + return; + } + else + { + cout << a[(m + n) / 2]; + shuchu(a, m, (m + n) / 2 - 1); + shuchu(a, (m + n) / 2 + 1, n); + } } int main() { - char a[20000]; - char b[20001]; - cin >> a; - for(int i=0; i<20000; i++) - { - b[i+1] = a[i]; - } - int n = strlen(a); - shuchu(b,1,n); - return 0; + char a[20000]; + char b[20001]; + cin >> a; + for (int i = 0; i < 20000; i++) + { + b[i + 1] = a[i]; + } + int n = strlen(a); + shuchu(b, 1, n); + return 0; } ``` ## 答案 ```cpp - +shuchu(a, m, (m + n) / 2 - 1); +shuchu(a, (m + n) / 2 + 1, n); ``` ## 选项 @@ -61,17 +113,20 @@ int main() ### A ```cpp - +shuchu(a, m, (m + n) / 2 - 1); +shuchu(a, (m + n) / 2, n); ``` ### B ```cpp - +shuchu(a, m, (m + n) / 2 + 1); +shuchu(a, (m + n) / 2 + 1, n); ``` ### C ```cpp - +shuchu(a, m, (m + n) / 2 - 1); +shuchu(a, (m + n) / 2 - 1, n); ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/20.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/20.exercises/solution.md" index 857e35d91..811b2f504 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/1.cpp/20.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/1.cpp/20.exercises/solution.md" @@ -1,54 +1,97 @@ # 求解一元二次方程组根问题 -
利用公式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。
+输入一行,包含三个浮点数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
+以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include +#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

 

提示:

  • n == height.length
  • 0 <= n <= 3 * 104
  • 0 <= height[i] <= 105
+以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include +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]
输出:
10
+以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include +#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

 

提示:

  • 0 <= nums.length <= 300
  • -231 <= nums[i] <= 231 - 1
+以下程序实现了这一功能,请你填补空白处的内容: + +```cpp +#include +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