简体   繁体   English

如何在 C 的数组中反转非负整数的运行?

[英]How do I reverse runs of nonnegative integers in an array in C?

I need to create a random array in C, find nonnegative values in it, and when there are at least 2 in a row, reverse them.我需要在 C 中创建一个随机数组,在其中找到非负值,当连续至少有 2 个时,将它们反转。 For example, if I have the random array 5 6 -7 -8 9 -4 7 8 2 -2 I need to get 6 5 -7 -8 9 -4 2 8 7 -2 .例如,如果我有随机数组5 6 -7 -8 9 -4 7 8 2 -2我需要得到6 5 -7 -8 9 -4 2 8 7 -2 This is what I've tried so far:这是我迄今为止尝试过的:

    #include <stdio.h>
#include <stdlib.h>
 int ARRAY[100];
int main(void) {
 int i;
 int nn; 
 int temp = ARRAY[i];
 rand();
 
 for (i=0; i<100; ARRAY[i++]=rand()%100-50 );

 printf("FIRST ARRAY:\n");
 for (i=0; i<100; i++)
    printf("%3d ",ARRAY[i]);
 putchar('\n');
 putchar('\n');



for(i=0; i<100; i++){
    if (ARRAY[i]<0) {
        if(!nn) {
            nn = 1;

        }
    }

        else {
            temp=ARRAY[i];
            ARRAY[i] = ARRAY[nn - 1];
            ARRAY[nn - 1] = temp;


            }
    }







 printf("Result:\n");

 putchar('\n');
 for (i=0; i<100; printf("%3d ",ARRAY[i++]));
 putchar('\n');
 return 0;
}

But had no luck with it.但没有运气。

For starters there is no any need to declare the array globally对于初学者来说,不需要全局声明数组

int ARRAY[100];

Also never use magic numbers like 100 .也永远不要使用像100这样的幻数。 Use named constants.使用命名常量。

And it is a bad style of programming to name variables except constants with all upper case letters.除了所有大写字母的常量之外,命名变量是一种糟糕的编程风格。

You can write a separate function as it is shown in the demonstrative program below.您可以编写一个单独的 function,如下面的演示程序所示。

#include <stdio.h>

void reverse_non_negative( int a[], size_t n )
{
    for (size_t i = 0; i < n; )
    {
        while (i < n && a[i] < 0) i++;

        size_t j = i;

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

        for (size_t k = 0; k < ( i - j ) / 2; k++)
        {
            int tmp = a[j + k];
            a[j + k] = a[i - k - 1];
            a[i - k - 1] = tmp;
        }
    }
}

int main()
{
    int a[] = { 5, 6, -7, -8, 9, -4, 7, 8, 2, -2 };
    const size_t N = sizeof( a ) / sizeof( *a );

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

    reverse_non_negative( a, N );

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

The program output is程序 output 是

5 6 -7 -8 9 -4 7 8 2 -2
6 5 -7 -8 9 -4 2 8 7 -2

A more general approach can look the following way更通用的方法可以如下所示

#include <stdio.h>

void reverse_sub_ranges( int a[], size_t n, int predicate( int ) )
{
    for (size_t i = 0; i < n; )
    {
        while (i < n && !predicate( a[i] )) i++;

        size_t j = i;

        while (i < n && predicate( a[i] )) i++;

        for (size_t k = 0; k < ( i - j ) / 2; k++)
        {
            int tmp = a[j + k];
            a[j + k] = a[i - k - 1];
            a[i - k - 1] = tmp;
        }
    }
}

int non_negative( int value )
{
    return !( value < 0 );
}

int main()
{
    int a[] = { 5, 6, -7, -8, 9, -4, 7, 8, 2, -2 };
    const size_t N = sizeof( a ) / sizeof( *a );

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

    reverse_sub_ranges( a, N, non_negative );

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

The program output is the same as shown above that is程序 output 与上图相同,即

5 6 -7 -8 9 -4 7 8 2 -2
6 5 -7 -8 9 -4 2 8 7 -2

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM