1.题目
一个数组中只有两个数字是出现一次,其他所有数字都出现了两次。
找出这两个数字,编程实现。
2.程序代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <windows.h>
void find_num(const int arr[10])
{
int i = 0;
int j = 0;
int count = 0;
for (i = 0; i < 10; i++)
{
count = 0;//每次给count初始化为0
for (j = 0; j < 10 ; j++)
{
if (arr[i] == arr[j])
{
count++;//若数组中的这两个值相等,则给count++
}
}
if (count == 1)//因为数组中的元素也要与自身比较,所以没有相等的也为1
{
printf("%d\n", arr[i]);
}
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 4, 3, 2, 1 };//定义一个数组
find_num(arr);//调用自定义函数
system("pause");
return 0;
}