简体   繁体   English

查找C中的最大和最小整数

[英]Finding the largest and smallest integers in C

As I mentioned in another question I've been teaching myself C out of of KN King's C Programming: A Modern Approach (2ndEdn). 正如我在另一个问题中提到的,我一直在用KN King's C Programming:A Modern Approach(2ndEdn)教自己C语言。

I'm enjoying it, but am hoping to post the odd question here for advice if appropriate because unfortunately I don't have a tutor and some bits raise more questions then they answer! 我很享受,但是如果合适的话,我希望在这里发出奇怪的问题以获得建议,因为不幸的是我没有导师,有些人提出了更多问题然后他们回答了!

I'm up to a question that asks me to write a program that finds the largest and smallest of four integers entered by the user... I've come up with a way to find the largest, but for the life of me can't work out how to get the smallest out. 我要问一个问题,要求我编写一个程序,找出用户输入的四个整数中最大和最小的...我想出办法找到最大的,但对于我的生活可以弄清楚如何让最小的出局。 The question says that four if statements should be sufficient. 问题是四个if语句应该足够了。 Math is not my forte, I'd appreciate any advice! 数学不是我的强项,我很感激任何建议!

#include <stdio.h>

int main(int argc, const char *argv[])
{

    int one, two, three, four;

    printf("Enter four integers: ");

    scanf("%d %d %d %d", &one, &two, &three, &four);

    if (four > three && four > two && four > one)
            printf("Largest: %d", four);
    else if (three > four && three > two && three > one)
            printf("Largest: %d", three);
    else if (two > three && two > four && two > one)
            printf("Largest: %d", two);
    else
            printf("Largest: %d", one);

    return 0;

}

I'm trying to keep it simple, as I'm only up to chapter 5 of 27! 我试图保持简单,因为我只有27章的第5章!

Cheers Andrew 干杯安德鲁

if (first > second)
    swap(&first, &second);
if (third > fourth)
    swap(&third, &fourth);
if (first > third)
    swap(&first, &third);
if (second > fourth)
    swap(&second, &fourth);

printf("Smallest: %d\n", first);
printf("Largest: %d\n", fourth);

The implementation of the swap() function is left as exercise. swap()函数的实现swap()练习。

another way would be as such: 另一种方式是这样的:

int one, two, three, four;  
//Assign values to the four variables;  
int largest, smallest;  
largest = max(max(max(one, two), three), four);  
smallest = min(min(min(one, two), three), four);  

Not a single if statement needed ;) 不需要单个if语句;)

Within the context of the chapter: 在本章的范围内:

  if (a > b) {
    max = a;
    min = b;
  } else {
    max = b;
    min = a;
  }
  if (c > d) {
    max2 = c;
    min2 = d;
  } else {
    max2 = d;
    min2 = c;
  }
  if (max < max2) {
    max = max2;
  }
  if (min > min2) {
    min = min2;
  }

I have that same book, and I'll admit it, that program gave me quite a headache. 我有同样的书,我承认,这个程序让我很头疼。 It's a little tricky for a beginner programmer. 这对初学程序员来说有点棘手。

First, you compare the first pair of integers (a and b in the code), and store local min and max somewhere. 首先,比较第一对整数(代码中的a和b),并将局部最小值和最大值存储在某处。 Do the same thing with the second pair. 对第二对做同样的事情。 Then compare local minimums to get the global minimum, and do the same thing with maximums. 然后比较局部最小值以获得全局最小值,并使用最大值执行相同的操作。 No more than four if's. 不超过四个if。

#include <stdio.h>

int main (void)
{
   int a, b, c, d, min1, max1, min2, max2, min, max;
   scanf ("%d %d %d %d", &a, &b, &c, &d);
   if (a > b) 
   {
      max1 = a;
      min1 = b;
   }
   else 
   {
      max1 = b; 
      min1 = a;
   }
   if (c > d) 
   {
      max2 = c;
      min2 = d;
   }
   else 
   {
      max2 = d;
      min2 = c;
   }
   if (min1 < min2) min = min1;
   else min = min2;
   if (max1 > max2) max = max1;
   else max = max2;
   printf ("%d %d", max, min);
   return 0;
}

There are better ways to solve this, some of them are shown here, but the book covers them in the later chapters. 有更好的方法可以解决这个问题,其中一些在这里展示,但本书将在后面的章节中介绍它们。

I'm going through KN King's "C Programming: A Modern Approach, Second Edition" too. 我也正在经历KN King的“C编程:现代方法,第二版”。 Below is my solution for this thread's original question. 以下是我对该主题原始问题的解决方案。

