<OFFER03>03_01_DuplicationInArray
#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的更多相关文章
- 剑指Offer03 逆序输出链表&链表逆序
多写了个逆序链表 /************************************************************************* > File Name: ...
随机推荐
- Excel-字符串连接
使用函数concatenate()将多个字符连接起来
- 洛谷P2657 windy数 [SCOI2009] 数位dp
正解:数位dp 解题报告: 传送门! 这题一看就是个数位dp鸭,"不含前导零且相邻两个数字之差至少为2"这种的 然后就直接套板子鸭(板子戳总结,懒得放链接辣QAQ 然后就是套路 然 ...
- 对nodejs的理解(一)
1.介绍一下事件驱动编程---快餐店点餐. 在基于线程的方式中(thread-based way)你到了柜台前,把你的点餐单给收银员或者给收银员直接点餐,然后等在那直到你要的食物准备好给你.收银员不能 ...
- github中的一个快捷键
在github中,快捷键t的作用是对项目的内容进行查找.在项目目录中按下t键,会进入下面这个界面, 在first-program(这是我自己的项目名)这个项目名后,输入你要查找的内容,即可找到项目中所 ...
- Maven项目常见错误解决方法汇总
issue 1.Java compiler level does not match the version of the installed Java project facet. 或者 One o ...
- Redis演示及使用场景
概述 Redis是一个开源的.使用C语言编写的.支持网络交互的.可基于内存也可持久化的Key-Value(字典, Remote Dictionary Server,远程字典服务器)数据库. 客户端:h ...
- set的一些数学运算
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...
- 转载 vsftpd安装
http://blog.csdn.net/shutfuckingup/article/details/8250290 1:安装vsftpd yum install vsftpd 2:关闭防火墙 ...
- #C++初学记录(深度搜索#递归)
深度搜索 走地图的题目是深度搜索里比较容易理解的题目,更深层次的是全排列和七皇后等经典题目,更加难以理解,代码比较抽象. 题目:红与黑 蒜厂有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖. ...
- Object-C-Foundation-set
无序集合 哈希表 NSSet *colors=[NSSet setWithObjects:@@"yellow",@"red",@"blue" ...