cari

Rumah  >  Soal Jawab  >  teks badan

c++ - 想要将一组乱序的数排列,在插入一组数,使其变为升序数列。

现在每次输出都会少掉最大项,还会多出来一些0,请问如何解决?

# include<cstdio>
# include<cstdlib>

long long int a[1000];
long long int c[1000];

void sort(long long int as[],long long int left,long long int right)
{  
    if(left<right)  
    {  
    int i = left;  
    int j = right;  
    int x = as[i];  
  
    while(i<j)  
    {  
        while(i<j&&as[j]>x)  
            j--;  
        if(i<j){  
            as[i] = as[j];  
            i++;  
        }  
        while(i<j&&as[i]<x)  
            i++;  
        if(i<j){  
         as[j] = as[i];  
         j--;  
        }  
    }  
    as[i] = x;  
    sort(as,left, i-1);  
    sort(as,i+1, right);  
    }  
}  

void addsort(long long int num)
{
    printf("\aPlease input the number of the integers you want to add to the origin array:\n");
    long long int addnum,i,j,k;
    scanf("%lld",&addnum);
    long long int add[10000];
    printf("\aPlease input the integers you want to add one by one:\n");
    for (i=1;i<=addnum;i++)
    {
        scanf("%lld",&add[i]);
    }
    if (addnum > 1)
    {
        sort(add,1,addnum);
    }
    
    long long int addposition = 1;
    long long int aposition = 1;
    long long int totalnum;
    totalnum = num + addnum;
    
    printf("The final array is:\n");
    
    for (i=1;i<=totalnum-5;i++)
    {
        if (a[aposition] > add[addposition])
        {
            printf("%lld ",add[addposition]);
            addposition++;
        }
        if (a[aposition] == add[addposition])
        {
            printf("%lld %lld ",add[addposition],add[addposition]);
            i++;
            aposition++;
            addposition++;
        }
        if (a[aposition] < add[addposition])
        {
            printf("%lld ",a[aposition]);
            aposition++; 
        } 
    }
    printf("\n"); 
    return;
} 

int main()
{
    long long int i,j,num;
    printf("\aPlease input the number of the integers in the origin arrray:\n");
    scanf("%lld",&num);
    printf("\aPlease input the integers in the origin array one by one:\n");
    for (i=1;i<=num;i++)
    {
        scanf("%lld",&a[i]);
    }
    sort(a,1,num);
    addsort(num);
    system("pause");
    return 0;
 }
PHP中文网PHP中文网2769 hari yang lalu411

membalas semua(1)saya akan balas

  • ringa_lee

    ringa_lee2017-04-17 14:50:42

    addsort中第二个for改成下面,逻辑错态度,自己对比代码吧

    for (i=1;i<=totalnum;i++)
    {
        if (aposition > num)
        {
            printf("%lld ",add[addposition]);
            addposition++;
        } else if (addposition > addnum)
        {
            printf("%lld ",a[aposition]);
            aposition++;
        } else if (a[aposition] > add[addposition])
        {
            printf("%lld ",add[addposition]);
            addposition++;
    
        } else if (a[aposition] == add[addposition])
        {
            printf("%lld %lld ",a[aposition],add[addposition]);
            i++;
            aposition++;
            addposition++;
    
        } else if (a[aposition] < add[addposition])
        {
            printf("%lld ",a[aposition]);
            aposition++; 
        }
    }

    balas
    0
  • Batalbalas