C语言用选择排序的方法合并有序数组

来源:互联网 发布:淘宝怎么发布虚拟产品 编辑:程序博客网 时间:2024/06/11 00:31

Problem A

合并有序数组(Merging sorted array)

时限:1000ms 内存限制:10000K 总时限:3000ms

描述:

给你两个有序且升序的数组,请你把它们合成一个升序数组并输出
Give you two ordered ascending array, you put them into one ascending array and output.

输入:

第一行为一个正整数n,n<=20 ;
第二行为n个数字,这n个数字用空格隔开
第三行为一个正整数m,m<=20 ;
第四行为M个数字,这m个数字用空格隔开
The first line is a positive integer n, n <= 20;
The second line are n numbers separated by space
The third is a positive integer m, m <= 20;
The fourth line are m numbers separated by space

输出:

输出合并后的数组,每个数字占一行,
Output the combined array, each number per line,

输入样例:

31 3 752 4 6 8 10

输出样例:

123467810

 

#include<stdio.h>
#include<string.h>
int main()
{
int i,x,j,n,m,k,t,a[21],b[21],c[41];
scanf("%d",&n);
for(i=0;i<n;i++)
{
 scanf("%d",&a[i]); 
    }
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
    scanf("%d",&b[i]);
    }
    k=m+n;
    for(i=0;i<k;i++)//合并两个数组
    {
    if(i<n)c[i]=a[i];
    if(i>=n)c[i]=b[i-n];
    }
    for(i=0;i<k-1;i++)//用选择排序的方法按从小到大的顺序给该数组排序
    {
    x=i;
    for(j=i+1;j<k;j++)
    {
    if(c[j]<c[x])x=j;
        }
        if(i!=x)
       {
         t=c[i];
        c[i]=c[x];
        c[x]=t;
       }
    }
    for(i=0;i<k;i++)
    {
    printf("%d\n",c[i]);
    }
    return 0;
    


Northwestern Polytechnical University
Copyright © 2007,Xiaolan.Lee
All Rights Reserved  

原创粉丝点击