作业帮 > 综合 > 作业

简单C程序:给随机三个数由大到小排序,求高手检查错误,

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/10 15:24:27
简单C程序:给随机三个数由大到小排序,求高手检查错误,
#include<stdio.h>
int main()
{
  int a,b,c;
  printf("Please input three integers:");
  scanf("%d%d%d",&a,&b,&c);
  if(a>=b)
     if(b>=c)
        printf("The number from small to large sort is:%d>%d>%d\n",a,b,c);
     else
        printf("The number from small to large sort is:%d>%d>%d\n",a,c,b);
  else if(b>=a)
             if(a>=c)
                printf("The number from small to large sort is:%d>%d>%d\n",b,a,c);
             else
                printf("The number from small to large sort is:%d>%d>%d\n",b,c,a);
          else if(c>=a)
                     if(a>=b)
                        printf("The number from small to large sort is:%d>%d>%d\n",c,a,b);
                     else
                        printf("The number from small to large sort is:%d>%d>%d\n",c,b,a);
}
你的程序逻辑判断上有问题
你检查下if(a>=b)这个语句里面的if-else语句:
如果a>=b,在b>=c不成立时,并不能说明a>=c>=b
再问: 请问,怎么修改
再答: 你把逻辑理清就行了:#include<stdio.h>
int main()
{
    int a,b,c;
    printf("Please input three integers:");
    scanf("%d%d%d",&a,&b,&c);
    if(a>=b)
        if(b>=c)
            printf("The number from small to large sort is:%d>=%d>=%d\n",a,b,c);
        else if(a>=c)
            printf("The number from small to large sort is:%d>=%d>=%d\n",a,c,b);
        else
            printf("The number from small to large sort is:%d>=%d>=%d\n",c,a,b);
    else/* if(b>=a)*/
        if(a>=c)
            printf("The number from small to large sort is:%d>=%d>=%d\n",b,a,c);
        else if (b>=c)
            printf("The number from small to large sort is:%d>=%d>=%d\n",b,c,a);
        else
            printf("The number from small to large sort is:%d>=%d>=%d\n",c,b,a);
/*
    else if(c>=a)
        if(a>=b)
            printf("The number from small to large sort is:%d>=%d>=%d\n",c,a,b);
        else
            printf("The number from small to large sort is:%d>=%d>=%d\n",c,b,a);
*/
}