Set Mismatch

题目描述

The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number.

Given an array nums representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.

输入

第一行一个数n表示数字的数量(n<=100)

第二行给出n个数

输出

在一行内输出两个数分别表示出现两次的数的和未出现过的数

样例输入

4
1 2 2 4

样例输出

2 3
#include<iostream>
using namespace std;
int main()
{
    int n,i,k;
    cin>>n;
    int  a[100]={0},b[100]={0};
    for(i=0;i<n;i++)
      {
            cin>>a[i];
            k=a[i];
            b[k]++;
      }
for(i=1;i<=n;i++)
{
      if(b[i]==2)
            cout<<i<<" ";
}
for(i=1;i<=n;i++)
{
      if(b[i]==0)
            cout<<i;
}
   return 0;
}

猜你喜欢

转载自blog.csdn.net/i_silence/article/details/80484042