日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

书包问题

發(fā)布時(shí)間:2023/12/20 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 书包问题 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#include <iostream> using namespace std;// abstract data type for stacktemplate <class T> // 棧的元素類(lèi)型為 T class Stack { public: // 棧的運(yùn)算集void clear(); // 變?yōu)榭諚ool push(const T item); // item入棧,成功則返回真,否則返回假bool pop(T* item); // 返回棧頂內(nèi)容并彈出,成功返回真,否則返回假,bool getTop(T* item); // 返回棧頂內(nèi)容但不彈出成功返回真,否則返回假,bool isEmpty(); // 若棧已空返回真bool isFull(); // 若棧已滿(mǎn)返回真 };// array-based stack: definition and implementation for some methodstemplate <class T> class arrStack: public Stack<T> { private: // 棧的順序存儲(chǔ)int mSize; // 棧中最多可存放的元素個(gè)數(shù)T *st; // 存放棧元素的數(shù)組 public: int top; // 棧頂位置,應(yīng)小于mSize public: // 棧的運(yùn)算的順序?qū)崿F(xiàn)arrStack(int size) { // 創(chuàng)建一個(gè)順序棧的實(shí)例mSize = size;top = -1;st = new T[mSize];}~arrStack() { // 析構(gòu)函數(shù)delete [] st;}void clear() { // 清空棧內(nèi)容top = -1;}bool push(const T item) { // 入棧操作的順序?qū)崿F(xiàn) if (top == mSize-1) { // 棧已滿(mǎn) cout << "棧滿(mǎn)溢出" << endl;return false;}else { // 新元素入棧并修改棧頂指針st[++top] = item;return true;}}bool pop(T* item) { // 出棧的順序?qū)崿F(xiàn)if (top == -1) { // 棧為空cout << "棧為空,不能出棧操作"<< endl; return false;}else {*item = st[top--]; // 返回棧頂元素并修改棧頂指針return true;}}bool getTop(T* item) { // 返回棧頂內(nèi)容,但不彈出if (top == -1) { // 棧空cout << " 棧為空,不能出棧操作"<< endl; return false;}else { *item = st[top];return true;}}bool isEmpty() { return (top == -1);}bool isFull() { return (top == mSize-1);} };bool knap(int s, int n); bool nonRecKnap(int s, int n); bool nonRecKnapOpt(int s, int n);enum rdType {a, b, c}; class knapNode {friend bool nonRecKnap(int s, int n);friend bool nonRecKnapOpt(int s, int n);int s, n; // 背包的承重量和物品的數(shù)目rdType rd; // 返回地址bool k; // 結(jié)果單元 }; // 引入兩個(gè)與棧中結(jié)點(diǎn)類(lèi)型相同的變量tmp和x作為進(jìn)出棧的緩沖: class knapNode tmp, x;// 定義一個(gè)棧變量: //Stack<knapNode> st(20); int w[5] = {2,4,4,5,7};int main() {int s;cin >> s; cout << "recursion" << endl; if (knap(s, 5)) cout << "Have solution" << endl;else cout << "No solution" << endl;cout << "non recursion" << endl; if (nonRecKnap(s, 5)) cout << "Have solution" << endl;else cout << "No solution" << endl;cout << "non recursion optimization" << endl; if (nonRecKnapOpt(s, 5)) cout << "Have solution" << endl;else cout << "No solution" << endl;cin >> s; return 0; } //遞歸版的背包問(wèn)題 bool knap(int s, int n) {if (s == 0) return true;if ((s < 0)||(s>0 && n <1))return false;if (knap(s-w[n-1], n-1)) {cout << w[n-1];return true;} else knap(s, n-1); } // 非遞歸的機(jī)械版 bool nonRecKnap(int s, int n) {arrStack<knapNode> st(20); tmp.s = s, tmp.n = n, tmp.rd = a; // 非遞歸調(diào)用入口st.push(tmp);label0: // 遞歸調(diào)用入口st.pop(&tmp); if (tmp.s == 0 ) { tmp.k = true; // 修改棧頂?shù)慕Y(jié)果單元kst.push(tmp);goto label3;}if ((tmp.s < 0) || (tmp.s > 0 && tmp.n < 1)) {tmp.k = false; // 修改棧頂?shù)慕Y(jié)果單元kst.push(tmp);goto label3;}st.push(tmp);x.s = tmp.s-w[tmp.n-1]; // 按照規(guī)則1進(jìn)行壓棧處理x.n = tmp.n -1;x.rd = b;st.push(x);goto label0;label1: // 規(guī)則1對(duì)應(yīng)的返回處理st.pop(&x); // 查看棧頂,根據(jù)其內(nèi)容分情況處理if (tmp.k == true) { // 若某層的結(jié)果單元為truex.k = true; // 把true結(jié)果上傳給調(diào)用方st.push(x);cout << w[x.n-1]<<endl; // 并輸出對(duì)應(yīng)的物品goto label3;}st.push(x); // 若某層的結(jié)果單元為falsetmp.s = x.s; // 當(dāng)前物品的選擇不合適,回溯,調(diào)用規(guī)則2tmp.n = x.n - 1; // 按照規(guī)則2進(jìn)行壓棧處理tmp.rd = c;st.push(tmp);goto label0;label2: // 規(guī)則2對(duì)應(yīng)的返回處理st.pop(&x);x.k = tmp.k; // 結(jié)果單元k的內(nèi)容上傳給調(diào)用方st.push(x);label3: // 遞歸出口處理st.pop(&tmp);switch (tmp.rd) {case a: return tmp.k; // 算法結(jié)束并返回結(jié)果case b: goto label1; // 轉(zhuǎn)向規(guī)則1的返回處理處case c: goto label2; // 轉(zhuǎn)向規(guī)則2的返回處理處} } // 非遞歸的優(yōu)化版 bool nonRecKnapOpt(int s, int n) {int t, n0 = n;bool k = false;arrStack<knapNode> st(20); tmp.s = s, tmp.rd = a; st.push(tmp);while (!st.isEmpty()) {t = st.top;st.getTop(&tmp);while ((tmp.s >= 0) && (tmp.s <= 0 || n0 > t)){ //處理?xiàng)m斣?#xff0c;以判斷是否滿(mǎn)足遞歸出口條件if (tmp.s == 0 ) { k = true;break;} else { // 尚未遞達(dá)歸出口前,按規(guī)則1進(jìn)行壓棧操作x.s = tmp.s - w[n0 - 1 - t];x.rd = b;st.push(x);}t = st.top;st.getTop(&tmp);}while (!st.isEmpty()) { // 返回處理 st.pop(&tmp);t = st.top;if (tmp.rd == 0) // 算法結(jié)束return k; if (tmp.rd == 1) // 從規(guī)則1返回if (k == true) // 結(jié)果為真則打印對(duì)應(yīng)的物品cout << w[n0 - 1 - t]<<endl; else { // 否則回溯,采用規(guī)則2進(jìn)棧st.getTop(&x);tmp.s = x.s;tmp.rd = c;st.push(tmp);break;}}} }

總結(jié)

以上是生活随笔為你收集整理的书包问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。