-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamiczne.c
90 lines (80 loc) · 1.54 KB
/
dynamiczne.c
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
#include <stdio.h>
#include <stdlib.h>
int C,n;
typedef FILE* plik;
plik dane;
void print_tab(int tab[n+1][C+1],int C, int n){
int i,j;
for(i=0;i<=n;i++){
for(j=0;j<=C;j++){
printf("%d ",tab[i][j]);
}
printf("\n");
}
printf("\n");
}
int maximum(int a, int b){
if(a>b){return a;}else
{return b;}
}
void create_tab(int tab[n+1][C+1], int tab1[n+1][2],int C, int n){
int i,j;
for(i=0;i<=C;i++){
tab[0][i]=0;
}
for(i=0;i<=n;i++){
tab[i][0]=0;
}
for(i=1;i<=n;i++){
for(j=1;j<=C;j++){
if(tab1[i][1]>j){
tab[i][j]=tab[i-1][j];
}else{
tab[i][j]=maximum(tab[i-1][j],tab[i-1][j-tab1[i][1]]+tab1[i][0]);
}}
}}
void pd(int tab[n+1][C+1],int tab1[n][2]){
int i,j;
i=n;
j=C;
while(i>0 && j>0){
while(tab[i][j]==tab[i-1][j]){
i=i-1;
}
printf("%d ",i);
j=j-tab1[i][1];
i=i-1;
}
}
void bf(int tab1[n][2]){
}
int main()
{
srand(time(NULL));
int p,w,i;
dane=fopen("dane.txt","w");
C=20;
n=10;
//fprintf(dane,"%d\n%d\n",C,n);
for(i=0;i<10000;i++){
p=rand()%10+1;
w=rand()%10+1;
fprintf(dane,"%d %d\n",p,w);
}
dane=fopen("dane.txt","r");
//fscanf(dane,"%d\n",&C);
//fscanf(dane,"%d\n",&n);
int tab1[n+1][2];
for(i=1;i<=n;i++){
fscanf(dane,"%d ",&tab1[i][0]);
fscanf(dane,"%d\n",&tab1[i][1]);
printf("%d %d\n",tab1[i][0],tab1[i][1]);
}
printf("\n");
int tab[n+1][C+1];
create_tab(tab,tab1,C,n);
print_tab(tab,C,n);
pd(tab,tab1);
bf(tab1);
fclose(dane);
}