#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> int main() { int n; scanf("%d\n", &n); int a[100] = {0}; int m; int s[100]; int b = (n + 1) / 2; int i; for (i = 1; i <= n; i++); { scanf("%d", &a[i]); } for (int j = 1; j <= n - 1; j++) { for (int k = 1; k <= n - j - 1; k++) { if (a[k] > a[k + 1]) { m = a[k]; a[k] = a[k + 1]; a[k + 1] = m; } } } for (int j = 1; j <= n; j++) { if (j = 1) printf("%d", a[1]); else if (j = b) printf(" %d", a[b]); else if (j = n) printf("%d", a[n]); else printf(" 0"); } return 0; }这个代码怎么修改
时间: 2023-06-20 11:09:54 浏览: 84
这段代码中有几个问题,需要进行修改:
1. 第5行应该使用 `%d` 格式化字符来读取输入的整数,而不是使用 `%c`。
2. 第17行的 `for` 循环多了一个分号,会导致下面的代码块只执行一次。
3. 第29行、34行和39行中的判断条件应该使用 `==` 而不是 `=`。
修改后的代码如下:
```c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main() {
int n;
scanf("%d", &n);
int a[100] = {0};
int m;
int s[100];
int b = (n + 1) / 2;
int i;
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int j = 1; j <= n - 1; j++) {
for (int k = 1; k <= n - j; k++) {
if (a[k] > a[k + 1]) {
m = a[k];
a[k] = a[k + 1];
a[k + 1] = m;
}
}
}
for (int j = 1; j <= n; j++) {
if (j == 1) {
printf("%d", a[1]);
} else if (j == b) {
printf(" %d", a[b]);
} else if (j == n) {
printf(" %d", a[n]);
} else {
printf(" 0");
}
}
return 0;
}
```
修改后的代码可以正确地读取输入、排序数组并输出结果。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)