【十二题解】 | #整型数组合并#

整型数组合并

http://www.nowcoder.com/practice/c4f11ea2c886429faf91decfaf6a310b

#include<stdio.h>

int mearge(inta, int lena, int*b, int lenb){

int*tmp=(int*)malloc(sizeof(int)*(lena+lenb));
for (int i=0; i<lena; i++){
    tmp[i] = a[i];
}
for(int i=0; i<lenb; i++){
    tmp[i+lena]=b[i];
}
free(a);
free(b);
return tmp;

}

int cmp (const void * a, const void * b)

{ return ( (int)a - (int)b ); }

int main(){

int number1;
while(scanf("%d", &number1) != EOF){
    int number2;
    int *a = (int*)malloc(sizeof(int)*number1);
    for(int i=0; i<number1; i++){
        scanf("%d", &a[i]);
    }
    scanf("%d", &number2);
    int *b = (int*)malloc(sizeof(int)*number2);
    for(int i=0; i<number2; i++){
        scanf("%d", &b[i]);
    }
    int *new = mearge(a, number1 ,b ,number2);
    qsort(new, number1+number2, sizeof(int), cmp);
    for(int i=0; i<number1+number2; i++){
        if(i>0 && new[i] == new[i-1])continue;
        printf("%d", new[i]);
    }
    printf("\n");
}

}

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务