作者在 2009-08-23 14:53:21 发布以下内容
最近开始学习运筹学方面的内容……
首先是学习了线性规划
某公司生产甲、乙两种产品,均需在A、B、C三种不同的设备上加工,产品加工所需工时单耗、产品销售后能获得的利润及设备可用工时数如下表所示。问:如何安排生产计划,才能使该公司获得的总利润最大?
用枚举法做这道题:
#include <stdio.h>
#include <windows.h>
class CTimer
{
public:
__forceinline CTimer( void ) {
QueryPerformanceFrequency( (PLARGE_INTEGER)&m_nFreq );
QueryPerformanceCounter( (PLARGE_INTEGER)&m_nStart ); }
__forceinline void Reset( void ) {
QueryPerformanceCounter( (PLARGE_INTEGER)&m_nStart ); }
__forceinline double Stop( void ) {
QueryPerformanceCounter( (PLARGE_INTEGER)&m_nCur );
return double( m_nCur - m_nStart ) / double( m_nFreq ); }
__forceinline bool SelfTest( void ){
return ( 0 != QueryPerformanceFrequency( (PLARGE_INTEGER)&m_nFreq ) ); }
private:
__int64 m_nCur;
__int64 m_nFreq;
__int64 m_nStart;
};
int main(){
int i,j;
int a,b,x,y;
CTimer tmr;
b=-1;
for(i=0;;i++){
if((3*i>540)&&(5*i>450)&&(9*i>720)) break;
for(j=0;(3*i+9*j<=540)&&(5*i+5*j<=450)&&(9*i+3*j<=720);j++){
a=70*i+30*j;
if(b==-1||a>b){
b=a;x=i;y=j;
}
}
}
printf("x=%d, y=%d ==> %d\nuse time: %f ms\n",x,y,b,tmr.Stop()*1000);
return 0;
}
运行结果为:
x=75, y=15 ==> 5700
use time: 0.107556 ms
Press any key to continue
只用了0.1毫秒就解决了问题……
首先是学习了线性规划
某公司生产甲、乙两种产品,均需在A、B、C三种不同的设备上加工,产品加工所需工时单耗、产品销售后能获得的利润及设备可用工时数如下表所示。问:如何安排生产计划,才能使该公司获得的总利润最大?
用枚举法做这道题:
#include <stdio.h>
#include <windows.h>
class CTimer
{
public:
__forceinline CTimer( void ) {
QueryPerformanceFrequency( (PLARGE_INTEGER)&m_nFreq );
QueryPerformanceCounter( (PLARGE_INTEGER)&m_nStart ); }
__forceinline void Reset( void ) {
QueryPerformanceCounter( (PLARGE_INTEGER)&m_nStart ); }
__forceinline double Stop( void ) {
QueryPerformanceCounter( (PLARGE_INTEGER)&m_nCur );
return double( m_nCur - m_nStart ) / double( m_nFreq ); }
__forceinline bool SelfTest( void ){
return ( 0 != QueryPerformanceFrequency( (PLARGE_INTEGER)&m_nFreq ) ); }
private:
__int64 m_nCur;
__int64 m_nFreq;
__int64 m_nStart;
};
int main(){
int i,j;
int a,b,x,y;
CTimer tmr;
b=-1;
for(i=0;;i++){
if((3*i>540)&&(5*i>450)&&(9*i>720)) break;
for(j=0;(3*i+9*j<=540)&&(5*i+5*j<=450)&&(9*i+3*j<=720);j++){
a=70*i+30*j;
if(b==-1||a>b){
b=a;x=i;y=j;
}
}
}
printf("x=%d, y=%d ==> %d\nuse time: %f ms\n",x,y,b,tmr.Stop()*1000);
return 0;
}
运行结果为:
x=75, y=15 ==> 5700
use time: 0.107556 ms
Press any key to continue
只用了0.1毫秒就解决了问题……