当前位置:   article > 正文

笔记12:if语句编程练习(打印输出三个数据中的最小值)

笔记12:if语句编程练习(打印输出三个数据中的最小值)

输入三个数,分别放入变量x,y,z中
打印输入数据中最小的那一个数

解决方案1

定义中间变量 t
1.比较x和y的大小关系,将较小的值赋值给t
2.比较t和z的大小关系,将较小的值赋值给t
3.t 中保存的就是3个数中的较小值

(直接t=z)

代码部分:

  1. #include<stdio.h>
  2. int main( )
  3. {
  4. int x = 0;
  5. int y = 0;
  6. int z = 0;
  7. int t = 0;
  8. printf("Input 3 integer: ");
  9. scanf("%d%d%d", &x, &y, &z);
  10. getchar();
  11. if(x<y)
  12. {
  13. t=x;
  14. }
  15. else
  16. {
  17. t=y;
  18. }
  19. if(t>z)
  20. {
  21. t=z;
  22. }
  23. printf("The smallest is:%d\n",t);
  24. getchar();
  25. return 0;
  26. }

运行结果:

解决方案2

代码:

  1. #include<stdio.h>
  2. int main( )
  3. {
  4. int x = 0;
  5. int y = 0;
  6. int z = 0;
  7. printf("Input 3 integer: ");
  8. scanf("%d%d%d",&x,&y,&z);
  9. getchar();
  10. if(x < y)
  11. {
  12. if(x < z)
  13. {
  14. printf("The smallest is:%d\n",x);
  15. getchar();
  16. }
  17. else
  18. {
  19. printf("The smallest is:%d\n",z);
  20. getchar();
  21. }
  22. }
  23. else
  24. {
  25. if(y < z)
  26. {
  27. printf("The smallest is:%d\n",y);
  28. getchar();
  29. }
  30. else
  31. {
  32. printf("The smallest is:%d\n",z);
  33. getchar();
  34. }
  35. }
  36. getchar();
  37. return 0;
  38. }

运行结果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/800765
推荐阅读
相关标签
  

闽ICP备14008679号