21xrx.com
2025-03-26 06:21:55 Wednesday
文章检索 我的文章 写文章
C语言中用if else语句进行三个整数的从小到大排序
2023-06-14 20:28:05 深夜i     20     0
C语言 排序 if

在C语言中,我们需要使用if else语句来完成三个整数从小到大的排序。排序的基本思想是:首先比较三个数中的前两个数,将较小的那个数与其它两个数进行比较,再用if else语句判断大小,最终得到三个数从小到大的排序结果。

下面是实现代码:

#include 
int main() {
  int a, b, c;
  printf("请输入三个整数:");
  scanf("%d%d%d", &a, &b, &c);
  if (a > b)
    int temp = a;
    a = b;
    b = temp;
  
  if (a > c)
    int temp = a;
    a = c;
    c = temp;
  
  if (b > c)
    int temp = b;
    b = c;
    c = temp;
  
  printf("排序后的结果为:%d %d %d\n", a, b, c);
  return 0;
}

在上述代码中,我们首先输入了三个整数,然后使用if else语句进行了排序,并输出了排序后的结果。

else语句。

  
  

评论区