鱼C论坛

 找回密码
 立即注册
查看: 1114|回复: 4

[已解决]关于三个数字从小到大排序的问题

[复制链接]
发表于 2020-11-15 17:12:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 jpan1221 于 2020-11-15 17:17 编辑

代码如下:


  1. include <stdio.h>

  2. int main() {
  3.     int a, b, c;
  4.     printf("input the first integer number: ");
  5.     scanf("%d", &a);
  6.     getchar();
  7.     printf("input the second integer number: ");
  8.     scanf("%d", &b);
  9.     getchar();
  10.     printf("input the third integer number: ");
  11.     scanf("%d", &c);
  12.     getchar();
  13.     int temp[256] = {0}; int t = 0;
  14.     temp[0] = a; temp[1] = b; temp[2] = c;
  15.     for(int i = 0; i < 3; i++)
  16.     {
  17.         for(int j = i + 1; j < 3; j ++)
  18.         {
  19.             if(temp[i] < temp[j])
  20.             {
  21.                 t = temp[i];
  22.                 temp[i] = temp[j];
  23.                 temp[j] = t;
  24.             }
  25.         }

  26.     }

  27.     printf("The biggest number is:%d\n",temp[0]);
  28.     printf("The smallest number is:%d\n",temp[2]);
  29. }
复制代码


我没看出来哪里有问题
但是运行结果是这样的:
input the first integer number:10
input the second integer number:1000
input the third integer number:15
The biggest number is:1
000
The smallest number is:0

Process finished with exit code 0

求大佬帮忙看看问题所在
最佳答案
2020-11-15 17:57:35
1、三个数比较大小,用不着弄得这么复杂;2、输入的都是整数,用不着用 getchar(); 来吸收回车键;

  1. include <stdio.h>

  2. int main() {
  3.      int a, b, c,t,max,min;
  4.      printf("input the first integer number: ");
  5.      scanf("%d", &a);
  6.      printf("input the second integer number: ");
  7.      scanf("%d", &b);
  8.      printf("input the third integer number: ");
  9.      scanf("%d", &c);
  10.    
  11.     max = a;
  12.     min = a;
  13.     if (max < b)
  14.     {         
  15.          max = b;
  16.     }
  17.     else
  18.     {
  19.         min = b;
  20.     }
  21.     if (max < c)
  22.     {         
  23.          max = c;
  24.     }
  25.     else
  26.     {
  27.         min = c;
  28.     }

  29.     printf("The biggest number is:%d\n",max);
  30.     printf("The smallest number is:%d\n",min);
  31. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-15 17:20:33 | 显示全部楼层

回帖奖励 +3 鱼币

  1.     for(int i = 0; i < 3; i++)
  2.     {
  3.         for(int j = i + 1; j < 3; j ++)
  4.         {
  5.             if(temp[i] < temp[j])
  6.             {
  7.                 t = temp[i];
  8.                 temp[i] = temp[j];
  9.                 temp[j] = t;
  10.             }
  11.         }
复制代码


你的错误在这里,0和1比较、交换,1和2比较、交换,最后一次循环2和3比较、交换时,temp[3]是0
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-11-15 17:57:35 | 显示全部楼层    本楼为最佳答案   
1、三个数比较大小,用不着弄得这么复杂;2、输入的都是整数,用不着用 getchar(); 来吸收回车键;

  1. include <stdio.h>

  2. int main() {
  3.      int a, b, c,t,max,min;
  4.      printf("input the first integer number: ");
  5.      scanf("%d", &a);
  6.      printf("input the second integer number: ");
  7.      scanf("%d", &b);
  8.      printf("input the third integer number: ");
  9.      scanf("%d", &c);
  10.    
  11.     max = a;
  12.     min = a;
  13.     if (max < b)
  14.     {         
  15.          max = b;
  16.     }
  17.     else
  18.     {
  19.         min = b;
  20.     }
  21.     if (max < c)
  22.     {         
  23.          max = c;
  24.     }
  25.     else
  26.     {
  27.         min = c;
  28.     }

  29.     printf("The biggest number is:%d\n",max);
  30.     printf("The smallest number is:%d\n",min);
  31. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-15 18:16:42 | 显示全部楼层
本帖最后由 jackz007 于 2020-11-15 18:26 编辑
  1.         for(int i = 0 ; i < 3 ; i ++)
复制代码

        修改成下面这样
  1.         for(int i = 0 ; i < 2 ; i ++)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-15 20:40:40 | 显示全部楼层
风过无痕1989 发表于 2020-11-15 17:57
1、三个数比较大小,用不着弄得这么复杂;2、输入的都是整数,用不着用 getchar(); 来吸收回车键;

谢谢1
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-9 10:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表