| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 466 人关注过本帖
标题:这个程序如何编写?
只看楼主 加入收藏
纯情程序猿
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2023-6-1
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
这个程序如何编写?
在数组a和数组b中分别放入n和m个由小到大的有序整数,程序把两个数组中的数按由小到大的顺序归并到数组c中。
搜索更多相关主题的帖子: 数组 顺序 归并 编写 整数 
2023-06-01 23:24
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
C++的做法
程序代码:
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;

int main( void )
{
    int a[] = { 2,3,4, 7,8,9 };
    int b[] = { 0,1, 5,6, 10 };
    int c[ size(a) + size(b) ];

    ranges::merge( a, b, c );
    ranges::copy( c, std::ostream_iterator<decltype(*c)>(cout," ") );
}


如果用C语言,或不允许用标准模板库的话
程序代码:
#include <stdio.h>

void merge( const int* first1, const int* last1, const int* first2, const int* last2, int* output )
{
    for( ; ; )
    {
        if( first1!=last1 && (first2==last2 || *first1<=*first2) )
            *output++ = *first1++;
        else if( first2!=last2 && (first1==last1 || *first2<*first1) )
            *output++ = *first2++;
        else
            break;
    }
}

int main( void )
{
    int a[] = { 2,3,4, 7,8,9 };
    int b[] = { 0,1, 5,6, 10 };
    int c[ sizeof(a)/sizeof(*a) + sizeof(b)/sizeof(*b) ];

    merge( a, a+sizeof(a)/sizeof(*a), b, b+sizeof(b)/sizeof(*b), c );
    for( size_t i=0; i!=sizeof(c)/sizeof(*c); ++i )
        printf( "%d ", c[i] );
}
2023-06-02 08:56
快速回复:这个程序如何编写?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015022 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved