简体   繁体   English

我正在尝试使用if语句以升序方式排列三个3号,但这不适用于第三个数字。 该怎么办?

[英]I am trying to arrange three 3 numbers in ascending manner using if statement, but this doesn't work for the third number. What can be done about it?

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

If I put 8,6,3, the output comes 6,8,3. 如果我输入8,6,3,则输出为6,8,3。 It doesn't change the last number. 它不会更改最后一个数字。 I am trying to arrange three 3 numbers in ascending manner using if statement, but this doesn't work for the third number. 我正在尝试使用if语句以升序方式排列三个3号,但这不适用于第三个数字。 What can be done about it? 该怎么办?

It easiest if you first find the smallest, then make sure the remaining two are correct : 最简单的方法是先找到最小的,然后确保其余两个正确:

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

    int ret = scanf("%d %d %d", &a, &b, &c);
    if (ret != 3) {
        printf("scanf() error\n");
        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;
}

You need to use if instead of else if as you want to compare a with b, b with c and a with c (the three and not only one of them). else if要比较a与b,b与c和a与c(这三个,而不仅仅是其中一个), if需要使用if而不是else if Moreover, as you are moving the numbers you have to take into account where they are moved for the last comparison. 此外,在移动数字时,必须考虑它们的位置,以便进行最后的比较。 And your third condition was wrong. 而你的第三个条件是错误的。 So this should be what you are trying to do: 因此,这应该是您要尝试执行的操作:

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

I think you have misunderstood the concept of if else if structure, In your case it is not working for third number because the execution will reach to else if part only when the if condition is false. 我认为您误解了if else if结构的概念,在您的情况下,它不适用于第三个数字,因为仅当if条件为false时,if部分的执行才能到达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

you may need to go over the concept of if else structure once again 您可能需要再次探讨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; 
}

The easiest way is to use an array instead of three individual variables. 最简单的方法是使用数组而不是三个单独的变量。 Then use qsort for getting the input sorted. 然后使用qsort对输入进行排序。

Like: 喜欢:

#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 %d\n", 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;
}

you are comparing using else if , if any one condition satisfies it won't execute the other else condition. 您正在比较使用else if ,如果一个条件满足则不会执行另一个else条件。

暂无
暂无

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

相关问题 使用if和swap语句按升序排列数字 - Arrange numbers in ascending order using if and swap statement 我确保用户输入的数字是有效数字。 如果人们不输入数字,if 语句会以某种方式起作用吗? 这是如何工作的? - I’m making sure the user enters a number a valid number. If persons doesn’t enter a number, the if statement somehow works? How is this working? 我正在尝试创建一个数组并用 1 到 10 的数字填充它。为什么它不起作用? - I am trying to create an array and fill it with numbers from 1 to 10. Why it doesn't work? 我正在尝试按升序将数字添加到链接列表中。 我该如何解决才能正常工作? 这是我的代码: - I'm trying to add numbers into a linked list in ascending order. what can I fix so it'll work? Here's my code: 我正在尝试扫描一个字符串并将其放入 function,但它不起作用 - I am trying to scanf a string and put it in a function, but it doesn't work 将给定数字升序排列 - Arrange given numbers in ascending order 如何按升序排列结构数组中的结构? - How can I arrange the structs in an array of structs in an ascending order? 我是否以定义的方式使用局部变量? - Am I using local variables in a defined manner? 如果声明不起作用? - if statement doesn't work? 试图让这个 if 语句起作用,但我似乎无法将变量与数组中的另一个变量进行比较 - Trying to get this if statement to work but I can't seem to compare on variable to another that is in and array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM