#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. jquery表格展示

    用bootstrap设计一个弹框,然后在弹框里面生成表格 <html> <head> <link rel="stylesheet" href=&quo ...

  2. 敌兵布阵---hud1166(线段树或者树状数组模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 线段树中对某一点的值进行改变: #include<iostream> #includ ...

  3. 洛谷P3389 高斯消元 / 高斯消元+线性基学习笔记

    高斯消元 其实开始只是想搞下线性基,,,后来发现线性基和高斯消元的关系挺密切就一块儿在这儿写了好了QwQ 先港高斯消元趴? 这个算法并不难理解啊?就会矩阵运算就过去了鸭,,, 算了都专门为此写个题解还 ...

  4. 【PyQt5-Qt Designer】QComboBox(下拉列表框) 使用模板

    import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * ###### ...

  5. jquery css样式

    1.单一样式 $('.list li').css( "float", "none"); 2.  多个样式 $('.list li').css( {        ...

  6. windows平台mysql密码设置

    登录mysql默认没有指定账号 查看默认账号是谁 select user(); mysql> select user();+----------------+| user() |+------- ...

  7. 居然上了模板使用排行榜第一 happy一下

    这段时间在学习css和div,顺便把博客给整了一下,然后不小心就上了FFandIE模板使用排行榜第一,happy一下下.不知道这个算不算排名,还是随机刷新.感觉应该是按流量统计的,这段时间有几篇文章一 ...

  8. 【剑指offer】合并两个排序的链表

    一.题目: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 二.思路: 递归思想,首先判断一方为空的情况,一方为空则直接返回另一方.都不为空时,首先找到表 ...

  9. orm之路由层

    一.简单配置 1.参数 第一个参数是正则表达式(如果要精准匹配:‘^publish/$’),或者加斜杠('^publish/') 第二个参数是视图函数(不要加括号) urlpatterns = [ u ...

  10. PAT 1054 The Dominant Color[简单][运行超时的问题]

    1054 The Dominant Color (20)(20 分) Behind the scenes in the computer's memory, color is always talke ...