c++ - 想要将一组乱序的数排列,在插入一组数,使其变为升序数列。
PHP中文网
PHP中文网 2017-04-17 14:48:42
[C++讨论组]

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

# include
# include

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(leftx)  
            j--;  
        if(i 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中文网

认证0级讲师

全部回复(1)
ringa_lee

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++; 
    }
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号