| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1271 人关注过本帖
标题:还是那个编程题(这次把分加上去了)
只看楼主 加入收藏
七有新人
Rank: 3Rank: 3
来 自:哈尔滨
等 级:论坛游侠
帖 子:64
专家分:132
注 册:2010-10-12
收藏
得分:0 
已经修改了,问一下,楼主是否可以再拿出几个测试的数据,我再测试一下,在往上贴

图!
不一定是真相!
2010-11-17 16:59
kingsroot
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:284
专家分:1159
注 册:2010-3-28
收藏
得分:0 
你把代码贴上来
2010-11-17 17:01
七有新人
Rank: 3Rank: 3
来 自:哈尔滨
等 级:论坛游侠
帖 子:64
专家分:132
注 册:2010-10-12
收藏
得分:0 
程序代码:
#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 &region_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 &region_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 &region_num);
    void max_detail(int **staff_feeling , int &staff_num , int &max_line_temp ,int &max_row_temp , int &max , int &region_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 &region_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 &region_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 &region_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 &region_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 ;
    }
    
}

图!
不一定是真相!
2010-11-17 17:05
kingsroot
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:284
专家分:1159
注 册:2010-3-28
收藏
得分:0 
编不过!你再看下
2010-11-17 17:16
七有新人
Rank: 3Rank: 3
来 自:哈尔滨
等 级:论坛游侠
帖 子:64
专家分:132
注 册:2010-10-12
收藏
得分:0 
可以通过啊
我只改了头文件和头文件的实现
三个文件分别是
head.h head.cpp main.cpp
图片附件: 游客没有浏览图片的权限,请 登录注册


[ 本帖最后由 七有新人 于 2010-11-17 17:26 编辑 ]

图!
不一定是真相!
2010-11-17 17:23
kingsroot
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:284
专家分:1159
注 册:2010-3-28
收藏
得分:0 
貌似不对 我回家再调试下  看到底那里错了!
2010-11-17 18:07
五当家
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:2
帖 子:1112
专家分:3674
注 册:2010-10-20
收藏
得分:14 
通不过是因为没有head.h文件

经验积累中............
2010-11-17 18:14
七有新人
Rank: 3Rank: 3
来 自:哈尔滨
等 级:论坛游侠
帖 子:64
专家分:132
注 册:2010-10-12
收藏
得分:0 
我自己把上面的代码 贴在一个新的文件夹里试了试,好使啊
你那里是编译不过去还是 编译过去无法运行啊?

图!
不一定是真相!
2010-11-17 18:22
七有新人
Rank: 3Rank: 3
来 自:哈尔滨
等 级:论坛游侠
帖 子:64
专家分:132
注 册:2010-10-12
收藏
得分:0 
楼主刚才是不是在windows下运行的阿?
我在windows下试了一下,发现果然有错误,看来操作系统之间还有很多差异的
而且我的代码不够规范

修改了一下。vc6.0下
main.cpp
程序代码:
#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;
}

head.h
程序代码:
#ifndef HEAD_H_
    #define HEAD_H_
    #include <iostream>
    using namespace std;
    
    void input_region_staff( int &num1 , int &num2 );
    void input_region_cap(int &region_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 &region_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 &region_num);
    void max_detail(int **staff_feeling , int &staff_num , int &max_line_temp ,int &max_row_temp , int &max , int &region_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    

head.cpp
程序代码:
#include "head.h"

void input_region_staff( int &num1 , int &num2 )
{
    cout << "请输入员工数和食品区域数" << endl;
    cin >> num1 >> num2;
}

void input_region_cap(int &region_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 &region_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 &region_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 &region_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 ;
    }
    
}

图!
不一定是真相!
2010-11-17 19:26
七有新人
Rank: 3Rank: 3
来 自:哈尔滨
等 级:论坛游侠
帖 子:64
专家分:132
注 册:2010-10-12
收藏
得分:0 
程序代码:
#include "head.h"

void input_region_staff( int &num1 , int &num2 )
{
    cout << "请输入员工数和食品区域数" << endl;
    cin >> num1 >> num2;
}

void input_region_cap(int &region_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 &region_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 &region_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 &region_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 ;
    }
    
}


中午闲着没事,又想了一下这个问题,发现仍然有很大的缺陷,所以晚上又稍微的修改了一下,这次只是
改了头文件的实现,别的没改的
改动后可以处理
3 3
1 1 1
80 80 80
95 80 80
100 95 80
这类的问题,原程序的答案为(2,0)(1,1)(0,2)max=260,新程序的答案为(1,0)(2,1)(0,2)max=270
但是随之而来的也有其他的问题例如
3 3
1 1 1
80 80 80
95 85 80
100 80 80
这个例子他会取去出 (1,0)(2,1)(0,2)max=255 小于最佳的 (2,0)(1,1)(0,2)max=265

如果考虑到实际情况不会出现过分接近的爱好如(100和99)之类的,这个程序可以满足需要,但是它并不是
一个适用于任何情况求最优值的程序。

同时也感觉到了这个问题远比我想的复杂,我还想到了一个别的方法,但是估计近段时间没有时间了,而且不
不知道效果怎样。

希望楼主能公布答案

[ 本帖最后由 七有新人 于 2010-11-18 19:54 编辑 ]

图!
不一定是真相!
2010-11-18 19:52
快速回复:还是那个编程题(这次把分加上去了)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032621 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved