你看,我自学了C++(不完全,我还在拖延-_-).所以,现在我开始上大学,他们正在教C,他们让我们做了一个输入四个整数的程序,我们必须告诉它们中最大和最小的.简单,不是吗?
问题是,我已经对函数和数组有了很好的理解.是的,我可以在阵列中编程,没问题.但由于这是第一个实验室,我们还没有"学到"那个,所以我不能使用其中的任何一个,它就会非常简单.
这就是我在那里写的(某种程度上感觉不对).
#include<stdio.h>
int main(void)
{
int first, second, third, fourth;
printf("Enter four integers (separated by space): ");
scanf("%d %d %d %d", &first, &second, &third, &fourth);
if((first>second) && (first>third) && (first>fourth))
printf("\nFirst number is largest");
else if((second>first) && (second>third) && (second>fourth))
printf("\nSecond number is largest");
else if((third>second) && (third>first) && (third>fourth))
printf("\nThird number is largest");
else if((fourth>second) && (fourth>third) && (fourth>first))
printf("\nFourth number is largest");
if((first<second) && (first<third) && (first<fourth))
printf("\nFirst number is smallest");
else if((second<first) && (second<third) && (second<fourth))
printf("\nSecond number is smallest");
else if((third<second) && (third<first) && (third<fourth))
printf("\nThird number is smallest");
else if((fourth<second) && (fourth<third) && (fourth<first))
printf("\nFourth number is smallest");
printf("\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,它太长,太无聊和复杂.但是看到我们现在所讨论的所有内容都是循环和决策语句.有更优雅的方式吗?一个使用较少的ifs?并不是说这有什么问题,但可能会更好.
PS这不完全是'家庭作业'或任何东西.我做了一个程序,我只是想知道我可以做些什么来使它变得更好并学习更好的编程实践.
hac*_*cks 25
根据OP的条件
但是看到我们现在所讨论的所有内容都是循环和决策语句.有更优雅的方式吗?一个使用较少的
ifs?
只有一个if和一个for循环可以完成此任务.简单而简短!
#include <stdio.h>
int main(void)
{
int num, max, min;
printf ("Enter four numbers: ");
scanf ("%d", &num);
max = min = num;
for (int i = 0; i < 3; i++)
{
scanf ("%d", &num);
if (max < num){
max = num;
continue;
}
min = num;
}
printf ("The smallest and largest of given four numbers are %d and %d respectively.\n", min, max);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
m01*_*m01 10
做一个"手动" 合并排序,或者好吧,只是它的第二位:
从概念上讲,合并排序的工作原理如下
- 将未排序的列表分成n个子列表,每个子列表包含1个元素(1个元素的列表被视为已排序).
- 重复合并子列表以生成新的子列表,直到只剩下1个子列表.这将是排序列表.

码:
int a = 5, b=4, c=7, d=9;
int min_ab, min_cd, min;
min_ab = a < b ? a : b;
min_cd = c < d ? c : d;
min = min_ab < min_cd ? min_ab : min_cd;
printf("%d", min);
Run Code Online (Sandbox Code Playgroud)
..和类似的最大.
如果您愿意,可以将三元运算符展开if (a < b) { min_ab = a; } else { min_ab = b; }(扩展多行以便于阅读).
合并排序具有复杂性O(n*log(n)),因此您最多需要O(n*log(n)) ifs(请参阅有关合并排序的维基百科文章).根据维基百科的说法,"......这些都是比较分类,因此在平均或最差情况下(源)不能比O(n log n)表现更好,所以我认为这不应该太过分了.最小数量的ifs ..虽然您可以尝试查看是否手动执行其他算法之一导致更少的ifs ;-).
尝试这样的事情
int main(void) {
int a=-2,b=-3,c=-4,d=-5;
int max=a,min=a;
if(b>max){
max=b;
}else if(b<min){
min=b;
}
if(c>max){
max=c;
}else if(c<min){
min=c;
}
if(d>max){
max=d;
}else if(d<min){
min=d;
}
printf("max: %d min : %d",max,min);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请求找到最大和最小的课堂问题的整点同时是教你提取每个比较最大的有价值的信息.
例如,如果您知道这a > b是真的,那么从单一比较中您应该意识到,a它不再是最小的候选者,并且不应再参与任何专门用于寻找最小的比较.而且,与此同时,你应该意识到这b不再是最大的候选人.有4个数字,两个测试a > b并且c > d已经清楚地将数字分成两个独立的类:最大的两个候选者和最小的两个候选者.其余的很简单.
换句话说,整个构思是要找到极值并行,利用各自的比较提供的信息来发现进一步的任务都最小和最大的价值.
if (first > second) {
int t = first; first = second; second = t;
}
if (third > fourth) {
int t = third; third = fourth; fourth = t;
}
/* Now 'first' and 'third' are candidates for the smallest,
while 'second' and 'fourth' are candidates for the largest */
int min = first < third ? first : third;
int max = second > fourth ? second : fourth;
Run Code Online (Sandbox Code Playgroud)
如您所见,这只需要四次比较即可找到这两个数字.
需要注意的是上面的代码给你值的最小和最大的,但它不会告诉你提供的每个价值数原来的"指数".目前尚不清楚它是否真的有必要.您提供的代码示例实现了它,您的问题文本没有说明这一点.在任何情况下,更新上述代码以使其"跟踪"数字的来源并不困难.
这太简单了,因为数字是 a、b、c、d:
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
biggest = max (max(a,b), max(c,d))
smallest = min (min(a,b), min(c,d))
Run Code Online (Sandbox Code Playgroud)
在这里,没有 if 语句,没有函数(尽管后者是我听说过的最愚蠢且对专家有害的要求)。