【问题标题】:How can I make a program to check if the array is in descending , ascending order or none of them?如何制作一个程序来检查数组是降序、升序还是没有?
【发布时间】:2018-12-18 00:15:46
【问题描述】:

此代码仅允许用户填充数组:
它应该在不使用任何排序功能的情况下检查它是按降序还是升序排列,或者可能都不是。但是它不能正常工作,因为它既不是升序也不是降序时不排序

 #include <stdio.h>
int main ()
{
    int array[10];
    int i;
    int c;
    int d;

    printf("Enter the element of array:\n");
    for(i=0; i<10; i++)
    {
        scanf("%d",&array[i]);
    }



    for(i=0; i<10; i++)
    {

        printf("%d\n",array[i]);
    }

    for(i=0; i<9; i++)
    {
        if(array[i]<array[i+1])
        {
            c=1;
        }
        else if(array[i]>array[i+1])
        {
            d=1;
        }
    }

    if(c==1)
    {
        printf("ASCENDING");
    }
    else if(d==1)
    {
        printf("DESCENDING");
    }
    else 
    {
        printf("NONE");
    }
    return 0;

}

【问题讨论】:

  • 从 0 到 N-2 遍历数组,检查下一个元素是否大于或等于当前元素。如果不是,则不排序。与检查小于或等于降序相同。
  • 对于 0 到 last_index - 1 之间的所有 i,升序表示 [i] &lt;= [i+1]。降序与 &gt;= 类似。
  • 您在循环中正确地进行了测试,但您需要进行一些更改才能正常工作。首先,您没有初始化cd,因此它们可以包含任何内容。特别是,如果其中任何一个碰巧持有1,您就会面临这种“错误警报”的风险。其次,它只需要一个元素实例,以将cd 设置为1。所以,首先初始化它们。这决定了它们的初始值的含义,与 1 的含义。然后修改循环中的逻辑或最后的最终测试以获得您真正想要的行为。
  • 如果你想要简单的解决方案,复制数组,qsort复制升序,然后memcmp原始,qsort复制降序,memcmp原始,否则两者都不是。

标签: c arrays sorting


【解决方案1】:
#include <stdio.h>

#define ASND 0
#define DSND 1

int main ()
{
    int a[10];
    int i = 0;
    int order;


    printf("Enter the element of array:\n");
    for(i=0; i<10; i++) {
        scanf("%d",&a[i]);
    }

    for(i=0; i<10; i++) {
        printf("%d\n",a[i]);
    }

    for(i=1;i<10;i++){
        if(a[i-1] < a[i]) {
            order = ASND;
            break;
        }
        if(a[i-1] > a[i]) {
            order = DSND;
            break;
        }
    }
    if(i==10) {
        printf("all elements are same\n");
        return 0;
    }
    if(order == ASND) {
        for(i=1;i<10;i++) {
            if(a[i-1] > a[i]) {
                printf("no order\n");
                return 0;
            }
        }
        printf("ascending order\n");
        return 0;
   }

    for(i=1;i<10;i++) {
        if(a[i-1] < a[i]) {
            printf("no order\n");
            return 0;
        }
    }
    printf("descending order\n");
    return 0;
}

【讨论】:

    【解决方案2】:

    这个很简单,可读性很强。我认为它可以很好地完成工作!只需要一个循环来检查它,总结一个计数器升序或降序,然后检查!

    #include <stdio.h>
    
    int main ()
    {
        int a[10];
        int i = 0;
        int order;
    
    
        printf("Enter the element of array:\n");
        for(i=0; i<10; i++) {
            scanf("%d",&a[i]);
        }
    
        for(i=0; i<10; i++) {
            printf("%d\n",a[i]);
        }
        int ascendingCount=0;
        int descendingCount=0;
        int num1=a[0];
        for(int i=1;i<10;i++){
            if(a[i]>=num1){
                num1=a[i];
                ascendingCount++;
            }
            else if(a[i]<=num1){
                num1=a[i];
                descendingCount++;
            }
    
        }
        if(ascendingCount==9) printf("it is ascendingCount");
        else if(descendingCount==9) printf("it is descending");
        else printf("its nothing");
    
    
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      当你打破了升序或降序规则时,你需要跳出循环,这样你就不会重置你使用的升序或降序规则变量。

      #include <stdio.h>
      int main ()
      {
          int array[10];
          int i = 0;
          int c = 0;
          int d = 0;
      
          printf("Enter the element of array:\n");
          for(i=0; i<10; i++)
          {
              scanf("%d",&array[i]);
          }
      
      
      
          for(i=0; i<10; i++)
          {
      
              printf("%d\n",array[i]);
          }
      
          for(i=0; i<9; i++)
          {
              if(array[i]<array[i+1])
              {
                  c=1;
              }
              else if(array[i]>array[i+1])
              {
                  d=1;
              }
      
              // Run of same value
              else if (d==0 && c==0)
              {
                  continue;
              }
      
              // Can't be both ascending and descending
              if (d == 1 && c == 1)
              {
                  d = 0;
                  c = 0;
                  break;
              }
          }
      
          if(c==1)
          {
              printf("ASCENDING");
          }
          else if(d==1)
          {
              printf("DESCENDING");
          }
          else 
          {
              printf("NONE");
          }
          return 0;
      
      }
      

      【讨论】:

        猜你喜欢
        • 2018-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-20
        • 2022-01-10
        • 2020-12-04
        • 1970-01-01
        相关资源
        最近更新 更多