我正在尝试使用 if 语句以升序排列三个 3 数字,但这不适用于第三个数字。对此,我们能做些什么呢?


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

如果我输入 8,6,3,则输出为 6,8,3。它不会更改最后一个数字。我正在尝试使用 if 语句以升序排列三个 3 数字,但这不适用于第三个数字。对此,我们能做些什么呢?

最简单的方法是先找到最小的,然后确保其余两个是正确的:

int main()
{
    int a, b, c, temp;
    int ret = scanf("%d %d %d", &a, &b, &c);
    if (ret != 3) {
        printf("scanf() errorn");
        exit(1);
    }
    // get smallest into a
    if ((b < a) && (b < c)) {
        temp = a;
        a = b;
        b = temp;
    } else if ((c < a) && (c < b)) {
        temp = a;
        a = c;
        c = temp;
    }
    // a is smallest, check b and c
    if (c < b) {
        temp = b;
        b = c;
        c = temp;
    }
    printf("%d %d %d", a, b, c);
    return 0;
}
您需要

使用 if 而不是 else if,因为您想将 a 与 b、b 与 c 和 a 与 c 进行比较(这三个,而不仅仅是其中之一(。此外,当您移动数字时,您必须考虑它们在上次比较中的位置。你的第三个条件是错误的。所以这应该是您要做的:

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

我认为您误解了 if else if 结构的概念,在您的情况下,它不适用于第三个数字,因为只有在 if 条件为 false 时,执行才会到达 else 如果部分。

#include <stdio.h> 
int main()
{
int a,b,c,temp;
scanf("%d %d %d",&a,&b,&c);
if(a>b)  //evaluates to true.
{
    temp=a;
    a=b;
    b=temp;
}
else if(b>c)  // not able to execute.
{
    temp=b;
    b=c;
    c=temp;
}
else if(c>a)  // not able to execute
{
    temp=c;
    c=a;
    a=temp;
}
printf("%d %d %d",a,b,c);
return 0;
}
a = 8
b = 6
c = 3
checking a>b evaluates to true hence swapped
now:
a = 6  // your output
b = 8
c = 3

您可能需要再次复习 if else 结构的概念

#include<stdio.h>
int main()
{
    int a ,b,c;
    printf("Enter the number : n");
    scanf("%d %d %d",&a,&b,&c);
    if((a>b)&&(a>c))
    {
        if(b>c)
           printf("%d %d %d",a,b,c);
        else
           printf("%d %d %d",a ,c,b);
     }
     else if((b>c)&&(b>a))
     {
         if(c>a)
            printf("%d %d %d",b,c,a);
         else
            printf("%d %d %d",b,a,c);
     }
     else if((c>a)&&(c>b))
     {
         if(a>b)
           printf("%d %d %d",c,a,b);
         else
            printf("%d %d %d",c,b,a);
     }
    return 0; 
}

最简单的方法是使用数组而不是三个单独的变量。然后使用 qsort 对输入进行排序。

喜欢:

#include <stdio.h>
#include <stdlib.h>
// Compare function for qsort
int cmp(const void *p1, const void *p2)
{
  if (*(int*)p1 < *(int*)p2) return -1;
  if (*(int*)p2 < *(int*)p1) return 1;
  return 0;
}
int main()
{
    int arr[3];
    if (scanf("%d %d %d", &arr[0], &arr[1], &arr[2]) != 3) exit(1);
    // Sort the input
    qsort(arr, sizeof(arr)/sizeof(int), sizeof(int), cmp);
    printf("%d %d %dn", arr[0], arr[1], arr[2]);
    return 0;
}
#include <stdio.h>
int main()
{
    int a,b,c,temp,min;
    scanf("%d %d %d",&a,&b,&c);
    if(a>b)
    {
        temp=a;
        a=b;
        b=temp;
    }
    if(c<a)
    {
        min=c;
        c=b;
        b=a;
        a=min;
    }
    else if(c>a && b<c) {
        min=c;
         c=b;
         b=min;
    }
    printf("%d %d %d",a,b,c);
    return 0;
}

您正在使用else if进行比较,如果任何一个条件满足,则不会执行另一个else条件。

最新更新