#include<bits/stdc++.h>
using namespace std;
#define MAXN 911
#define Inf 0x3f3f3f3f
struct MatrixDimension
{
int n,m; ///表示矩阵是n*m维的
} Di[MAXN];
void TheMain();
void print(int **a, int **b, int n, int m, bool f);
void MatrixChainMain();
void MatrixChain(MatrixDimension MD[], int **s, int **bestnum, int n);
void print1(int **s, int l, int r);
void LCSlengthMain();
void LCSlength(int n, int m, char *s1, char *s2, int **c, int **b);
void print2(char *s1, char *s2, int nr, int mr, int **c);
void KnapsackMain();
void Knapsack(int **bestValue, int n, int *weight, int *value, int Capacity);
void print3(int **bv, int *w, int c, int n);
///-----------------------------------------------------------------
int main()
{
TheMain();
cout<<"程序已结束!"<<endl;
return 0;
}
///-----------------------------------------------------------------
void TheMain()
{
int flag;
cout<<"--------------------------------------------------------------------------------------------"<<endl;
cout<<" 主菜单 "<<endl;
cout<<"功能介绍:演示矩阵连乘、最长公共子序列、0-1背包的动态规划。 "<<endl;
cout<<"输入数字1进入矩阵连乘界面,输入2进入最长公共子序列界面,输入3进入0-1背包界面,输入0结束程序。"<<endl;
cin>>flag;
while(flag != 0 && flag != 1 && flag != 2 && flag != 3)
{
cout<<"输入错误,请按以下要求重新输入!"<<endl;
cout<<"输入数字1进入矩阵连乘界面,输入2进入最长公共子序列界面,输入3进入0-1背包界面,输入0结束程序。"<<endl;
cin>>flag;
}
cout<<"3秒后进入新界面..."<<endl;
int t1 = time(NULL), t2 = time(NULL);
while(t2- t1 < 3)
t2 = time(NULL);
system("cls");
if(flag == 1)
MatrixChainMain();
else if(flag == 2)
LCSlengthMain();
else if(flag == 3)
KnapsackMain();
else
return;
}
void print(int **a, int **b, int n, int m, bool f)///f = 1代表矩阵a,b相同,否则不同
{
cout<<"等待3秒后输出"<<endl;
int t1 = time(NULL);
while(time(NULL) - t1 < 3) {}
system("cls");
if(f)
for(int i = 0; i <= n; i++)
{
for(int j = 0; j <= m; j++)
cout<<left<<fixed<<setw(5)<<a[i][j];
cout<<endl;
}
else
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(i <= j)
cout<<left<<fixed<<setw(11)<<a[i][j];
else
cout<<left<<fixed<<setw(11)<<b[j][i];
}
cout<<endl;
}
}
///------------------------------------------------------矩阵连乘
void MatrixChainMain()
{
cout<<"-------------------------------------------------"<<endl;
cout<<" 矩阵连乘界面 "<<endl;
int n, i;
cout<<"输入矩阵个数n = ";
cin>>n;
cout<<"输入"<<n<<"个矩阵的维数:";
for(i = 0; i < n; i++)
cin>>Di[i].n>>Di[i].m;
int **s = new int*[n];
for(int i = 0; i < n; i++)
s[i] = new int[n];
int **bm = new int*[n];
for(int i = 0; i < n; i++)
bm[i] = new int[n];
MatrixChain(Di, s, bm, n);
cout<<"矩阵相乘的次序为:";
print1(s,0,n-1);
cout<<endl;
int flag;
cout<<"输入0返回主菜单,输入1重新演示,输入2进入最长公共子序列界面,输入3进入0-1背包界面,输入4结束。"<<endl;
cin>>flag;
while(flag != 0 && flag != 1 && flag != 2 && flag != 3 &&flag != 4)
{
cout<<"输入错误,请按以下要求重新输入!"<<endl;
cout<<"输入0返回主菜单,输入1重新演示,输入2进入最长公共子序列界面,输入3进入0-1背包界面,输入4结束。"<<endl;
cin>>flag;
}
cout<<"3秒后进入新界面..."<<endl;
int t1 = time(NULL), t2 = time(NULL);
while(t2- t1 < 3)
t2 = time(NULL);
system("cls");
if (flag == 0)
TheMain();
else if(flag == 1)
MatrixChainMain();
else if(flag == 2)
LCSlengthMain();
else if(flag == 3)
KnapsackMain();
else
return;
}
void MatrixChain(MatrixDimension MD[], int **s, int **bestnum, int n)
{
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
if(i == j)
bestnum[i][j] = 0;
else
{
bestnum[i][j] = -1;
s[i][j] = -1;
}
}
print(bestnum, s, n, n, 0);
for(int r = 1; r < n; r++) ///r代表第r条对角线
{
for(int i = 0; i < n - r; i++)
{
int k = i + r;
bestnum[i][k] = Inf;
for(int j = i; j < k; j++) ///接下来求从第i个矩阵到第k个矩阵的最优值及最优解
{
int temp = bestnum[i][j] + bestnum[j+1][k] + MD[i].n * MD[j].m * MD[k].m;
if(bestnum[i][k] >= temp)
{
bestnum[i][k] = temp;
s[i][k] = j;
}
}
}
print(bestnum, s, n, n, 0);
}
}
void print1(int **s, int l, int r)
{
if(l >= r)
{
cout<<"A"<<l;
return ;
}
int temp = s[l][r];
cout<<"(";
print1(s,l,temp);
print1(s,temp+1,r);
cout<<")";
}
///-------------------------------------------------最长公共子序列
void LCSlengthMain()
{
cout<<"-------------------------------------------------"<<endl;
cout<<" 最长公共子序列界面 "<<endl;
int n, m;
cout<<"输入序列1的长度n = ";
cin>>n;
cout<<"输入序列2的长度m = ";
cin>>m;
char *s1 = new char[n+1];
char *s2 = new char[m+1];
cout<<"输入第一个序列的元素:";
for(int i = 1; i <= n; i++)
cin>>s1[i];
cout<<"输入第二个序列的元素:";
for(int i = 1; i <= m; i++)
cin>>s2[i];
int **c = new int*[n+1]; ///c[i][j]存储s1[i],s2[j]的最长公共子序列长度
int **b = new int*[n+1]; ///b[i][j]表示c[i][j]是由哪个子问题得来的,作用是方便输出,有没有无所谓。
for(int i = 0; i <= n; i++)
{
c[i] = new int[m+1];
b[i] = new int[m+1];
}
LCSlength(n, m, s1, s2, c, b);
cout<<"最长子序列长度为:"<<c[n][m]<<endl;
cout<<"最长子序列为:";
print2(s1, s2, n, m, c);
cout<<endl;
int flag;
cout<<"输入0返回主菜单,输入1重新演示,输入2进入矩阵连乘界面,输入3进入0-1背包界面,输入4结束。"<<endl;
cin>>flag;
while(flag != 0 && flag != 1 && flag != 2 && flag != 3 &&flag != 4)
{
cout<<"输入错误,请按以下要求重新输入!"<<endl;
cout<<"输入0返回主菜单,输入1重新演示,输入2进入矩阵连乘界面,输入3进入0-1背包界面,输入4结束。"<<endl;
cin>>flag;
}
cout<<"3秒后进入新界面..."<<endl;
int t1 = time(NULL), t2 = time(NULL);
while(t2- t1 < 3)
t2 = time(NULL);
system("cls");
if (flag == 0)
TheMain();
else if(flag == 2)
MatrixChainMain();
else if(flag == 1)
LCSlengthMain();
else if(flag == 3)
KnapsackMain();
else
return;
}
void LCSlength(int n, int m, char *s1, char *s2, int **c, int **b)
{
for(int i = 0; i <= n; i++)
for(int j = 0; j <= m; j++)
{
if(i == 0 || j == 0)
c[i][j] = 0;
else
c[i][j] = -1;
}
print(c, c, n, m, 1);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(s1[i] == s2[j])
{
c[i][j] = c[i-1][j-1] + 1;
b[i][j] = 0;
}
else
{
if(c[i-1][j] > c[i][j-1])
{
c[i][j] = c[i-1][j];
b[i][j] = 1;
}
else
{
c[i][j] = c[i][j-1];
b[i][j] = 2;
}
}
}
print(c, c, n, m, 1);
}
}
void print2(char *s1, char *s2, int nr, int mr, int **c)
{
if(nr == 0 || mr == 0)
return;
if(c[nr][mr] == c[nr-1][mr-1] + 1)
{
print2(s1,s2,nr-1,mr-1,c);
cout<<s1[nr];
}
else
{
if(c[nr][mr] == c[nr-1][mr])
print2(s1,s2,nr-1,mr,c);
else
print2(s1,s2,nr,mr-1,c);
}
}
///--------------------------------------------------------0-1背包
void KnapsackMain()
{
cout<<"-------------------------------------------------"<<endl;
cout<<" 0-1背包界面 "<<endl;
int c;
cout<<"输入背包容量:";
cin>>c;
int n;
cout<<"输入物品数量:";
cin>>n;
int *w = new int[n];
cout<<"输入每个物品的重量:";
for(int i = 0; i < n; i++)
cin>>w[i];
int *v = new int[n];
cout<<"输入每个物品的价值:";
for(int i = 0; i < n; i++)
cin>>v[i];
int **bv = new int*[n];
for(int i = 0; i < n; i++)
bv[i] = new int[c+1];
Knapsack(bv, n, w, v, c);
cout<<"背包可装入的最大价值为:"<<bv[0][c]<<endl;
cout<<"背包装入物品的一种方案为:"<<endl;
print3(bv,w,c,n);
int flag;
cout<<"输入0返回主菜单,输入1重新演示,输入2进入最长公共子序列界面,输入3进入矩阵连乘界面,输入4结束。"<<endl;
cin>>flag;
while(flag != 0 && flag != 1 && flag != 2 && flag != 3 &&flag != 4)
{
cout<<"输入错误,请按以下要求重新输入!"<<endl;
cout<<"输入0返回主菜单,输入1重新演示,输入2进入最长公共子序列界面,输入3进入矩阵连乘界面,输入4结束。"<<endl;
cin>>flag;
}
cout<<"3秒后进入新界面..."<<endl;
int t1 = time(NULL), t2 = time(NULL);
while(t2- t1 < 3)
t2 = time(NULL);
system("cls");
if (flag == 0)
TheMain();
else if(flag == 3)
MatrixChainMain();
else if(flag == 2)
LCSlengthMain();
else if(flag == 1)
KnapsackMain();
else
return;
}
void Knapsack(int **bestValue, int n, int *weight, int *value, int Capacity)
{
int jMax = (weight[n-1] - 1) < Capacity ? (weight[n-1] - 1) : Capacity;
for(int i = 0; i < n; i++)
for(int j = 0; j <= Capacity; j++)
{
if(i == n - 1 && j <= jMax)
bestValue[i][j] = 0;
else if(i == n - 1 && j > jMax)
bestValue[i][j] = value[n - 1];
else
bestValue[i][j] = -1;
}
print(bestValue, bestValue, n - 1, Capacity, 1);
for(int i = n - 2; i > 0; i--)
{
for(int j = 0; j <= Capacity; j++)
{
int temp1 = j - weight[i];
if(temp1 < 0)
bestValue[i][j] = bestValue[i+1][j];
else
{
int temp2 = bestValue[i+1][temp1] + value[i];
bestValue[i][j] = temp2 > bestValue[i+1][j] ? temp2 : bestValue[i+1][j];
}
}
print(bestValue, bestValue, n - 1, Capacity, 1);
}
bestValue[0][Capacity] = bestValue[1][Capacity];
int temp = Capacity - weight[0];
if(temp >= 0)
bestValue[0][Capacity] = bestValue[0][Capacity] > ( bestValue[1][temp] + value[0]) ? bestValue[0][Capacity] : ( bestValue[1][temp] + value[0]);
print(bestValue, bestValue, n - 1, Capacity, 1);
}
void print3(int **bv, int *w, int c, int n)
{
int j = c;
for(int i = 0; i < n - 1; i++)
{
if(bv[i][j] == bv[i+1][j])
continue;
cout<<"物品"<<i + 1<<" ";
j = j - w[i];
}
if(bv[n - 1][j] == 0)
cout<<endl;
else
cout<<"物品"<<n<<endl;
}
来源:CSDN
作者:Only*
链接:https://blog.csdn.net/weixin_43040863/article/details/103715372