提交 8615933c 编写于 作者: ToTensor's avatar ToTensor

optimize 15 exercises

上级 14acb73a
......@@ -26,43 +26,86 @@
2
```
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <iostream>
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 <iostream>
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
......@@ -2,6 +2,25 @@
<p style="margin-left:0pt; margin-right:0pt">Ø 问题描述&#xff1a;已知一只家禽得了流感&#xff0c;流感的传播时间为 24 小时&#xff0c;在这 24 小时内最多能将流感传给其它 M 只家禽&#xff0c;农场主需要购买紧急药品&#xff0c;假设发现时已经过了 N 天&#xff0c;那么农场主需要至少买多少包药呢&#xff1f;&#xff08;一包药为一只家禽用量&#xff09;</p><p style="margin-left:0pt; margin-right:0pt">Ø 输入&#xff1a;一行&#xff0c;两个整数&#xff0c;第一个表示流感传染家禽的数量 M&#xff0c;第二个表示发现时已过的天数。</p><p style="margin-left:0pt; margin-right:0pt">Ø 输出&#xff1a;一行&#xff0c;一个整数&#xff0c;表示需要药的包数。</p><p style="margin-left:0pt; margin-right:0pt">Ø 样例输入&#xff1a;</p><p style="margin-left:0pt; margin-right:0pt">10 2</p><p style="margin-left:0pt; margin-right:0pt">Ø 样例输出&#xff1a;</p><p style="margin-left:0pt; margin-right:0pt">121</p>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <iostream>
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
......@@ -9,6 +9,45 @@
样例输出
Positive elegance</p>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <stdio.h>
#include <string.h>
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</p>
#include <string.h>
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(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;
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
......@@ -20,6 +20,34 @@
输出格式
  对于每一个询问&#xff0c;输出一行包含&#34;Case #x: y&#34;&#xff0c;x是询问编号&#xff08;从1开始标号&#xff09;&#xff0c;y是你每天最少需要的盒子数量。</p>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include<iostream>
#include<cmath>
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
# 寻找孪生素数
数学家希尔伯特在1900年国际数学家大会的报告上提出一个“孪生素数猜想”,即: 存在无穷多个素数p,使得p + 2是素数。p和p+2这一对差为2的素数,被称为“孪生素数”。
看起来,这个猜想是成立的,我们总能找到很多对孪生素数,例如:3和5,5和7,11和13…… 这一猜想至今还未被证明。
现在,对于给定的整数n, 请寻找大于n的最小的一对孪生素数p和q(q=p+2)。
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <cmath>
#include <iostream>
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<<i<<' '<<i+2;
return 0;
int x;
cin >> 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
# 有序表的折半查找
问题描述:
**问题描述:**
用有序表表示静态查找表时,通常检索函数可以用折半查找来实现。
折半查找的查找过程是:首先确定待查记录所在的范围,然后逐步缩小范围直到找到或者确定找不到相应的记录为止。而每次需要缩小的范围均为上一次的一半,这样的查找过程可以被称为折半查找。
第二行包含n个用空格隔开的正整数,表示n个有序的整数。输入保证这n个整数是从小到大递增的。
第三行包含k个用空格隔开的正整数,表示k次查询的目标。
输出:
**输出:**
只有1行,包含k个整数,分别表示每一次的查询结果。如果在查询中找到了对应的整数,则输出其相应的位置,否则输出-1。
请在每个整数后输出一个空格,并请注意行尾输出换行。
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <stdio.h>
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 <stdio.h>
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
# 最优路线
题目描述
**题目描述**
探险队要穿越泥潭,必须选择可踩踏的落脚点。可是泥潭面积很大,落脚点又实在少得可怜,一不小心就会深陷泥潭而无法脱身。侦查员费尽周折才从老乡手里弄到了一份地图,图中标出了落脚点的位置,而且令人震惊的是:泥潭只有一条穿越路线,且对于 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 <stdio.h>
#include <string.h>
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 <stdio.h>
#include <string.h>
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<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);
}
}
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;
}
```
......
......@@ -5,45 +5,80 @@
输出形式&#xff1a;保留小数点后两位有效数字;输出从大到小排列
</p>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <math.h>
#include <stdio.h>
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 <math.h>
#include <stdio.h>
void paixu(float *p,int n)
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;
tmp = p[j];
p[j] = p[j + 1];
p[j + 1] = tmp;
}
}
}
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;
tmp = p[j];
p[j] = p[j + 1];
p[j + 1] = 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;
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;
}
```
## 答案
```cpp
tmp = p[j];
p[j] = p[j + 1];
p[j + 1] = tmp;
```
## 选项
......@@ -51,17 +86,23 @@ int main()
### A
```cpp
tmp = p[j];
p[j] = p[j - 1];
p[j - 1] = tmp;
```
### B
```cpp
tmp = p[j];
p[j + 1] = tmp;
p[j] = p[j + 1];
```
### C
```cpp
tmp = p[j];
p[j - 1] = tmp;
p[j] = p[j - 1];
```
\ No newline at end of file
# 擅长编码的小k
题目描述
**题目描述**
小k不仅擅长数学,也擅长编码。有一种编码方式如下:
首先写下文本中间的字符(如果文本中的字符编号为1..n,那么中间一个字符的编号为(n+1)DIV 2,其中DIV为整除的意思),然后 用这个方法递归地写下左边,最后再按这个方法递归地写下右边。例如,单词为orthography则其编码为gtorhoprahy。即先写中间的那个字符g,再对ortho递归地编码,最后将raphy递归地编码就得到了gtorhoprahy。
给一个原来的文本,求出编码后的 文本。
输入
**输入**
一行字符,表示原始的文本内容。
输出
**输出**
一行字符,表示编码后的文本内容。
样例
输入
**样例**
**输入**
```json
orthography
输出
```
**输出**
```json
gtorhoprahy
提示
```
**提示**
100%的数据,字符串长度不超过20000
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <iostream>
#include <cstring>
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 <iostream>
#include <cstring>
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
# 求解一元二次方程组根问题
<pre>利用公式x1 = (-b + sqrt(b*b-4*a*c))/(2*a), x2 = (-b - sqrt(b*b-4*a*c))/(2*a)求一元二次方程ax<sup>2</sup> + bx + c =0 的根,其中a不等于0。
输入一行,包含三个浮点数a, b, c(它们之间以一个空格分开),分别表示方程ax<sup>2</sup> + 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
<pre>利用公式x1 = (-b + sqrt(b*b-4*a*c))/(2*a), x2 = (-b - sqrt(b*b-4*a*c))/(2*a)求一元二次方程ax<sup>2</sup> + bx + c =0 的根,其中a不等于0。<br />
输入一行,包含三个浮点数a, b, c(它们之间以一个空格分开),分别表示方程ax<sup>2</sup> + bx + c =0 的系数。输出一行,表示方程的解。<br />
若两个实根相等,则输出形式为:x1=x2=...。<br />
若两个实根不等,则输出形式为:x1=...;x2 = ...,其中x1若是两个虚根,则输出:x1=实部+虚部i; x2=实部-虚部i,其中x1,x2满足以下两个条件中的一个:<br />
1. x1的实部大于x2的实部<br />
2. x1的实部等于x2的实部且x1的虚部大于等于x2的虚部<br />
所有实数部分要求精确到小数点后5位,数字、符号之间没有空格。<br />
样例输入:1.0 2.0 8.0<br />
样例输出:x1=-1.00000+2.64575i;x2=-1.00000-2.64575i</pre>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <iostream>
#include <iomanip>
#include <cmath>
#include <complex>
using namespace std;
static const double e = 1e-12;
_______________________________
int main()
{
complex<double> a, b, c;
complex<double> 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 <iostream>
#include<iomanip>
#include <iomanip>
#include <cmath>
#include <complex>
using namespace std;
static const double e = 1e-12;
bool operator == (complex<double> c1, complex<double> c2) { return abs(c1-c2) < e;}
bool operator==(complex<double> c1, complex<double> c2) { return abs(c1 - c2) < e; }
int main()
{
complex<double> a,b,c;
complex<double> 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;
complex<double> a, b, c;
complex<double> 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<double> c1, complex<double> c2) { return abs(c1 - c2) < e; }
```
## 选项
......@@ -56,17 +99,17 @@ int main()
### A
```cpp
;
```
### B
```cpp
bool operator==(complex<double> c1, complex<double> c2) { return abs(c1 - c2) > e; }
```
### C
```cpp
bool operator==(complex<double> c1, complex<double> c2) { return abs(c1 - c2) >= e; }
```
\ No newline at end of file
......@@ -52,6 +52,87 @@
</div>
</div>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void solveSudoku(vector<vector<char>> &board)
{
int size = board.size();
vector<vector<bool>> rows(size, vector<bool>(10));
vector<vector<bool>> cols(size, vector<bool>(10));
vector<vector<bool>> boxes(size, vector<bool>(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<vector<bool>> &rows,
vector<vector<bool>> &cols, vector<vector<bool>> &boxes)
{
return !rows[row][num] && !cols[col][num] && !boxes[idx][num];
}
bool dfs(vector<vector<char>> &board, int size, vector<vector<bool>> &rows,
vector<vector<bool>> &cols, vector<vector<bool>> &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<vector<char>> &board)
{
int size = board.size();
vector<vector<bool>> rows(size, vector<bool>(10));
vector<vector<bool>> cols(size, vector<bool>(10));
vector<vector<bool>> boxes(size, vector<bool>(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<vector<char>> &board)
{
int size = board.size();
vector<vector<bool>> rows(size, vector<bool>(10));
vector<vector<bool>> cols(size, vector<bool>(10));
vector<vector<bool>> boxes(size, vector<bool>(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<vector<bool>> &rows,
vector<vector<bool>> &cols, vector<vector<bool>> &boxes)
{
return !rows[row][num] && !cols[col][num] && !boxes[idx][num];
}
bool dfs(vector<vector<char>> &board, int size, vector<vector<bool>> &rows,
vector<vector<bool>> &cols, vector<vector<bool>> &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<vector<bool>> &rows,
vector<vector<bool>> &cols, vector<vector<bool>> &boxes)
{
return !rows[row][num] && !cols[col][num] && !boxes[idx][num];
}
bool dfs(vector<vector<char>> &board, int size, vector<vector<bool>> &rows,
vector<vector<bool>> &cols, vector<vector<bool>> &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
......@@ -2,6 +2,28 @@
<p>给定 <em>n</em> 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。</p><p> </p><p><strong>示例 1:</strong></p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0042.Trapping%20Rain%20Water/images/rainwatertrap.png" style="height: 161px; width: 412px;" /></p><pre><strong>输入:</strong>height = [0,1,0,2,1,0,1,3,2,1,2,1]<strong><br />输出:</strong>6<strong><br />解释:</strong>上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 </pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>height = [4,2,0,3,2,5]<strong><br />输出:</strong>9</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>n == height.length</code></li> <li><code>0 <= n <= 3 * 10<sup>4</sup></code></li> <li><code>0 <= height[i] <= 10<sup>5</sup></code></li></ul>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int trap(vector<int> &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
......@@ -2,6 +2,49 @@
<p>给定 <em>n</em> 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。</p><p>求在该柱状图中,能够勾勒出来的矩形的最大面积。</p><p>&nbsp;</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram.png"></p><p><small>以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为&nbsp;<code>[2,1,5,6,2,3]</code>。</small></p><p>&nbsp;</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram_area.png"></p><p><small>图中阴影部分为所能勾勒出的最大矩形面积,其面积为&nbsp;<code>10</code>&nbsp;个单位。</small></p><p>&nbsp;</p><p><strong>示例:</strong></p><pre><strong>输入:</strong> [2,1,5,6,2,3]<strong><br />输出:</strong> 10</pre>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <stdio.h>
#include <stdlib.h>
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
......@@ -2,6 +2,37 @@
<p>给你一个未排序的整数数组 <code>nums</code> ,请你找出其中没有出现的最小的正整数。</p><p> </p><p><strong>进阶:</strong>你可以实现时间复杂度为 <code>O(n)</code> 并且只使用常数级别额外空间的解决方案吗?</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,0]<strong><br />输出:</strong>3</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [3,4,-1,1]<strong><br />输出:</strong>2</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [7,8,9,11,12]<strong><br />输出:</strong>1</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= nums.length <= 300</code></li> <li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li></ul>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int firstMissingPositive(vector<int> &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
......@@ -35,6 +35,54 @@
</div>
</div>
以下程序实现了这一功能,请你填补空白处的内容:
```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int totalNQueens(int n)
{
vector<int> stack(n);
return dfs(n, 0, stack);
}
private:
int dfs(int n, int row, vector<int> &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<int> &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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册