题解 | #整型数组合并#

整型数组合并

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//读取元素后按照升序无重合并

int cmp_int(const void *a, const void *b);
int main()
{
  int n1, n2;
  while (scanf("%d", &n1) != EOF)
  {
    int s1[n1];
    for (int i = 0; i < n1; i++)
      scanf("%d", &s1[i]);
    scanf("%d", &n2);
    int s2[n2];
    for (int i = 0; i < n2; i++)
      scanf("%d", &s2[i]);
    int s[n1 + n2];
    int top = -1;
    int flag = 0; //0无重复
    for (int i = 0; i < n1; i++)
    {
      flag = 0;
      for (int j = 0; j <= top; j++)
      {
        if (s1[i] == s[j])
        {
          flag = 1;
          break;
        }
      }
      if (flag == 0)
        s[++top] = s1[i];
    }

    for (int i = 0; i < n2; i++)
    {
      flag = 0;
      for (int j = 0; j <= top; j++)
      {
        if (s2[i] == s[j])
        {
          flag = 1;
          break;
        }
      }
      if (flag == 0)
        s[++top] = s2[i];
    }

    //0-top sort
    qsort(s, top + 1, sizeof(s[0]), cmp_int);

    for (int i = 0; i <= top; i++)
    {
      printf("%d", s[i]);
    }
    printf("\n");
  }

  return 0;
}

int cmp_int(const void *a, const void *b)
{
  return *(int *)a - *(int *)b;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
10-05 10:13
已编辑
HHHHaos:让这些老登来现在秋招一下,简历都过不去
点赞 评论 收藏
分享
10-28 14:42
门头沟学院 Java
watermelon1124:因为嵌入式炸了
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务