c语言输入十个随机整数从小到大排列
时间: 2024-05-15 14:18:08 浏览: 19
你可以使用冒泡排序算法来实现将十个随机整数从小到大排列。以下是代码示例:
```c
#include <stdio.h>
int main() {
int arr[10];
int i, j, temp;
// 读入随机整数
printf("请输入十个随机整数:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
// 冒泡排序
for (i = 0; i < 9; i++) {
for (j = 0; j < 9 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// 输出结果
printf("从小到大排序后的结果为:\n");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
相关问题
输入四个整数从小到大排列的C语言代码
#include <stdio.h>
int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int arr[4] = {a, b, c, d};
int temp;
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 4; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < 4; i++) {
printf("%d ", arr[i]);
}
return 0;
}
c语言输入三个整数从小到大排序
好的,以下是一个简单的C语言代码示例,可以实现输入三个整数,然后从小到大进行排序:
```c
#include <stdio.h>
int main() {
int a, b, c, temp;
printf("请输入三个整数:");
scanf("%d%d%d", &a, &b, &c);
if (a > b) {
temp = a;
a = b;
b = temp;
}
if (a > c) {
temp = a;
a = c;
c = temp;
}
if (b > c) {
temp = b;
b = c;
c = temp;
}
printf("从小到大排序:%d %d %d\n", a, b, c);
return 0;
}
```
在这个代码中,我们首先定义了三个整数变量:a、b、c,以及一个临时变量temp。然后,通过`scanf`语句,从用户输入中获取这三个整数的值。
接下来,我们使用`if`语句和临时变量temp来对这三个整数进行排序。`if`语句用于比较三个数的大小关系,临时变量temp用于交换两个变量的值。最后,我们使用`printf`语句将排序后的结果输出到屏幕上。
需要注意的是,C语言中没有提供像C++中的`swap`函数,因此我们需要使用临时变量来实现变量值的交换操作。
相关推荐
![](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)