已经修改了,问一下,楼主是否可以再拿出几个测试的数据,我再测试一下,在往上贴
图!
不一定是真相!
#include <iostream> #include "head.h" int main( void ) { int region_num = 0 ; int staff_num = 0; static int index = 0; input_region_staff( staff_num , region_num ); int *region_capacity = new int [region_num]; int *best_match = new int [staff_num * 3]; static int *real_capacity = new int [region_num]; int** staff_feeling = new int* [staff_num]; int sum = 0; for(int i = 0 ; i < staff_num ; i++) { staff_feeling[i] = new int[region_num]; } input_region_cap( region_num , region_capacity ); input_staff_feeling( staff_num , region_num , staff_feeling ); if( test_num_equ(staff_num , region_capacity , region_num) == 1) return 0; cout<<endl; cout<<"-------------------华丽的分割线--------------------"<<endl; cout<<endl; output_region_staff( staff_num , region_num ); output_region_cap(region_num , region_capacity ); output_staff_feeling( staff_num , region_num , staff_feeling); find_max(staff_feeling , staff_num , region_num ,best_match , region_capacity , real_capacity , index); cout<<"-------------------华丽的分割线--------------------"<<endl; output_result(best_match , staff_num); delete [] region_capacity; delete [] best_match; delete [] real_capacity; for(int i = 0 ; i < staff_num ; i++) { delete [] staff_feeling[i]; } return 0; }
#ifndef HEAD_H_ #define HEAD_H_ #include <iostream> using namespace std; void input_region_staff( int &num1 , int &num2 ); void input_region_cap(int ®ion_num , int* region_capacity ); void input_staff_feeling(int staff_num , int region_num ,int **staff_feeling); int test_num_equ(int &staff_num , int *region_capacity , int region_num); void output_region_staff( int &num1 , int &num2 ); void output_region_cap(int ®ion_num , int *region_capacity ); void output_staff_feeling(int staff_num , int region_num ,int **staff_feeling); void output_result(int *best_match , int &staff_num); int find_second_max(int **staff_feeling , int search_line , int search_row , int &max , int ®ion_num); void max_detail(int **staff_feeling , int &staff_num , int &max_line_temp ,int &max_row_temp , int &max , int ®ion_num); void find_max(int **staff_feeling , int staff_num , int region_num ,int *best_match , int *region_capacity , int *real_capacity , int index); void line_empty(int **staff_feeling , int line ,int region_num ); void row_empty(int **staff_feeling , int row ,int staff_num ); #endif
#include "head.h" void input_region_staff( int &num1 , int &num2 ) { cout << "请输入员工数和食品区域数" << endl; cin >> num1 >> num2; } void input_region_cap(int ®ion_num , int* region_capacity ) { //region_capacity = new int [region_num]; cout << "请输入每一个食品区域所能容纳的人数" << endl; for(int i = 0 ; i< region_num ; i++) { cin >> *(region_capacity + i); } } int test_num_equ(int &staff_num , int *region_capacity , int region_num) { int sum = 0; for(int i = 0 ; i < region_num ; i++) { sum += region_capacity[ i ]; } if(sum != staff_num) { cout << "staff_num and region_capacity isn't euqal" << endl; return 1; } return 0; } void input_staff_feeling(int staff_num , int region_num ,int **staff_feeling) { cout << "输入每一个员工对视频区域的喜好程度(0~100)" << endl; for(int i = 0 ; i < staff_num ; i++) for(int j = 0 ; j < region_num ; j++) { cin >> *(*(staff_feeling + i) + j); } } void output_region_staff( int &num1 , int &num2 ) { cout << "员工数 食品区域数 " << endl; cout<< num1 << " " << num2 << endl; } void output_region_cap(int ®ion_num , int *region_capacity ) { cout << "每一个食品区域的容纳人数为:" << endl; for(int i = 0 ; i < region_num ; i++) { cout << region_capacity[ i ] << " "; } cout<<endl; } void output_staff_feeling(int staff_num , int region_num , int **staff_feeling) { cout<<"员工喜好程度矩阵"<<endl; for(int i = 0 ; i < staff_num ; i++) { for(int j = 0 ; j < region_num ; j++) { cout << staff_feeling[i][j] << " "; } cout<<endl; } } int find_second_max(int **staff_feeling , int search_line , int search_row , int &max , int ®ion_num) { staff_feeling[search_line][search_row] = 0; int second_max = 0; for(int i = 0 ; i < region_num ; i++) { if(second_max < staff_feeling[search_line][i]) second_max = staff_feeling[search_line][i]; } staff_feeling[search_line][search_row] = max; return second_max; } void max_detail(int **staff_feeling , int &staff_num , int &max_line_temp ,int &max_row_temp , int &max , int ®ion_num) { //int max = staff_feeling[0][0]; int max_num = 0; int *max_info ; int second_max = 0; int index = 0; int search_flag = 0; int select_num = 0; //找最大值 for(int i = 0 ; i < staff_num ; i++ ) for(int j = 0 ; j < region_num ; j++ ) { if( max < staff_feeling[i][j]) { max = staff_feeling[i][j]; //max_line_temp = i; //max_row_temp = j; } } //找最大值的个数 for(int i = 0 ; i < staff_num ; i++ ) for(int j = 0 ; j < region_num ; j++ ) { if( max == staff_feeling[i][j] ) { max_num++; break; //在看一个下continue的意义 } } max_info = new int [(max_num + 1) * 4]; //将最大值的信号,放到一个数组当中去(一行当中应该只有一个最大值,例如100,100,只应该记录一个100) for(int i = 0 ; i < staff_num ; i++ ) for(int j = 0 ; j < region_num ; j++ ) { if( max == staff_feeling[i][j] ) { max_info[ index++ ] = max; max_info[ index++ ] = i; max_info[ index++ ] = j; max_info[ index++ ] = 0; break; } } if(max_num == 0) { max_line_temp = max_info[1]; max_row_temp = max_info[2]; } else { for(int i = 0 ; i < max_num ; i++) { max_info[(i * 4) + 3] = find_second_max(staff_feeling , max_info[(i * 4) + 1] , max_info[(i * 4) + 2] , max , region_num); } select_num = max_info[3]; for(int i = 0 ; i < max_num ; i++) { if(select_num > max_info[(i * 4) + 3]) search_flag = i; } max_line_temp = max_info[(search_flag * 4) + 1]; max_row_temp = max_info[(search_flag * 4) + 2]; } } void find_max(int **staff_feeling , int staff_num , int region_num ,int *best_match , int *region_capacity , int *real_capacity , int index) { int max = staff_feeling[0][0]; int max_line_temp = 0; int max_row_temp = 0; static int flag = staff_num; if(flag == 0) return; /* for(int i = 0 ; i < staff_num ; i++ ) for(int j = 0 ; j < region_num ; j++ ) { if( max < staff_feeling[i][j]) { max = staff_feeling[i][j]; max_line_temp = i; max_row_temp = j; } } */ max_detail(staff_feeling , staff_num , max_line_temp ,max_row_temp , max , region_num); if( real_capacity[max_row_temp] != region_capacity[max_row_temp]) { best_match[index++] = max; best_match[index++] = max_line_temp; best_match[index++] = max_row_temp; flag--; line_empty(staff_feeling , max_line_temp , region_num); real_capacity[max_row_temp]++; find_max(staff_feeling , staff_num , region_num , best_match , region_capacity , real_capacity , index); } else { //staff_feeling[max_line_temp][max_row_temp] = -1; row_empty(staff_feeling , max_row_temp , staff_num); find_max(staff_feeling , staff_num , region_num , best_match , region_capacity , real_capacity , index); } } void line_empty(int **staff_feeling , int line ,int region_num ) { for(int i = 0 ; i < region_num ; i++) { staff_feeling[line][i] = -1; } } void row_empty(int **staff_feeling , int row ,int staff_num ) { for(int i = 0 ; i < staff_num ; i++) { staff_feeling[i][row] = -1; } } void output_result(int *best_match , int &staff_num) { int sum = 0; cout<<"最大匹配值为: "; for(int k = 0 ; k < (staff_num * 3) ; k += 3) { sum += best_match[k]; } cout << sum << endl; cout << "最佳匹配坐标为(员工号,食品区域):" << endl; for(int k = 0 ; k < (staff_num * 3) ; k += 3) { cout << "(" << best_match[k+1] << "," << best_match[k+2] << ")" << endl ; } }
#include <iostream> #include "head.h" int main( void ) { int region_num = 0 ; int staff_num = 0; static int index = 0; input_region_staff( staff_num , region_num ); int *region_capacity = new int [region_num]; int *best_match = new int [staff_num * 3]; static int *real_capacity = new int [region_num]; for(int k = 0 ; k < region_num ; k++) { real_capacity[k] = 0; } int** staff_feeling = new int* [staff_num]; int sum = 0; for(int i = 0 ; i < staff_num ; i++) { staff_feeling[i] = new int[region_num]; } input_region_cap( region_num , region_capacity ); input_staff_feeling( staff_num , region_num , staff_feeling ); if( test_num_equ(staff_num , region_capacity , region_num) == 1) return 0; cout<<endl; cout<<"-------------------华丽的分割线--------------------"<<endl; cout<<endl; output_region_staff( staff_num , region_num ); output_region_cap(region_num , region_capacity ); output_staff_feeling( staff_num , region_num , staff_feeling); find_max(staff_feeling , staff_num , region_num ,best_match , region_capacity , real_capacity , index); cout<<"-------------------华丽的分割线--------------------"<<endl; output_result(best_match , staff_num); delete [] region_capacity; delete [] best_match; delete [] real_capacity; for(int i1 = 0 ; i1 < staff_num ; i1++) { delete [] staff_feeling[i1]; } return 0; }
#ifndef HEAD_H_ #define HEAD_H_ #include <iostream> using namespace std; void input_region_staff( int &num1 , int &num2 ); void input_region_cap(int ®ion_num , int* region_capacity ); void input_staff_feeling(int staff_num , int region_num ,int **staff_feeling); int test_num_equ(int &staff_num , int *region_capacity , int region_num); void output_region_staff( int &num1 , int &num2 ); void output_region_cap(int ®ion_num , int *region_capacity ); void output_staff_feeling(int staff_num , int region_num ,int **staff_feeling); void output_result(int *best_match , int &staff_num); int find_second_max(int **staff_feeling , int search_line , int search_row , int &max , int ®ion_num); void max_detail(int **staff_feeling , int &staff_num , int &max_line_temp ,int &max_row_temp , int &max , int ®ion_num); void find_max(int **staff_feeling , int staff_num , int region_num ,int *best_match , int *region_capacity , int *real_capacity , int index); void line_empty(int **staff_feeling , int line ,int region_num ); void row_empty(int **staff_feeling , int row ,int staff_num ); #endif
#include "head.h" void input_region_staff( int &num1 , int &num2 ) { cout << "请输入员工数和食品区域数" << endl; cin >> num1 >> num2; } void input_region_cap(int ®ion_num , int* region_capacity ) { //region_capacity = new int [region_num]; cout << "请输入每一个食品区域所能容纳的人数" << endl; for(int i = 0 ; i< region_num ; i++) { cin >> *(region_capacity + i); } } int test_num_equ(int &staff_num , int *region_capacity , int region_num) { int sum = 0; for(int i = 0 ; i < region_num ; i++) { sum += region_capacity[ i ]; } if(sum != staff_num) { cout << "staff_num and region_capacity isn't euqal" << endl; return 1; } return 0; } void input_staff_feeling(int staff_num , int region_num ,int **staff_feeling) { cout << "输入每一个员工对视频区域的喜好程度(0~100)" << endl; for(int i = 0 ; i < staff_num ; i++) for(int j = 0 ; j < region_num ; j++) { cin >> *(*(staff_feeling + i) + j); } } void output_region_staff( int &num1 , int &num2 ) { cout << "员工数 食品区域数 " << endl; cout<< num1 << " " << num2 << endl; } void output_region_cap(int ®ion_num , int *region_capacity ) { cout << "每一个食品区域的容纳人数为:" << endl; for(int i = 0 ; i < region_num ; i++) { cout << region_capacity[ i ] << " "; } cout<<endl; } void output_staff_feeling(int staff_num , int region_num , int **staff_feeling) { cout<<"员工喜好程度矩阵"<<endl; for(int i = 0 ; i < staff_num ; i++) { for(int j = 0 ; j < region_num ; j++) { cout << staff_feeling[i][j] << " "; } cout<<endl; } } int find_second_max(int **staff_feeling , int search_line , int search_row , int &max , int ®ion_num) { staff_feeling[search_line][search_row] = 0; int second_max = 0; for(int i = 0 ; i < region_num ; i++) { if(second_max < staff_feeling[search_line][i]) second_max = staff_feeling[search_line][i]; } staff_feeling[search_line][search_row] = max; return second_max; } void max_detail(int **staff_feeling , int &staff_num , int &max_line_temp ,int &max_row_temp , int &max , int ®ion_num) { //int max = staff_feeling[0][0]; int max_num = 0; int *max_info ; int second_max = 0; int index = 0; int search_flag = 0; int select_num = 0; //找最大值 for(int i = 0 ; i < staff_num ; i++ ) for(int j = 0 ; j < region_num ; j++ ) { if( max < staff_feeling[i][j]) { max = staff_feeling[i][j]; //max_line_temp = i; //max_row_temp = j; } } //找最大值的个数 for(int i1 = 0 ; i1 < staff_num ; i1++ ) for(int j1 = 0 ; j1 < region_num ; j1++ ) { if( max == staff_feeling[i1][j1] ) { max_num++; break; //在看一个下continue的意义 } } max_info = new int [(max_num + 1) * 4]; //将最大值的信号,放到一个数组当中去(一行当中应该只有一个最大值,例如100,100,只应该记录一个100) for(int i2 = 0 ; i2 < staff_num ; i2++ ) for(int j2 = 0 ; j2 < region_num ; j2++ ) { if( max == staff_feeling[i2][j2] ) { max_info[ index++ ] = max; max_info[ index++ ] = i2; max_info[ index++ ] = j2; max_info[ index++ ] = 0; break; } } if(max_num == 0) { max_line_temp = max_info[1]; max_row_temp = max_info[2]; } else { for(int i = 0 ; i < max_num ; i++) { max_info[(i * 4) + 3] = find_second_max(staff_feeling , max_info[(i * 4) + 1] , max_info[(i * 4) + 2] , max , region_num); } select_num = max_info[3]; for(int i3 = 0 ; i3 < max_num ; i3++) { if(select_num > max_info[(i3 * 4) + 3]) search_flag = i3; } max_line_temp = max_info[(search_flag * 4) + 1]; max_row_temp = max_info[(search_flag * 4) + 2]; } } void find_max(int **staff_feeling , int staff_num , int region_num ,int *best_match , int *region_capacity , int *real_capacity , int index) { int max = staff_feeling[0][0]; int max_line_temp = 0; int max_row_temp = 0; static int flag = staff_num; if(flag == 0) return; /* for(int i = 0 ; i < staff_num ; i++ ) for(int j = 0 ; j < region_num ; j++ ) { if( max < staff_feeling[i][j]) { max = staff_feeling[i][j]; max_line_temp = i; max_row_temp = j; } } */ max_detail(staff_feeling , staff_num , max_line_temp ,max_row_temp , max , region_num); if( real_capacity[max_row_temp] != region_capacity[max_row_temp]) { best_match[index++] = max; best_match[index++] = max_line_temp; best_match[index++] = max_row_temp; flag--; line_empty(staff_feeling , max_line_temp , region_num); real_capacity[max_row_temp]++; find_max(staff_feeling , staff_num , region_num , best_match , region_capacity , real_capacity , index); } else { //staff_feeling[max_line_temp][max_row_temp] = -1; row_empty(staff_feeling , max_row_temp , staff_num); find_max(staff_feeling , staff_num , region_num , best_match , region_capacity , real_capacity , index); } } void line_empty(int **staff_feeling , int line ,int region_num ) { for(int i = 0 ; i < region_num ; i++) { staff_feeling[line][i] = -1; } } void row_empty(int **staff_feeling , int row ,int staff_num ) { for(int i = 0 ; i < staff_num ; i++) { staff_feeling[i][row] = -1; } } void output_result(int *best_match , int &staff_num) { int sum = 0; cout<<"最大匹配值为: "; for(int k = 0 ; k < (staff_num * 3) ; k += 3) { sum += best_match[k]; } cout << sum << endl; cout << "最佳匹配坐标为(员工号,食品区域):" << endl; for(int k1 = 0 ; k1 < (staff_num * 3) ; k1 += 3) { cout << "(" << best_match[k1+1] << "," << best_match[k1+2] << ")" << endl ; } }
#include "head.h" void input_region_staff( int &num1 , int &num2 ) { cout << "请输入员工数和食品区域数" << endl; cin >> num1 >> num2; } void input_region_cap(int ®ion_num , int* region_capacity ) { //region_capacity = new int [region_num]; cout << "请输入每一个食品区域所能容纳的人数" << endl; for(int i = 0 ; i< region_num ; i++) { cin >> *(region_capacity + i); } } int test_num_equ(int &staff_num , int *region_capacity , int region_num) { int sum = 0; for(int i = 0 ; i < region_num ; i++) { sum += region_capacity[ i ]; } if(sum != staff_num) { cout << "staff_num and region_capacity isn't euqal" << endl; return 1; } return 0; } void input_staff_feeling(int staff_num , int region_num ,int **staff_feeling) { cout << "输入每一个员工对视频区域的喜好程度(0~100)" << endl; for(int i = 0 ; i < staff_num ; i++) for(int j = 0 ; j < region_num ; j++) { cin >> *(*(staff_feeling + i) + j); } } void output_region_staff( int &num1 , int &num2 ) { cout << "员工数 食品区域数 " << endl; cout<< num1 << " " << num2 << endl; } void output_region_cap(int ®ion_num , int *region_capacity ) { cout << "每一个食品区域的容纳人数为:" << endl; for(int i = 0 ; i < region_num ; i++) { cout << region_capacity[ i ] << " "; } cout<<endl; } void output_staff_feeling(int staff_num , int region_num , int **staff_feeling) { cout<<"员工喜好程度矩阵"<<endl; for(int i = 0 ; i < staff_num ; i++) { for(int j = 0 ; j < region_num ; j++) { cout << staff_feeling[i][j] << " "; } cout<<endl; } } int find_second_max(int **staff_feeling , int search_line , int search_row , int &max , int ®ion_num ) { int max_temp = staff_feeling[search_line][search_row]; staff_feeling[search_line][search_row] = 0; int second_max = 0; for(int i = 0 ; i < region_num ; i++) { if(second_max < staff_feeling[search_line][i]) second_max = staff_feeling[search_line][i]; } staff_feeling[search_line][search_row] = max_temp; return second_max; } void max_detail(int **staff_feeling , int &staff_num , int &max_line_temp ,int &max_row_temp , int &max , int ®ion_num) { //int max = staff_feeling[0][0]; int max_num = 0; int *max_info ; int second_max = 0; int index = 0; int search_flag = 0; int select_num = 0; //找最大值 for(int i = 0 ; i < staff_num ; i++ ) for(int j = 0 ; j < region_num ; j++ ) { if( max < staff_feeling[i][j]) { max = staff_feeling[i][j]; //max_line_temp = i; //max_row_temp = j; } } //找最大值的个数 for(int i1 = 0 ; i1 < staff_num ; i1++ ) for(int j1 = 0 ; j1 < region_num ; j1++ ) { if( max == staff_feeling[i1][j1] || (max - staff_feeling[i1][j1]) <= 5 ) { max_num++; break; //在看一个下continue的意义 } } max_info = new int [(max_num + 1) * 4]; //将最大值的信号,放到一个数组当中去(一行当中应该只有一个最大值,例如100,100,只应该记录一个100) for(int i2 = 0 ; i2 < staff_num ; i2++ ) for(int j2 = 0 ; j2 < region_num ; j2++ ) { if( max == staff_feeling[i2][j2] || (max - staff_feeling[i2][j2]) <= 5 ) { max_info[ index++ ] = staff_feeling[i2][j2]; max_info[ index++ ] = i2; max_info[ index++ ] = j2; max_info[ index++ ] = 0; break; } } if(max_num == 0) { max_line_temp = max_info[1]; max_row_temp = max_info[2]; } else { for(int i = 0 ; i < max_num ; i++) { max_info[(i * 4) + 3] = find_second_max(staff_feeling , max_info[(i * 4) + 1] , max_info[(i * 4) + 2] , max , region_num ); } select_num = max_info[3]; for(int i3 = 0 ; i3 < max_num ; i3++) { if(select_num > max_info[(i3 * 4) + 3]) search_flag = i3; } max_line_temp = max_info[(search_flag * 4) + 1]; max_row_temp = max_info[(search_flag * 4) + 2]; } } void find_max(int **staff_feeling , int staff_num , int region_num ,int *best_match , int *region_capacity , int *real_capacity , int index) { int max = staff_feeling[0][0]; int max_line_temp = 0; int max_row_temp = 0; static int flag = staff_num; if(flag == 0) return; /* for(int i = 0 ; i < staff_num ; i++ ) for(int j = 0 ; j < region_num ; j++ ) { if( max < staff_feeling[i][j]) { max = staff_feeling[i][j]; max_line_temp = i; max_row_temp = j; } } */ max_detail(staff_feeling , staff_num , max_line_temp ,max_row_temp , max , region_num); if( real_capacity[max_row_temp] != region_capacity[max_row_temp]) { best_match[index++] = staff_feeling[max_line_temp][max_row_temp]; best_match[index++] = max_line_temp; best_match[index++] = max_row_temp; flag--; line_empty(staff_feeling , max_line_temp , region_num); real_capacity[max_row_temp]++; find_max(staff_feeling , staff_num , region_num , best_match , region_capacity , real_capacity , index); } else { //staff_feeling[max_line_temp][max_row_temp] = -1; row_empty(staff_feeling , max_row_temp , staff_num); find_max(staff_feeling , staff_num , region_num , best_match , region_capacity , real_capacity , index); } } void line_empty(int **staff_feeling , int line ,int region_num ) { for(int i = 0 ; i < region_num ; i++) { staff_feeling[line][i] = -1; } } void row_empty(int **staff_feeling , int row ,int staff_num ) { for(int i = 0 ; i < staff_num ; i++) { staff_feeling[i][row] = -1; } } void output_result(int *best_match , int &staff_num) { int sum = 0; cout<<"最大匹配值为: "; for(int k = 0 ; k < (staff_num * 3) ; k += 3) { sum += best_match[k]; } cout << sum << endl; cout << "最佳匹配坐标为(员工号,食品区域):" << endl; for(int k1 = 0 ; k1 < (staff_num * 3) ; k1 += 3) { cout << "(" << best_match[k1+1] << "," << best_match[k1+2] << ")" << endl ; } }