#include <stdio.h> void W(int a[10]) { int i, j, t; for(i = 0; i < 10; i++) for(j = 0; j < 9; j++) for(i = 0; i < 9 - j; i++) if(a[i] > a[i + 1]) { t = a[i]; a[i] = a[i + 1]; a[i + 1] = t; } } int main() { int i; int a[10]; printf("请输入10个数:"); for(i = 0; i < 10; i++) scanf("%d", &a[i]); W(a); printf("排序后的结果为:"); for(i = 0; i < 10; i++) printf("%d ", a[i]); return 0; }改正这个代码
时间: 2024-01-23 11:04:25 浏览: 26
这段代码存在两个问题:
1. 内层循环的变量 i 与外层循环的变量 i 重名,会导致内层循环无法正常执行,需要将内层循环的变量名修改为其他名称;
2. 内层循环条件不正确,应该是 j < 9 - i,而不是 i < 9 - j。
修改后的代码如下:
```
#include <stdio.h>
void W(int a[10]) {
int i, j, t;
for(i = 0; i < 10; i++)
for(j = 0; j < 9 - i; j++)
if(a[j] > a[j + 1]) {
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
int main() {
int i;
int a[10];
printf("请输入10个数:");
for(i = 0; i < 10; i++)
scanf("%d", &a[i]);
W(a);
printf("排序后的结果为:");
for(i = 0; i < 10; i++)
printf("%d ", a[i]);
return 0;
}
```
相关问题
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #define SHA1_BLOCK_SIZE 20 #define SHA1_DIGEST_SIZE 5
SHA-1是一种密码散列函数,用于产生消息的数字指纹。SHA-1算法的输出长度为160位,即20个字节。在C语言中,可以使用以下代码来实现SHA-1算法:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define SHA1_BLOCK_SIZE 20
#define SHA1_DIGEST_SIZE 5
void sha1(const uint8_t *message, size_t message_len, uint8_t *digest) {
// TODO: 实现SHA-1算法
}
int main() {
uint8_t message[] = "hello world";
uint8_t digest[SHA1_DIGEST_SIZE];
sha1(message, strlen((char *)message), digest);
printf("SHA-1 digest: ");
for (int i = 0; i < SHA1_DIGEST_SIZE; i++) {
printf("%02x", digest[i]);
}
printf("\n");
return 0;
}
```
} 2、程序填空 #include<stdio.h> voidfun(inta[],intn) {inti,t;
#include <stdio.h>
void fun(int a[], int n) {
int i, t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - 1 - i];
a[n - 1 - i] = t;
}
}
// 示例测试
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int i;
fun(arr, 5);
for (i = 0; i < 5; i++)
printf("%d ", arr[i]);
return 0;
}
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.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)