在找到数组是否已排序时发生错误。

huangapple go评论66阅读模式
英文:

there is an error in finding the array is sorted or not

问题

#include <stdio.h>;

int ret(int[], int);

int main() {
    int n;

    scanf("%d", &n);

    int i;
    int a[n];

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

    ret(a, n);

    return 0;
}

int ret(int a[], int n) {
    int i, c1 = 0, c2 = 0;

    if (a[n - 1] >= a[n - 2] && a[0] <= a[1]) {
        for (i = 0; i < n - 1; i++) {
            if (a[i] <= a[i + 1]) {
                c1++;
            }
        }

        if (c1 == n - 1) {
            puts("已排序");
        } else {
            puts("未排序");
        }
    } else if (a[n - 1] <= a[n - 2] && a[0] >= a[1]) {
        for (i = 0; i < n - 1; i++) {
            if (a[i] >= a[i + 1]) {
                c2++;
            }
        }

        if (c2 == n - 1) {
            puts("已排序");
        } else {
            puts("未排序");
        }
    }

    return 0;
}

这是您提供的C代码的翻译,只包含代码部分,没有其他内容。此代码用于检查数组是否已排序。如果数组已排序,它将打印"已排序",否则将打印"未排序"。如果您在运行代码时遇到问题,请提供更多详细信息,以便我可以帮助您解决问题。

英文:
#include &lt;stdio.h&gt;

int ret(int[], int);

int main() {
    int n;

    scanf(&quot;%d&quot;, &amp; n);

    int i;
    int a[n];

    for (i = 0; i &lt; n; i++) {
        scanf(&quot;%d&quot;, &amp; a[i]);
    }
    
    ret(a, n);

    return 0;
}

int ret(int a[], int n) {
    int i, c1 = 0, c2 = 0;

    if (a[n - 1] &gt;= a[n - 2] &amp;&amp; a[0] &lt;= a[1]) {
        for (i = 0; i &lt; n - 1; i++) {
            if (a[i] &lt;= a[i + 1]) {
                c1++;
            }
        }

        if (c1 == n - 1) {
            puts(&quot;SORTED&quot;);
        } else {
            puts(&quot;NOTSORTED&quot;);
        }
    } else if (a[n - 1] &lt;= a[n - 2] &amp;&amp; a[0] &gt;= a[1]) {
        for (i = 0; i &lt; n - 1; i++) {
            if (a[i] &gt;= a[i + 1]) {
                c2++;
            }
        }

        if (c2 == n - 1) {
            puts(&quot;SORTED&quot;);
        } else {
            puts(&quot;NOT SORTED&quot;);
        }
    }
    
    return 0;
}

I want to check whether the array is sorted or not, but I can't get the output. Sometimes it gives the correct output, but sometimes it doesn't give any output. Please check the code.

答案1

得分: 1

如果这个if语句不触发if(a[n-1]>=a[n-2]&&a[0]<=a[1]),而且这个也不触发
else if(a[n-1]<=a[n-2] &&a[0]>=a[1]){,那么没有任何行代码可以产生任何输出。

英文:

If this if statement doesn't fire if(a[n-1]&gt;=a[n-2]&amp;&amp;a[0]&lt;=a[1]) and this one doesn't either
else if(a[n-1]&lt;=a[n-2] &amp;&amp;a[0]&gt;=a[1]){, then there is no line of code that can produce any output.

答案2

得分: 0

如@DavidSchwartz指出,对数组的两端进行采样不考虑整个数组的方向,甚至可能排除任何“分析”。

除此之外,int a[1] = { 42 }; 是一个完全有效的数组,会导致您的代码进入“未定义行为”(访问不属于数组的内存)。

尝试以下操作:

#include <stdio.h>

void ret( int a[], int n ) {
	int cUP = 0, cDN = 0; // 计算上升和下降变化的总数。

	for( int i = 1; i < n; i++ ) { // 注意:从1开始并向后检查
		cUP += ( a[ i-1 ] < a[ i ] ); // 上升
		cDN += ( a[ i-1 ] > a[ i ] ); // 下降
	}

    // 如果上升和下降都存在,则数组未排序。
	if( cUP && cDN )
		puts( "未排序" );
	else
		puts( "已排序" );
}

int main() {
	int a[] = { 1, 2, 3 };
	ret( a, sizeof a/sizeof a[0] );

	int b[] = { 3, 2, 1 };
	ret( b, sizeof b/sizeof b[0] );

	int c[] = { 3, 2, 5 };
	ret( c, sizeof c/sizeof c[0] );

	int d[] = { 3, 2, 2, 2, 1 };
	ret( d, sizeof d/sizeof d[0] );

	int e[] = { 2, 2, 2 };
	ret( e, sizeof e/sizeof e[0] );

	return 0;
}

实际上,循环可以提前退出,如下所示:

for( int i = 1; i < n && (!cUP || !cDN); i++ ) {

如果两个计数器都变为非零,则数组未排序,循环可以提前结束。

英文:

As pointed out by @DavidSchwartz, sampling both ends of the array does not account for the direction of the entire array and may even preclude any 'analysis'.

Apart from that, int a[1] = { 42 }; is a perfectly valid array that would lead your code into undefined behaviour (accessing memory that is not part of the array.)

Try the following:

#include &lt;stdio.h&gt; 

void ret( int a[], int n ) {
	int cUP = 0, cDN =0; // Tally up/down variations.

	for( int i = 1; i &lt; n; i++ ) { // NB: start at 1 and check backward
		cUP += ( a[ i-1 ] &lt; a[ i ] ); // going UP
		cDN += ( a[ i-1 ] &gt; a[ i ] ); // going DOWN
	}

    // If both up &amp; down found, array is not sorted.
	if( cUP &amp;&amp; cDN )
		puts( &quot;NOT SORTED&quot; );
	else
		puts( &quot;SORTED&quot; );
}

int main() {
	int a[] = { 1, 2, 3 };
	ret( a, sizeof a/sizeof a[0] );

	int b[] = { 3, 2, 1 };
	ret( b, sizeof b/sizeof b[0] );

	int c[] = { 3, 2, 5 };
	ret( c, sizeof c/sizeof c[0] );

	int d[] = { 3, 2, 2, 2, 1 };
	ret( d, sizeof d/sizeof d[0] );

	int e[] = { 2, 2, 2 };
	ret( e, sizeof e/sizeof e[0] );

	return 0;
}

In fact, the loop could bail out early with the following:

	for( int i = 1; i &lt; n &amp;&amp; (!cUP || !cDN); i++ ) {

If both counters become non-zero, the array is not sorted and the loop can finish early.

huangapple
  • 本文由 发表于 2023年3月7日 09:19:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定