题目描述
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;
}
本文介绍了一种解决数组中数字重复与缺失问题的方法。通过输入一个包含错误数据的数组,程序能够找出重复的数字及丢失的数字,并将这两个数字作为结果返回。示例输入为4个数字,程序成功输出了重复的数字2和缺失的数字3。

1134

被折叠的 条评论
为什么被折叠?



