题解 | #整型数组合并#

整型数组合并

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;
}
全部评论

相关推荐

11-18 15:57
门头沟学院 Java
最终归宿是测开:这个重邮的大佬在重邮很有名的,他就喜欢打92的脸,越有人质疑他,他越觉得爽😂
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务