用c语言写 本题目要求读入3个整数a,b,c,然后按从小到大的顺序输出。
时间: 2024-01-03 11:05:19 浏览: 49
好的,下面是实现您要求的功能的示例代码:
```
#include <stdio.h>
int main() {
int a, b, c, temp;
printf("请输入三个整数:\n");
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;
}
```
这个程序首先要求用户输入三个整数,然后使用 if 语句和交换变量的值来将这三个整数从小到大排列。具体来说,首先比较 a 和 b 的大小,如果 a 大于 b,则交换它们的值;然后比较 a 和 c 的大小,如果 a 大于 c,则交换它们的值;最后比较 b 和 c 的大小,如果 b 大于 c,则交换它们的值。最后输出排序后的结果即可。
相关问题
本题目要求读入3个整数a,b,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 (b > c) {
temp = b;
b = c;
c = temp;
if (a > b) {
temp = a;
a = b;
b = temp;
}
}
printf("从小到大排序后的结果为:%d %d %d", a, b, c);
return 0;
}
```
以上代码可实现按从小到大的顺序输出三个整数,希望能够帮到你。
要求读入若干整数(最少1个不超过4个),然后按从小到大的顺序输出 C语言
以下是C语言读入若干整数并按从小到大顺序输出的代码示例:
```c
#include <stdio.h>
int main() {
int n, i, j, temp, arr[4];
printf("请输入1-4个整数,用空格隔开:\n");
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("从小到大的顺序输出为:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)