C语言 c语言如何判断给定数组是否按降序排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29713194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:52:16  来源:igfitidea点击:

How to find whether the given array is sorted in descending order in c programming

carrayssorting

提问by Anish Kumar

I need to find out whether the given array is sorted in descending order or not...
I got the output but in portal it shows as Wrong Answer.

我需要找出给定的数组是否按降序排序...
我得到了输出,但在门户中它显示为错误答案。

This is my code.

这是我的代码。

#include<stdio.h>
int main()
{
   int n,a[15],i,k=0;
   scanf("%d",&n);
   for(i=1;i<=n;i++)
   { scanf("%d",&a[i]);}
   for(i=1;i<=n;i++)
   {
      if(a[i]<a[i+1])
         k++;
   }
   if(k==0)
      printf("yes");
   else
       printf("no");
   return 0;
}

Help me to figure it out...

帮我想办法...

回答by Arun A S

Arrays are indexed from 0to size - 1. So if you have

数组的索引从0size - 1。所以如果你有

int array[15];

the elements are a[0]to a[14]. But in your code, you are starting from a[1]to a[15]and might try to access a[15], which is unsafe memory and will cause problems.

的元素是a[0]a[14]。但是在您的代码中,您从a[1]to开始a[15]并可能尝试访问a[15],这是不安全的内存并会导致问题。

So you should first change your for loop.

所以你应该首先改变你的 for 循环。

You should change it to

你应该把它改成

   for( i = 0 ; i < n ; i++ )
   { 
     scanf("%d",&a[i]);
   }
   for( i = 0 ; i < n - 1 ; i++ )
   {
      if( a[i] < a[i+1] )
      {
         k++;
         break;
      }
   }

In the second for loop, you should loop till i < n - 1because otherwise, in

在第二个 for 循环中,你应该循环直到i < n - 1因为否则,在

 if(a[i]<a[i+1])

when i = n, you will try to access the n + 1th with element with a[i+1], which might be out of bounds.

当 时i = n,您将尝试访问n + 1带有元素的th a[i+1],这可能会越界。

You can also just exit from the loop once you find out that the array is not in descending order to save time.

您也可以在发现数组不是按降序排列时退出循环以节省时间。

You must also be certain that a[15]is enough to store all the values ( that is, the number of values given as input should not exceed 15, check the problem statement to ensure this )

您还必须确定a[15]足以存储所有值(即作为输入给出的值数量不应超过 15,请检查问题陈述以确保这一点)

回答by Himanshu

you should start array index from 0in both for loop.

您应该从0两个 for 循环中开始数组索引。

Use this

用这个

for(i=0;i<n;i++)

in second for loop you should use

在第二个 for 循环中你应该使用

for(i=0;i<n-1;i++) // you need to compare up to second last element with last, so run loop upto `i<n-1`

for example if you enter n = 5loop will work for 0to 4

例如,如果您输入n = 5循环将适用04

It is good to use break;if value of kincreases

break;如果值k增加,最好使用

for(i=1;i<=n;i++)
{
  if(a[i]<a[i+1]){
     k++;
     break; // if k increments break the loop.
   }
}

回答by Vlad from Moscow

Here you are.:)

这个给你。:)

#include <stdio.h>

#define N   15

int main(void) 
{
    int a[N];
    int i, n;

    printf( "Enter number of elements in the array (not greater than %d: ", N );
    scanf( "%d", &n );

    if ( N < n ) n = N;
    printf( "Enter %d elements of the array: ", n );

    for ( i = 0; i < n; i++ ) scanf( "%d", &a[i] );

    i = 0;

    while ( i++ < n && !( a[i-1] < a[i] ) );

    if ( i == n ) puts( "The array is sorted in the descending order" );
    else puts( "The array is not sorted in the descending order" );

    return 0;
}

The program output might look like

程序输出可能看起来像

Enter number of elements in the array (not greater than 15: 15
Enter 15 elements of the array: 10 10 9 9 9 8 7 6 5 5 4 3 2 1 0
The array is sorted in the descending order

As for you code then this loop

至于你的代码然后这个循环

for(i=1;i<=n;i++)
{
   if(a[i]<a[i+1])
      k++;
}

is unsafe because using index i+1in expression a[i+1]you can access memory beyond the array. Also you have to check whether the entered value of n is less than 15 that is the size of the array because you use the range of indices [0, n]. And in the first loop the index has to start from 0. Otherwise the first element of the array will not be initialized.

是不安全的,因为i+1在表达式中使用索引a[i+1]可以访问数组之外​​的内存。此外,您必须检查输入的 n 值是否小于数组大小的 15,因为您使用了索引范围[0, n]。在第一个循环中,索引必须从 0 开始。否则数组的第一个元素将不会被初始化。

回答by leicar

First, you define array a[15]:

首先,定义数组 a[15]:

int n,a[15],i,k=0;

and you use variable n to control its size

并且您使用变量 n 来控制其大小

scanf("%d",&n);
for(i=1;i<=n;i++)
{ scanf("%d",&a[i]);}

then you need to keep n <= 15.

那么你需要保持 n <= 15。

p.s., array starts with '1' in Matlab but '0' in C.

ps,数组在Matlab中以'1'开头,在C中以'0'开头。

回答by NUMBART

I can't agree less about what others have said regarding array indexing, the problem in not knowing whether a[i+1] exists or not, and using of break. I would add though when all the numbers in this input array are equal it will still pass as decreasing in your code. If that is a problem you can add another if statement checking whether at all it decreases anywhere and use another variable say change for checking whether it gets satisfied.

对于其他人对数组索引、不知道 a[i+1] 是否存在以及使用 break 的问题,我完全同意。我想补充一点,当这个输入数组中的所有数字都相等时,它仍然会在你的代码中作为递减传递。如果这是一个问题,您可以添加另一个 if 语句,检查它是否在任何地方减少,并使用另一个变量 say change 来检查它是否得到满足。

int change = 0;
for(i=0;i<n;i++)
{
    if(a[i]<a[i+1])
        k++;
    if(a[i]>a[i+1])
        change++;
}
if(k==0 && change > 1)
    printf("yes");
else
    printf("no");

Check with the problem statement whether it allows such cases as descending. If it doesn't go with this otherwise its fine.

检查问题陈述是否允许降序等情况。如果它不符合这个,否则它很好。