如何找到一个数组里面重复次数最多的数

 
 
#include < iostream > #include < string > #include < iomanip > using namespace std; struct box { int number; int chongfu; }; void show( int x[], int y); void show_struct(box x[], int y); int main() { int test[ 10 ] = { 2 , 3 , 1 , 1 , 6 , 65 , 4 , 6 , 8 , 8 }; for ( int i = 0 ;i < 10 ;i ++ ) { for ( int j = i + 1 ;j < 10 ;j ++ ) { if (test[i] > test[j]) { int t; t = test[i]; test[i] = test[j]; test[j] = t; } } } box b[ 10 ]; for ( int i = 0 ;i < 10 ;i ++ ) { b[i].chongfu = 1 ; } int c = 0 ; for ( int i = 0 ;i < 10 ;i ++ ) { if (test[i] == test[i + 1 ]) { b[c].number = test[i]; b[c].chongfu += 1 ; } else { b[c].number = test[i]; ++ c; } } for ( int i = 0 ;i < 10 ;i ++ ) { for ( int j = i + 1 ;j < 10 ;j ++ ) { if (b[i].chongfu < b[j].chongfu) { box temp; temp = b[i]; b[i] = b[j]; b[j] = temp; } } } show(test, 10 ); show_struct(b,c); return 0 ; } void show( int x[], int y) { for ( int i = 0 ;i < y;i ++ ) { cout << x[i] << " " ; } cout << endl; } void show_struct(box x[], int y) { for ( int i = 0 ;i < y;i ++ ) { cout << " number: " << x[i].number << " chongfu: " << x[i].chongfu << endl; } }

运行效果:
1 1 2 3 4 6 6 8 8 65
number:1 chongfu:2
number:2 chongfu:1
number:3 chongfu:1
number:4 chongfu:1
number:6 chongfu:2
number:8 chongfu:2
number:65 chongfu:1
请按任意键继续. . .
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值