// 面试题3(二):不修改数组找出重复的数字
// 题目:在一个长度为n+1的数组里的所有数字都在1到n的范围内,所以数组中至
// 少有一个数字是重复的。请找出数组中任意一个重复的数字,但不能修改输入的
// 数组。例如,如果输入长度为8的数组{2, 3, 5, 4, 3, 2, 6, 7},那么对应的
// 输出是重复的数字2或者3。 #include <iostream>
using namespace std;
int counter(const int*, int, int, int); bool getDuplication(const int* numbers, int length, int& num)
{
if (numbers == NULL || length <= )//判断输入是否对
return false; for (int i = ; i < length; i++)//判断是否满足题目条件
{
if (numbers[i] < || numbers[i] > length)
return false;
} int start = , end = length - ; while (end >= start)
{
int middle = ((end - start) >> ) + start;
int count = counter(numbers, length, start, middle);//查找落在二分左区间内个数 //cout << "start=" << start << endl << "middle=" << middle << endl << "end=" << end << endl<< "count=" << count << endl; if (start == end)//二分不动了,停止,判断这个值count值
{
if (count > )
{
num = start;
return true;
}
else
break; } if (count > (middle - start) + )//如果落在左区间的个数大于区间范围,则这里面一定有重复,否则就去右区间看看
end = middle;
else
start = middle + ;
} return false;
} int counter(const int* numbers, int length, int start, int middle)
{
int count = ;
if (numbers == NULL || start > middle || start < )
return count; for (int i = ; i < length; i++)
{
if (numbers[i] >= start&&numbers[i] <= middle)
count++;
}
return count;
} void test(const char* testName, int* numbers, int length, int* duplications, int dupLength)
{
int result;
bool flag = getDuplication(numbers, length, result); if (!flag)
result = -; for (int i = ; i < dupLength; ++i)
{
if (result == duplications[i])
{
std::cout << testName << " passed." << std::endl;
return;
}
}
std::cout << testName << " FAILED." << std::endl;
} // 多个重复的数字
void test1()
{
int numbers[] = { , , , , , , , };
int duplications[] = { , };
test("test1", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 一个重复的数字
void test2()
{
int numbers[] = { , , , , , , , };
int duplications[] = { };
test("test2", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 重复的数字是数组中最小的数字
void test3()
{
int numbers[] = { , , , , , , , , };
int duplications[] = { };
test("test3", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 重复的数字是数组中最大的数字
void test4()
{
int numbers[] = { , , , , , , , , };
int duplications[] = { };
test("test4", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 数组中只有两个数字
void test5()
{
int numbers[] = { , };
int duplications[] = { };
test("test5", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 重复的数字位于数组当中
void test6()
{
int numbers[] = { , , , , , , , };
int duplications[] = { };
test("test6", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 多个重复的数字
void test7()
{
int numbers[] = { , , , , , , };
int duplications[] = { , };
test("test7", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 一个数字重复三次
void test8()
{
int numbers[] = { , , , , , , };
int duplications[] = { };
test("test8", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 没有重复的数字
void test9()
{
int numbers[] = { , , , , , };
int duplications[] = { - };
test("test9", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 无效的输入
void test10()
{
int* numbers = nullptr;
int duplications[] = { - };
test("test10", numbers, , duplications, sizeof(duplications) / sizeof(int));
} void main()
{
test1();
test2();
test3();//判断边界
test4();//判断边界
test5();
test6();
test7();
test8();
test9();
test10(); system("pause");
}