找出那个数字出现3次的数字,简单的C++
#include <stdio.h>
#define COUNT 3
#define ARRAY_SIZE(_) (sizeof (_) / sizeof (*_))
int _tmain(int argc, _TCHAR* argv[])
{
// 找出那个数字出现3次的数字
// x=[1 2 3 1 2 5 1 2]
int x[] = {1, 2, 3, 1, 2, 5, 1, 2};
int y[] = {0, 0, 0, 0 ,0, 0, 0, 0};
int n = ARRAY_SIZE(x);
int k = 0;
// second
for (int i = 0; i < n; i++)
{
int count = 0;
int flag = 0;
int tmp = x[i];
// check this value have exit ?
for (int j = 0; j < k; j++)
{
if (!(y[j] ^ tmp))
{
flag = 1;
break;
}
}
if (flag)
continue;
// count
for (int j = i/* 0 */; j < n; j++)
{
if (!(tmp ^ x[j]) && (count++ > COUNT))
break;
}
// save result
if (count == COUNT)
y[k++] = tmp;
}
for (int i = 0; i < k; i++)
printf(" %d", y[i]);
return 0;
}