求C中最大和最小的整数

发布于 2024-10-26 23:22:01 字数 974 浏览 1 评论 0原文

正如我在另一个问题中提到的,我一直在 KN King 的 C 编程:现代方法 (2ndEdn) 中自学 C。

我很喜欢它,但我希望在适当的情况下在这里发布奇怪的问题以寻求建议,因为不幸的是我没有导师,有些问题提出了更多问题,然后他们回答了!

我正在解决一个问题,要求我编写一个程序,查找用户输入的四个整数中的最大和最小......我想出了一种方法来找到最大的,但对于我的生活来说可以不知道如何取出最小的。该问题表明四个 if 语句就足够了。数学不是我的强项,我将不胜感激任何建议!

#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;

}

我尽量保持简单,因为我只读到了 27 章中的第 5 章!

干杯 安德鲁

As I mentioned in another question I've been teaching myself C out of of K.N King's C Programming: A Modern Approach (2ndEdn).

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. 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!

Cheers
Andrew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

昇り龍 2024-11-02 23:22:01
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);

swap() 函数的实现留作练习。

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.

梦行七里 2024-11-02 23:22:01

另一种方式是这样的:

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);  

不需要一个 if 语句;)

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 ;)

逐鹿 2024-11-02 23:22:01

在本章的上下文中:

  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;
  }

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;
  }
開玄 2024-11-02 23:22:01

我有同一本书,我承认,那个程序让我很头疼。
对于初学者程序员来说,这有点棘手。

首先,比较第一对整数(代码中的 a 和 b),并将本地最小值和最大值存储在某处。对第二对做同样的事情。然后比较局部最小值以获得全局最小值,并对最大值做同样的事情。不超过四个如果。

#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;
}

有更好的方法来解决这个问题,其中一些方法在这里展示,但本书将在后面的章节中介绍它们。

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. 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.

#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.

安静被遗忘 2024-11-02 23:22:01

我也在阅读 KN King 的“C 编程:现代方法,第二版”。以下是我对该线程原始问题的解决方案。

请注意,我仅使用本书第 5 章中介绍的 C 概念。最初的编程问题来自第 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 }

I'm going through K.N. King's "C Programming: A Modern Approach, Second Edition" too. 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. The original programming problem comes from Chapter 5, Programming Problem 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 }
久夏青 2024-11-02 23:22:01
#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);
}
#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);
}
A君 2024-11-02 23:22:01
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));
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));
白龙吟 2024-11-02 23:22:01

我设法用少于 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);
}

但是我不知道我是否在做正确的事情。至少我认为这会对某人有所帮助:)

I managed to solve this problem in even fewer than 4 if statements, here is my solution:

#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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文