不知道ITAT是什么东西,做了一道它的编程题,简单的递归实现。
题目如下:
//************************************************************
1、 某企业在未来的12个月要研究一种新产品,新产品的研制需要四个阶段,每个阶段都可用慢、正常、快等三种速度进行,时间和成本如下表所列。
| 
 | 理论研究 | 试验阶段 | 政府批准 | 销售 | 
| 慢 | 5/5 | 3/6 | 6/1 | 5/8 | 
| 正常 | 4/7 | 2/8 | 4/1 | 4/10 | 
| 快 | 2/10 | 1/12 | 2/3 | 3/15 | 
说明:单位(月/万元),时间按月,成本按万元为单位。
例如:5/5代表5个月,5万元;4/7代表4个月,7万元。
该企业准备在12个月内花费最少的费用就可以有新产品。
//*******************************************************************
实现如下:
 View Code
View Code 
1 /*
2 * Fredric 2012-3-27
3 * 第四届ITAT竞赛试题
4 */
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 typedef enum E_PROCESS{
9 E_FAST,
10 E_MIDDLE,
11 E_LOW,
12 E_NONE,
13 };
14
15 typedef struct tagCost{
16 int month;
17 int price;
18 }stCost;
19
20 stCost gStatus1[E_NONE] = {{2,10},{4,7}, {5,5}};//理论研究阶段
21 stCost gStatus2[E_NONE] = {{1,12},{2,8}, {3,6}};//实验阶段
22 stCost gStatus3[E_NONE] = {{2,3}, {4,2}, {6,1}};//政府批准
23 stCost gStatus4[E_NONE] = {{3,15},{4,10},{5,8}};//销售
24
25 stCost* gStatus[4] = {gStatus1, gStatus2, gStatus3, gStatus4};
26
27 E_PROCESS eTempRes[4]; //临时解
28 E_PROCESS eFinalRes[4];//最终解
29
30 int limitMonth = 12; //12个月内
31 int leastCost = 10000;//最小费用
32
33 void initTemp(){
34 for (int i = 0; i < 4; i++)
35 {
36 eTempRes[i] = E_NONE;
37 }
38 }
39
40 void search(int status, int month, int price){
41 int curPrice = 0;
42
43 for (int i = E_FAST; i < E_NONE; i++)
44 {
45 if ((gStatus[status] + i)->month + month < limitMonth)
46 {
47 eTempRes[status] = (E_PROCESS)i;
48 curPrice = price + (gStatus[status] + i)->price;
49 if (3 > status)
50 {
51 search(status + 1, month + (gStatus[status] + i)->month, curPrice);
52 }else{
53 if (curPrice < leastCost)
54 {
55 leastCost = curPrice;
56 for (i = 0; i < 4; i++)
57 {
58 eFinalRes[i] = eTempRes[i];
59 }
60 }
61 }
62 }
63 }
64
65 return;
66 }
67
68 void main(void)
69 {
70 search(0, 0, 0);
71
72 int totalvalue = 0;
73 int totalmonth = 0;
74
75 for(int i = 0; i < 4; i++){
76 totalvalue += (gStatus[i] + eFinalRes[i])->price;
77 totalmonth += (gStatus[i] + eFinalRes[i])->month;
78 printf("Status No,%d is %d\r\n", i, eFinalRes[i]);
79 }
80
81 printf("For %d Month, the Least Cost is %d\r\n",totalmonth, totalvalue);
82
83 return;
84 }
 
                     
                    
                 
                    
                 
        
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号