编写一个程序,将三个整数作为输入并打印出第二大的数字

Write a program that will take three integers as input and will print the second largest number

我试过这个程序取 3 个整数并打印第二大数:

#include<stdio.h>
int main()
{
    int a,b,c,max2;
    printf("Enter 3 integers: ");
    scanf("%d%d%d",&a,&b,&c);
    max2=a;
    if(a>b){
        max2=b;
        printf("")
    }
    return 0;
}

现在我被困在这里了。我无法找到这段代码背后的逻辑。我能做什么?

这不是你能理解的逻辑,它不会给你正确的、预期的结果。

代码:

#include <stdio.h>

int main()
{
    int a, b, c;
    printf("Values: ");
    scanf("%d%d%d", &a, &b, &c);
    if(a>b && a>c)
    {
        if(b>c)
            printf("2nd largest: %d", b);
        else
            printf("2nd largest: %d", c);
    }
    else if(b>c && b>a)
    {
        if(c>a)
            printf("2nd largest: %d", c);
        else
            printf("2nd largest: %d", a);
    }
    else if(a>b)
            printf("2nd largest: %d", a);
        else
            printf("2nd largest: %d", b);
    return 0;
}

您应该比较所有三个变量以获得这些数字中的 2nd largest

输出:

Values: 32 31 12
2nd largest: 31

解释:

首先选择一个变量并将其与其他两个变量进行比较,例如if(a>b && a>c),如果它是true,则意味着alargest和任何一个两个变量 bc2nd largest,所以在 if(a>b && a>c) 块内部有一个比较 if(b>c),如果 trueb 是第二大的,否则 c 是第二大的。同样,比较其他两个变量是否最大。例如else if(b>c && b>a)else if(c>a && c>b).

一种方法是对这三个数字进行排序,然后打印中间的一个:

#include <stdio.h>

static inline void swap_if_out_of_order (int *p, int *q)
{
    if (*p > *q) {
        int t = *p;
        *p = *q;
        *q = t;
    }
}

int main (void)
{
    int a, b, c;

    printf("Enter three integers\n");
    if (scanf("%d%d%d", &a, &b, &c) == 3) {
        swap_if_out_of_order(&a, &b);
        swap_if_out_of_order(&b, &c);
        swap_if_out_of_order(&a, &b);
        printf("Second greatest: %d\n", b);
    }
}

或者,不排序,最多比较3次:

#include <stdio.h>

int main (void)
{
    int a, b, c, m;
    
    printf("Enter three integers\n");
    if (scanf("%d%d%d", &a, &b, &c) == 3) {
        if (a > b) {
            if      (b > c) m = b;
            else if (a > c) m = c;
            else            m = a;
        } else   if (a > c) m = a;
          else   if (b > c) m = c;
          else              m = b;
        printf("Second greatest: %d\n", m);
    }
}

或者,可能是使用最大和最小函数的最有效方式:

#include <stdio.h>

static inline int min (int x, int y) { return x < y ? x : y; }
static inline int max (int x, int y) { return x > y ? x : y; }

int main (void)
{
    int a, b, c;
    
    printf("Enter three integers\n");
    if (scanf("%d%d%d", &a, &b, &c) == 3)
        printf("Second greatest: %d\n", max(min(a, b), min(max(a, b), c)));
}

JS 递归:

function f(a,b,c) {
  if(a>=b && c<b) return b;
  if(a>b) return f(a,c,b);
   return f(b,a,c);
}

f(2,12,0) // 2

       #include <stdio.h>
        main()
       {
              int a,b,c;

               scanf("%d %d %d",&a,&b,&c);
               // largest //
               if(a>b&&a>c)
               printf("largest=%d",a);
               if(b>a&&b>c)
               printf("largest=%d",b);
               if(c>b&&c>a)
              printf("largest=%d",c);
             // second largest//
              if(a>b&&a<c)
             printf("\nscenond largest=%d",a);
            if(b>a&&b<c)
             printf("\nscenond largest=%d",b);
             if(c>a&&c<b)
           printf("\nscenond largest=%d",c);

       }


this will output the largest and the second largest number.