排序三个数字的简单的方法

编程入门 行业动态 更新时间:2024-10-10 03:27:27
本文介绍了排序三个数字的简单的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有一个更简单的和更好的解决这个问题的方法,因为

  • 我用了太多的变数。
  • 我用这么多如果别人语句
  • 我这样做是使用蛮力方法
  •   

    写接收三个整数作为输入,递增的顺序输出数的程序。结果  不要使用循环/阵列。

    的#include<&stdio.h中GT;主要(){   诠释NO1;   诠释NO2;   诠释NO3;   诠释STO;   INT喜;   INT LO;   的printf(请输入1号:);   scanf函数(%d个,&安培; NO1);   的printf(进入2号);   scanf函数(%d个,&安培; NO2);   的printf(请输入3号:)​​;   scanf函数(%d个,&安培; NO3);   如果(NO1> NO2){      STO = NO1;      LO = NO2;   }其他{      STO = NO2;      LO = NO1;   }   如果(STO> NO3){      HI = STO;      如果(LO> NO3){         STO = LO;         LO = NO3;      }其他{         STO = NO3;      }   }其他HI = NO3;   的printf(最小%d个\\ N,LO);   的printf(MIDDLE%d个\\ N,STO);   的printf(最高%d个\\ N,喜);   残培();}

    解决方案

    如果(A> C)   掉期(A,C)如果(A> B)   掉期(A,B)//现在最小的元素是第一个。只是检查2次和3次如果(B> C)   掉期(B,C);

      

    注:交换改变了两个值  变数。

    Is there a simpler and better way to solve this problem because

  • I used too many variables.
  • I used so many if else statements
  • I did this using the brute force method
  • Write a program that receives three integers as input and outputs the numbers in increasing order. Do not use loop / array.

    #include <stdio.h> main(){ int no1; int no2; int no3; int sto; int hi; int lo; printf("Enter No. 1: "); scanf("%d", &no1); printf("Enter No. 2: "); scanf("%d", &no2); printf("Enter No. 3: "); scanf("%d", &no3); if (no1>no2) { sto=no1; lo=no2; } else { sto=no2; lo=no1; } if (sto>no3) { hi=sto; if(lo>no3){ sto=lo; lo=no3; }else { sto=no3; } }else hi=no3; printf("LOWEST %d\n", lo); printf("MIDDLE %d\n", sto); printf("HIGHEST %d\n", hi); getch(); }

    解决方案

    if (a > c) swap(a, c) if (a > b) swap(a, b) //Now the smallest element is the first one. Just check the 2-nd and 3-rd if (b > c) swap(b, c);

    Note: Swap changes the values of two variables.

    更多推荐

    排序三个数字的简单的方法

    本文发布于:2023-11-29 09:57:32,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1646069.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:简单   数字   方法

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!