-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdongtai1.cpp
168 lines (150 loc) · 3.27 KB
/
dongtai1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
int value[5]={6,3,5,4,6};
int weight[5]={2,2,6,5,4};
#include <stdio.h>
int list[200][200];
int x[15];
int n;
int c;
int s;
int max (int a,int b)
{
if(a>b)return a;
else return b;
}
int ks(int n,int weight[],int value[],int x[],int c)
{
int i,j;
for(i=0;i<=n;i++)
list[i][0]=0;
for(j=0;j<=c;j++)
list[0][i]=0;
for(i=0;i<=n-1;i++)
for(j=0;j<=c;j++)
if(j<weight[i])
list[i][j]=list[i-1][j];
else
list[i][j]=max(list[i-1][j],list[i-1][j-weight[i]]+value[i]);
j=c;
for(i=n-1;i>=0;i--){
if(list[i][j]>list[i-1][j]){
x[i]=1;
j=j-weight[i];
}else x[i]=0;
}
printf("背包中的物品序列号:\n");
for(i=0;i<n;i++)
printf("%d\n",x[i]);
return list[n-1][c]; }
void main(){
int weight[15]={2,2,6,5,4};
int value[15]={6,3,5,4,6};
c=10;
n=5;
s=ks(n,weight,value,x,c);
printf("背包中的总价值:\n");
printf("%d\n",s);
}
投资
#include<stdio.h>
void Investing(int m,int n,int g[100][100]);
int main(){
int i,j,n,m,g[100][100]={0};
printf("请输入要录入的行数与列数(空格隔开) :");
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++){
printf("项目%d投资利润表:",i) ;
for(j=1;j<=m;j++){
scanf("%d",&g[i][j]);
}
}
Investing(m,n,g);
return 0;
}
void Investing(int m,int n,int g[100][100]){
int i,j,k,s;
int h[n+1],d[n+1][m+1];
for (i=2; i<=n; i=i+1){
for(j=0; j<=m; j=j+1)
d[1][j]=j;
for(j=0; j<=m; j=j+1){
h[j]=0;
for( k=0; k<=j; k=k+1){
s=g[i-1][k]+g[i][j-k];
if(s>h[j]) {
h[j]=s;
d[i][j]=j-k;
}
}
}
for(j=0; j<=m; j=j+1) g[i][j]=h[j];
}
s=m;
h[n]=d[n][m];
for (k=n-1; k>0; k=k-1)
{ s=s-h[k+1];
h[k]=d[k][s];
}
for(k=1; k<=n; k=k+1)
printf("项目%d:投资额:%d\n",k,h[k]) ;
printf("可获得最大利润额:%d\n",g[n][m]);
}
吾问无为谓
#include <stdio.h>
#include <string.h>
#define MAXLEN 100
void LCSLength(char *x, char *y, int m, int n, int c[][MAXLEN], int b[][MAXLEN])
{
int i, j;
for(i = 0; i <= m; i++)
c[i][0] = 0;
for(j = 1; j <= n; j++)
c[0][j] = 0;
for(i = 1; i<= m; i++)
{
for(j = 1; j <= n; j++)
{
if(x[i-1] == y[j-1])
{
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] = -1;
}
}
}
}
void PrintLCS(int b[][MAXLEN], char *x, int i, int j)
{
if(i == 0 || j == 0)
return;
if(b[i][j] == 0)
{
PrintLCS(b, x, i-1, j-1);
printf("%c ", x[i-1]);
}
else if(b[i][j] == 1)
PrintLCS(b, x, i-1, j);
else
PrintLCS(b, x, i, j-1);
}
int main(int argc, char **argv)
{
char x[MAXLEN] = {"ABCBDAB"};
char y[MAXLEN] = {"BDCABA"};
int b[MAXLEN][MAXLEN];
int c[MAXLEN][MAXLEN];
int m, n;
m = strlen(x);
n = strlen(y);
LCSLength(x, y, m, n, c, b);
PrintLCS(b, x, m, n);
return 0;
}