大佬教程收集整理的这篇文章主要介绍了Bermudez C第5章P 2:不使用数组或循环来升序,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
问题如下:编写一个程序,读取五个值并按升序写出.
非常萌芽的程序员不允许使用数组或循环.唯一可用的工具是“if”和“switch”语句.
这是我的问题:我通过蛮力解决了这个问题 – 它非常不优雅.一个猜测是,我应该对这个练习感到不安;也就是说,也许百慕兹想向读者展示一个人需要做的事情!单纯依赖“if”和/或“switch”语句时的排列.
另一个猜测(可能更可能的是)我正在做一些非常错误的事情.有些东西告诉我,我可以将这些代码至少削减一半.
有什么建议?
#include <stdio.h> int main() { int a,b,c,d,e,temp; printf("Program 5.2: Ascending Order of Values\n"); printf("======================================\n\n"); printf("Enter first value: "); scanf("%d",&a); printf("Enter second value: "); scanf("%d",&b); printf("Enter third value: "); scanf("%d",&c); printf("Enter fourth value: "); scanf("%d",&d); printf("Enter fifth value: "); scanf("%d",&E); printf("\nRe-arranged in ascending order: \n"); printf("===============================\n\n"); /* SorTing Network - 9 comparators */ if (a > b) { temp = a; a = b; b = temp; } // 0,1 if (d > E) { temp = d; d = e; e = temp; } // 3,4 if (c > E) { temp = c; c = e; e = temp; } // 2,4 if (c > d) { temp = c; c = d; d = temp; } // 2,3 if (a > d) { temp = a; a = d; d = temp; } // 0,3 if (a > C) { temp = a; a = c; c = temp; } // 0,2 if (b > E) { temp = b; b = e; e = temp; } // 1,4 if (b > d) { temp = b; b = d; d = temp; } // 1,3 if (b > C) { temp = b; b = c; c = temp; } // 1,2 printf("%d %d %d %d %d\n",a,E); return 0; }
以上是大佬教程为你收集整理的Bermudez C第5章P 2:不使用数组或循环来升序全部内容,希望文章能够帮你解决Bermudez C第5章P 2:不使用数组或循环来升序所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。