• 东南大学《算法设计基础》课程作业 2


    Question

    Answer

    Code for #2

    #include "pch.h"
    
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <memory>
    #include <queue>
    #include <cmath>
    #include <ctime>
    
    using namespace std;
    
    #define NINF -99999999
    #define NMARKET 8
    
    struct ResultNode {
    	int val; // profit
    	queue<int> seq; // operation sequence
    };
    
    // [prevStatus, opCode]
    vector< vector< int > > cb = { {0, 6}, {1, 2}, {0, 3}, // transformed to Have No
    			       {1, 6}, {0, 1}, {1, 4}, {0, 5} }; // transformed to Have
    
    
    int B[NMARKET + 1] = { 0,5,6,7,5,9,7,17,16 };
    int S[NMARKET + 1] = { 0,4,9,10,18,12,6,15,18 };
    
    void gen() {
    	srand(time(NULL));
    	for (int i = 1; i <= NMARKET; i++)
    		B[i] = rand() * 10;
    	for (int i = 1; i <= NMARKET; i++)
    		S[i] = rand() * 5;
    	cout << "init ok" << endl;
    }
    
    int checkResult() {
    	int a[NMARKET + 1][NMARKET + 1] = { 0 };
    	for (int i = 1; i <= NMARKET; i++) {
    		a[i][i] = max(a[i - 1][i - 1] + S[i] - B[i], a[i - 1][i]);
    		for (int j = i + 1; j <= NMARKET; j++) {
    			a[i][j] = max(max(a[i][i] + S[j] - B[i], a[i - 1][j]), a[i][j - 1]);
    		}
    	}
    	return a[NMARKET][NMARKET];
    }
    
    int F(int i, int j) {
    	int r = 0;
    	switch (j) {
    	case 1:
    		r -= B[i]; break;
    	case 2:
    		r += S[i]; break;
    	case 3:
    		r -= B[i]; r += S[i]; break;
    	case 4:
    		r += S[i]; r -= B[i]; break;
    	case 5:
    		r -= B[i]; r += S[i]; r -= B[i]; break;
    	case 6:
    		r = 0;
    	}
    	return r;
    }
    
    void printSeq(queue<int> res) {
    	int i = 1;
    	while (!res.empty()) {
    		switch (res.front()) {
    		case 1: cout << "(" << i << ","; break;
    		case 2: cout << i << "),"; break;
    		case 3: cout << "(" << i << "," << i << "),"; break;
    		case 4: cout << i << "),(" << i << ","; break;
    		case 5: cout << "(" << i << "," << i << "),(" << i << ","; break;
    		case 6: break;
    		}
    		i++;
    		res.pop();
    	}
    	cout << endl;
    }
    
    int main() {
    	// gen();
    
    
    	ResultNode dp[NMARKET + 1][2];
    
    	dp[0][0].val = 0;
    	dp[0][1].val = NINF;
    
    	for (int i = 1; i <= NMARKET; i++) {
    		// xxx -> have no
    		for (auto j = cb.begin(); j < cb.begin() + 3; j++) {
    			if (dp[i - 1][(*j)[0]].val + F(i, (*j)[1]) > dp[i][0].val) {
    				dp[i][0].val = dp[i - 1][(*j)[0]].val + F(i, (*j)[1]);
    				dp[i][0].seq = queue<int>(dp[i - 1][(*j)[0]].seq);
    				dp[i][0].seq.push((*j)[1]);
    			}
    		}
    		// xxx -> have
    		for (auto j = cb.begin() + 3; j < cb.end(); j++) {
    			if (dp[i - 1][(*j)[0]].val + F(i, (*j)[1]) > dp[i][1].val) {
    				dp[i][1].val = dp[i - 1][(*j)[0]].val + F(i, (*j)[1]);
    				dp[i][1].seq = queue<int>(dp[i - 1][(*j)[0]].seq);
    				dp[i][1].seq.push((*j)[1]);
    			}
    		}
    	}
    
    	cout << "My max profit is: " << endl;
    	cout << dp[NMARKET][0].val << endl;
    	
    	cout << "The translated operation sequence is: " << endl;
    	printSeq(dp[NMARKET][0].seq);
    	cout << "The original operation sequence is: " << endl;
    	while (!dp[NMARKET][0].seq.empty()) {
    		cout << dp[NMARKET][0].seq.front() << " ";
    		dp[NMARKET][0].seq.pop();
    	}
    	cout << endl;
    
    	cout << "The DP array is " << endl;
    	for (int i = 0; i <= NMARKET; i++) {
    		for (int j = 0; j <= 1; j++)
    			cout << dp[i][j].val << " ";
    		cout << endl;
    	}
    	cout << endl;
    	cout << "Your max profit is:" << endl;
    	cout << checkResult() << endl;
    
    	system("pause");
    	return 0;
    
    }
    
    
  • 相关阅读:
    实验四(作业一:字符串加密)
    实验三“类和对象”动手动脑
    实验三“类和对象”课后作业(追踪已经创建了几个对象)
    递归实验动手动脑题目
    ("X+Y="+X+Y)与(X+Y+"=X+Y")
    Linq To Xml
    代码优先-Code First
    restFul接口设计规范
    四: 使用vue搭建网站前端页面
    三: vue组件开发及自动化工具vue-cli
  • 原文地址:https://www.cnblogs.com/zxuuu/p/14236363.html
Copyright © 2020-2023  润新知