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

add 3 exercises

上级 4f66165b
# 字符图形输出
编程实现把输入任意整数n后,可打印出n行三角字符阵列图形。例如,输入整数5时,程序运行结果如下:
```json
ENTER A NUMBER:5<回车>
A C F J O
B E I N
D H M
G L
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
```cpp
#include <iostream>
#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--)
{
a[j][i - j] = c++;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
char c = 'A';
int n = 5;
for (int i = 0; i < n; i++)
{
for (int j = i; j >= 0; j--)
{
a[j][i - j] = c++;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
```
## 答案
```cpp
a[j][i - j] = c++;
```
## 选项
......@@ -47,17 +78,17 @@ int main()
### A
```cpp
a[j][i] = c++;
```
### B
```cpp
a[j - 1][i - j] = c++;
```
### C
```cpp
a[j][i - 1] = c++;
```
\ No newline at end of file
......@@ -2,6 +2,48 @@
<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
```cpp
......@@ -10,54 +52,68 @@
#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
{
if (index > maxlen)
{
maxlen = index;
strcpy(maxbuf,maxbuf2);
maxbuf[index] = '\0';
maxbuf2[0] = buf[i];
index = 1;
}else
{
maxbuf2[0] = buf[i];
index = 1;
}
}
i++;
}
if (index > maxlen)
{
maxlen = index;
strcpy(maxbuf,maxbuf2);
maxbuf[index] = '\0';
}
printf("最大串长度:%d,字符串:%s\n",maxlen,maxbuf);
return 0;
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
{
if (index > maxlen)
{
maxlen = index;
strcpy(maxbuf, maxbuf2);
maxbuf[index] = '\0';
maxbuf2[0] = buf[i];
index = 1;
}
else
{
maxbuf2[0] = buf[i];
index = 1;
}
}
i++;
}
if (index > maxlen)
{
maxlen = index;
strcpy(maxbuf, maxbuf2);
maxbuf[index] = '\0';
}
printf("最大串长度:%d,字符串:%s\n", maxlen, maxbuf);
return 0;
}
```
## 答案
```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()
### A
```cpp
if (index > maxlen)
{
maxlen = index;
strcpy(maxbuf, maxbuf2);
maxbuf[index] = '\0';
maxbuf2[0] = buf[i];
index = 1;
}
```
### B
```cpp
if (index > maxlen)
{
maxlen = index;
strcpy(maxbuf, maxbuf2);
maxbuf[index] = '\0';
maxbuf2[0] = buf[i];
index++;
}
else
{
maxbuf2[0] = buf[i];
index++;
}
```
### C
```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,35 +2,62 @@
举例如下:一个数组{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
```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;
if (diff < min)
{
min = diff;
idx = i;
}
}
printf("最接近的是%d 下标是%d", arr[idx], idx);
return 0;
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;
if (diff < min)
{
min = diff;
idx = i;
}
}
printf("最接近的是%d 下标是%d", arr[idx], idx);
return 0;
}
```
## 答案
```cpp
if (diff < min)
{
min = diff;
idx = i;
}
```
## 选项
......@@ -38,17 +65,29 @@ int main()
### A
```cpp
if (diff > min)
{
min = diff;
idx = i;
}
```
### B
```cpp
if (diff >= min)
{
min = diff;
idx = i;
}
```
### C
```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.
先完成此消息的编辑!
想要评论请 注册