gpt4 book ai didi

c - 我怎样才能编写一个程序来检查数组是否按降序、升序或没有?

转载 作者:行者123 更新时间:2023-11-30 14:43:40 28 4
gpt4 key购买 nike

此代码仅允许用户填充数组:
它应该检查它是否按降序或升序排列,或者可能没有任何一个,而不使用任何排序功能。但它无法正常工作,因为它既不升序也不降序时不排序

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

}

最佳答案

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

关于c - 我怎样才能编写一个程序来检查数组是否按降序、升序或没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53824746/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com