#include<cstdio>

 bool duplicate(int numbers[], int length, int* duplication)
{
if (numbers == nullptr || length <= )
return false;
for (int i = ; i < length; ++i)
{
if (numbers[i] < || numbers[i] > length - )
return false; }
for (int i = ; i < length; ++i)
{
while (numbers[i] != i)
{
if (numbers[i] == numbers[numbers[i]])
{
*duplication = numbers[i]; //
return true;
} int temp = numbers[i];
numbers[i] = numbers[temp];
numbers[temp] = temp; }
return false;
}
}
// test codes
bool contains(int array[], int length, int number)
{
for (int i = ; i < length; ++i)
{
if (array[i] == number)
return true;
}
return false;
}
void test(char* testName, int numbers[], int lengthNumbers, int expected[], \
int expectedExpected, bool validArgument)
{
printf("%s begins: ", testName);
int duplication;
bool validInput = duplicate(numbers, lengthNumbers, &duplication); if (validArgument == validInput)
{
if (validArgument)
{
if (contains(expected, expectedExpected, duplication))
printf("Passed.\n");
else
printf("Failed.\n");
}
else
printf("Passed.\n");
}
else
printf("Failed.\n");
} void test1()
{
int numbers[] = { ,,,, };
int duplications[] = { };
test("Test1", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true);
}
void test3()
{
int numbers[] = { ,,,, };
int duplications[] = { , };
test("Test1", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true);
} // 无效的输入
void test6()
{
int* numbers = nullptr;
int duplications[] = { - }; // not in use in the test function
test("Test6", numbers, , duplications, sizeof(duplications) / sizeof(int), false);
} // 没有重复的数字
void test4()
{
int numbers[] = { , , , , };
int duplications[] = { - }; // not in use in the test function
test("Test4", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), false);
} // 没有重复的数字
void test5()
{
int numbers[] = { , , , , };
int duplications[] = { - }; // not in use in the test function
test("Test5", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), false);
}
// 重复的数字是数组中最大的数字
void test2()
{
int numbers[] = { , , , , };
int duplications[] = { };
test("Test2", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true);
}
void main()
{
test1();
test2();
test3();
test4();
test5();
test6();
}

<OFFER03>03_01_DuplicationInArray的更多相关文章

  1. 剑指Offer03 逆序输出链表&链表逆序

    多写了个逆序链表 /************************************************************************* > File Name: ...

随机推荐

  1. IE强制标准模式---标准模式与兼容模式设置

    <meta http-equiv="X-UA-Compatible" content="edge" /> 这个是最有效的方法. 文档模式(docum ...

  2. 关于找不到类org/apache/commons/lang/xwork/StringUtils的问题

    在替换最新版的 struts2包的解决过程中.遇到 找不到这两个包org/apache/commons/lang/xwork/StringUtils.org/apache/commons/lang/x ...

  3. 开源的挖矿软件,sha256

    http://cryptomining-blog.com/tag/sha-256d-miner/ https://github.com/cbuchner1/CudaMiner/blob/master/ ...

  4. [py]列表生成式-支持条件,多值的拼接

    列表生成式 代码简洁一些 支持多条件, 过滤,或拼接某些值 支持返回多值 是一种生成式 # 生成一个列表 print(list(range(1, 11))) # 生成一个列表x^2 ## 方法1: 返 ...

  5. PAT 1068 Find More Coins[dp][难]

    1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...

  6. JS的二维数组

    今天,记录一下JS的二位数组,并附上例题. 一.二维数组的本质:数组中的元素又是数组. 其实,我们都见过这样的二维数组,只不过没在意罢了,例如: var arr = [[1,2,4,6],[2,4,7 ...

  7. 从Maven仓库中导出jar包

    从Maven仓库中导出jar包:进入工程pom.xml 所在的目录下,输入以下命令:mvn dependency:copy-dependencies -DoutputDirectory=lib更简单的 ...

  8. selenium python 启动Firefox

    我的火狐浏览器版本是最新的: 下载geckodrive:https://github.com/mozilla/geckodriver/releases/ 下载完后将exe文件放到这里“D:\firef ...

  9. ios一些问题

    多线程,加锁,如何互斥. http里面的get put post的差别 sockect tcp udp

  10. selenium webdriver处理HTML5 的视频播放

    import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.sele ...