首页 > 其他分享 >找出那个数字出现3次的数字

找出那个数字出现3次的数字

时间:2022-12-13 16:01:34浏览次数:46  
标签:tmp 那个 找出 数字 int COUNT count ++


找出那个数字出现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;
}

// print
for (int i = 0; i < k; i++)
printf(" %d", y[i]);


return 0;
}



标签:tmp,那个,找出,数字,int,COUNT,count,++
From: https://blog.51cto.com/u_15911341/5934388

相关文章