c语言寻找相同最大数的位置,C语言寻找出现次数最多的数字

/*不使用列表,而是直接用整型数组进行操作,速度最快,不进行删除,而是将当前位置的数据设置为尾部的数据,等同于数据交换,将不再用的数据移动到尾部就行了,数组的速度比列表要快太多了,*/

int _tmain(int argc, _TCHAR* argv[])

{

int list[]={1,2,3,4,4,3,2,1,1,2};

int length =sizeof(list)/sizeof(int);

printf("测试数组为:");

for(int i =0;i

{

printf("%d,",list[i]);

}

int *result;

result = new int[length];

int count = 0;

// 这个是剩余可用的总数

int has = length;

int element;

int MaxNum =0;

int tempCount;

// 循环,直到列表里不再有数据

while (has > 0)

{

// 拿到最后一个数据

element = list[has - 1];

// 计数

tempCount = 0;

// 向前查找相同的

for (int j = has - 1; j >= 0; j--)

{

// 如果找到,则计数加1,然后将数据和末尾交换

// 这也是为何要从末尾开始循环的理由

if (element == list[j])

{

tempCount++;

// 将当前位置交换为末尾的数据

// 注意末尾数据是动态变化的

list[j] = list[has - 1];

has--;

}

}

printf("\n修改数组为:");

for(int i =0;i

{

printf("%d,",list[i]);

}

// 如果这个计数最大,则更新输出的数据

if (tempCount > count)

{

count = tempCount;

MaxNum =0;

result[MaxNum]=element;

//printf("%d,",count);

// outValue.clear();

// outValue.add(element);

}

else if (tempCount == count)

{

// 否则加入到最大数的列表里

// outValue.add(element);

MaxNum ++;

result[MaxNum]=element;

//printf("%d,",count);

}

}

printf("\n出现频率最高的次数是:%d 次", count );

printf("\n出现频率最高次数的数字是:\n");

for(int m =0;m<=MaxNum;m++)

{

printf("%d,",result[m]);

}

getchar();

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值