#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
请按任意键继续. . .