对于一个既定的自然数 N ,有一个 N + M 个元素的数组,其中存放了小于等于 N 的所有
自然数,求重复出现的自然数序列{X} 。
//============================================================================
// Name : FindRepeatNum.cpp
// Author : Lee
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
void FindRepeat(int array[], int n,int m) {
int index = array[n+m - 1] - 1;
while (true) {
if (array[index] < 0){
cout << "The repeat number is " << index + 1 << endl;
m--;
if(0==m){
return;
}
index=array[n+m-1]-1;
}
array[index] *= -1;
index = array[index] * (-1) - 1;
}
}
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
int tst[12]={1,2,2,3,4,4,5,8,7,6,9,10};
FindRepeat(tst,10,2);
return 0;
}