提交 45858ce0 编写于 作者: 每日一练社区's avatar 每日一练社区

add 3 exercises

上级 4f66165b
# 字符图形输出 # 字符图形输出
编程实现把输入任意整数n后,可打印出n行三角字符阵列图形。例如,输入整数5时,程序运行结果如下: 编程实现把输入任意整数n后,可打印出n行三角字符阵列图形。例如,输入整数5时,程序运行结果如下:
```json
ENTER A NUMBER:5<回车> ENTER A NUMBER:5<回车>
A C F J O A C F J O
B E I N B E I N
D H M D H M
G L G L
K K
```
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <iostream>
using namespace std;
char a[100][100];
int main()
{
char c = 'A';
int n = 5;
for (int i = 0; i < n; i++)
{
for (int j = i; j >= 0; j--)
{
_______________;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
```
## template ## template
```cpp ```cpp
#include <iostream> #include <iostream>
using namespace std; using namespace std;
char a[100][100]; char a[100][100];
int main() int main()
...@@ -39,7 +70,7 @@ int main() ...@@ -39,7 +70,7 @@ int main()
## 答案 ## 答案
```cpp ```cpp
a[j][i - j] = c++;
``` ```
## 选项 ## 选项
...@@ -47,17 +78,17 @@ int main() ...@@ -47,17 +78,17 @@ int main()
### A ### A
```cpp ```cpp
a[j][i] = c++;
``` ```
### B ### B
```cpp ```cpp
a[j - 1][i - j] = c++;
``` ```
### C ### C
```cpp ```cpp
a[j][i - 1] = c++;
``` ```
\ No newline at end of file
...@@ -2,6 +2,48 @@ ...@@ -2,6 +2,48 @@
<p>如何在一亿位整数组成的字符串中找到最长的递增数字字符串&#xff1f;</p> <p>如何在一亿位整数组成的字符串中找到最长的递增数字字符串&#xff1f;</p>
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100000
int main()
{
char buf[MAX_SIZE] = {0};
int i = 0, len = 0, index = 0;
char maxbuf[12] = {0};
char maxbuf2[12] = {0};
int maxlen = 0;
gets(buf);
len = strlen(buf);
maxbuf2[0] = buf[0];
i = 1;
index = 1;
while (i < len)
{
if (buf[i] > buf[i - 1])
{
maxbuf2[index] = buf[i];
index++;
}
else
{
_________________;
}
i++;
}
if (index > maxlen)
{
maxlen = index;
strcpy(maxbuf, maxbuf2);
maxbuf[index] = '\0';
}
printf("最大串长度:%d,字符串:%s\n", maxlen, maxbuf);
return 0;
}
```
## template ## template
```cpp ```cpp
...@@ -11,7 +53,7 @@ ...@@ -11,7 +53,7 @@
int main() int main()
{ {
char buf[MAX_SIZE] = {0}; char buf[MAX_SIZE] = {0};
int i = 0,len = 0,index = 0; int i = 0, len = 0, index = 0;
char maxbuf[12] = {0}; char maxbuf[12] = {0};
char maxbuf2[12] = {0}; char maxbuf2[12] = {0};
int maxlen = 0; int maxlen = 0;
...@@ -20,22 +62,24 @@ int main() ...@@ -20,22 +62,24 @@ int main()
maxbuf2[0] = buf[0]; maxbuf2[0] = buf[0];
i = 1; i = 1;
index = 1; index = 1;
while(i < len) while (i < len)
{ {
if (buf[i] > buf[i-1]) if (buf[i] > buf[i - 1])
{ {
maxbuf2[index] = buf[i]; maxbuf2[index] = buf[i];
index++; index++;
}else }
else
{ {
if (index > maxlen) if (index > maxlen)
{ {
maxlen = index; maxlen = index;
strcpy(maxbuf,maxbuf2); strcpy(maxbuf, maxbuf2);
maxbuf[index] = '\0'; maxbuf[index] = '\0';
maxbuf2[0] = buf[i]; maxbuf2[0] = buf[i];
index = 1; index = 1;
}else }
else
{ {
maxbuf2[0] = buf[i]; maxbuf2[0] = buf[i];
index = 1; index = 1;
...@@ -46,10 +90,10 @@ int main() ...@@ -46,10 +90,10 @@ int main()
if (index > maxlen) if (index > maxlen)
{ {
maxlen = index; maxlen = index;
strcpy(maxbuf,maxbuf2); strcpy(maxbuf, maxbuf2);
maxbuf[index] = '\0'; maxbuf[index] = '\0';
} }
printf("最大串长度:%d,字符串:%s\n",maxlen,maxbuf); printf("最大串长度:%d,字符串:%s\n", maxlen, maxbuf);
return 0; return 0;
} }
``` ```
...@@ -57,7 +101,19 @@ int main() ...@@ -57,7 +101,19 @@ int main()
## 答案 ## 答案
```cpp ```cpp
if (index > maxlen)
{
maxlen = index;
strcpy(maxbuf, maxbuf2);
maxbuf[index] = '\0';
maxbuf2[0] = buf[i];
index = 1;
}
else
{
maxbuf2[0] = buf[i];
index = 1;
}
``` ```
## 选项 ## 选项
...@@ -65,17 +121,48 @@ int main() ...@@ -65,17 +121,48 @@ int main()
### A ### A
```cpp ```cpp
if (index > maxlen)
{
maxlen = index;
strcpy(maxbuf, maxbuf2);
maxbuf[index] = '\0';
maxbuf2[0] = buf[i];
index = 1;
}
``` ```
### B ### B
```cpp ```cpp
if (index > maxlen)
{
maxlen = index;
strcpy(maxbuf, maxbuf2);
maxbuf[index] = '\0';
maxbuf2[0] = buf[i];
index++;
}
else
{
maxbuf2[0] = buf[i];
index++;
}
``` ```
### C ### C
```cpp ```cpp
if (index > maxlen)
{
maxlen = index;
strcpy(maxbuf, maxbuf2);
maxbuf[index] = '\0';
maxbuf2[0] = buf[i];
index = 0;
}
else
{
maxbuf2[0] = buf[i];
index = 0;
}
``` ```
\ No newline at end of file
...@@ -2,6 +2,28 @@ ...@@ -2,6 +2,28 @@
举例如下:一个数组{915,941,960,976,992,1015,1034,1050,1073,1089,1115,1131,1150,1166,1182,1208,1227};目标值假设是1000,最接近元素为992,下标为4 举例如下:一个数组{915,941,960,976,992,1015,1034,1050,1073,1089,1115,1131,1150,1166,1182,1208,1227};目标值假设是1000,最接近元素为992,下标为4
以下程序实现了这一功能,请你填补空白处内容:
```cpp
#include <stdio.h>
int main()
{
int min = (1 << 31) - 1;
int idx = 0;
int arr[] = {915, 941, 960, 976, 992, 1015, 1034, 1050, 1073, 1089, 1115, 1131, 1150, 1166, 1182, 1208, 1227};
int n = 1000;
for (int i = 0; i < sizeof(arr) / sizeof(int); i++)
{
int diff = arr[i] - n;
if (diff < 0)
diff = -diff;
_________________;
}
printf("最接近的是%d 下标是%d", arr[idx], idx);
return 0;
}
```
## template ## template
```cpp ```cpp
...@@ -10,12 +32,13 @@ int main() ...@@ -10,12 +32,13 @@ int main()
{ {
int min = (1 << 31) - 1; int min = (1 << 31) - 1;
int idx = 0; int idx = 0;
int arr[] = {915,941,960,976,992,1015,1034,1050,1073,1089,1115,1131,1150,1166,1182,1208,1227}; int arr[] = {915, 941, 960, 976, 992, 1015, 1034, 1050, 1073, 1089, 1115, 1131, 1150, 1166, 1182, 1208, 1227};
int n = 1000; int n = 1000;
for (int i = 0; i < sizeof(arr) / sizeof(int); i++) for (int i = 0; i < sizeof(arr) / sizeof(int); i++)
{ {
int diff = arr[i] - n; int diff = arr[i] - n;
if (diff < 0) diff = -diff; if (diff < 0)
diff = -diff;
if (diff < min) if (diff < min)
{ {
min = diff; min = diff;
...@@ -30,7 +53,11 @@ int main() ...@@ -30,7 +53,11 @@ int main()
## 答案 ## 答案
```cpp ```cpp
if (diff < min)
{
min = diff;
idx = i;
}
``` ```
## 选项 ## 选项
...@@ -38,17 +65,29 @@ int main() ...@@ -38,17 +65,29 @@ int main()
### A ### A
```cpp ```cpp
if (diff > min)
{
min = diff;
idx = i;
}
``` ```
### B ### B
```cpp ```cpp
if (diff >= min)
{
min = diff;
idx = i;
}
``` ```
### C ### C
```cpp ```cpp
if (diff == min)
{
min = diff;
idx = i;
}
``` ```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册