Note that I'm using only C concepts introduced up through Chapter 5 of the book. 请注意,我只使用了本书第5章介绍的C概念。 The original programming problem comes from Chapter 5, Programming Problem 7. 最初的编程问题来自第5章,编程问题7。

 21 #include <stdio.h>
 22 
 23 int main(void)
 24 {
 25     int i1, i2, i3, i4, large_1, small_1, large_2, small_2,
 26         largest, smallest;
 27 
 28     printf("\nEnter four integers: ");
 29     scanf("%d %d %d %d", &i1, &i2, &i3, &i4);
 30 
 31     if (i1 < i2) {
 32         small_1 = i1;
 33         large_1 = i2;
 34     } else {
 35         small_1 = i2;
 36         large_1 = i1;
 37     }
 38 
 39     if (i3 < i4) {
 40         small_2 = i3;
 41         large_2 = i4;
 42     } else {
 43         small_2 = i4;
 44         large_2 = i3;
 45     }
 46 
 47     if (large_1 < large_2)
 48         largest = large_2;
 49     else
 50         largest = large_1;
 51 
 52     if (small_1 < small_2)
 53         smallest = small_1;
 54     else
 55         smallest = small_2;
 56 
 57     printf("Largest: %d\n", largest);
 58     printf("Smallest: %d\n\n", smallest);
 59 
 60     return 0;
 61 }
#include <stdio.h>
/*
SOLUTION 1

int main(void) {
    int a1,a2,a3,a4,max,min,max1,min1,max2,min2;
    printf("Enter four integers : ");
    scanf("%d %d %d %d",&a1,&a2,&a3,&a4);
    if (a1 > a2) {
        max1 = a1;
        min1 = a2;
    } else {
        max1 = a2;
        min1 = a1;
    }
    if (a3 > a4) {
        max2 = a3;
        min2 = a4;
    } else {
        max2 = a4;
        min2 = a3;
    }
    if (max1 > max2)
        max = max1;
    else
        max = max2;
    if (min1 < min2)
        min = min1;
    else
        min = min2;
    printf("Largest : %d\n",max);
    printf("Smallest : %d\n",min);
}
*/

/*
SOLUTION 2
*/

int main(void) {
    int a1,a2,a3,a4;
    printf("Enter four integers : ");
    scanf("%d %d %d %d",&a1,&a2,&a3,&a4);
    if (a1 > a2) {
        int temp1 = a1; a1 = a2; a2 = temp1; // Swap the numbers (a1 to contain smallest number)
    }
    if (a3 > a4) {
        int temp2 = a3; a3 = a4; a4 = temp2; // Swap the numbers (a1 to contain smallest number)
    }
    if (a1 > a3) {
        int temp3 = a1; a1 = a3; a3 = temp3; // Swap the numbers (a1 to contain smallest number)
    }
    if (a2 > a4) {
        int temp4 = a2; a2 = a4; a4 = temp4; // Swap the numbers (a1 to contain smallest number)    
    }
    printf("Largest : %d\n",a4);
    printf("Smallest : %d\n",a1);
}

I managed to solve this problem in even fewer than 4 if statements, here is my solution: 我设法用甚至不到4个if语句来解决这个问题,这是我的解决方案:

#include<stdio.h>

int main(void){
    int no1, no2, no3, no4;
    int max1, max2, max3, min1, min2, min3;

    printf("Enter four integers:");
    scanf_s("%d %d %d %d", &no1, &no2, &no3, &no4);

    if(no1 > no2 || no1 < no2 && no3 > no4 || no3 < no4){
        no1 > no2 ? (max1=no1) : (max1=no2);
        no1 > no2 ? (min1=no2) : (min1=no1);
        no3 > no4 ? (max2=no3) : (max2=no4);
        no3 > no4 ? (min2=no4) : (min2=no3);
    }
    if(max1 > max2 || max1 < max2 && min1 > min2 || min1 < min2){
        max1 > max2 ? (max3=max1) : (max3=max2);
        min1 > min2 ? (min3=min2) : (min3=min1);
    }

    printf("The largest number is %d \n", max3);
    printf("The smallest number is %d \n", min3);
}

However I don't know if I am doing the right things. 但是我不知道我做的是不是做对了。 At least I think it will help someone :) 至少我认为它会帮助别人:)

printf("Largest: %d\n",(one>two ? one:two)>(three>four ? three:four)
                ? (one>two ? one:two):(three>four ? three:four));
printf("Smallest: %d",(one<two ? one:two)<(three<four ? three:four)
                ? (one<two ? one:two):(three<four ? three:four));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM