HTML - aiuudaa

 
Vista:

aiuudaa

Publicado por ernesto (1 intervención) el 12/05/2023 04:50:30
# include<stdio.h>

void knapsack(int n, float weight[], float profit[], float capacity) {
float x[20], tp = 0;
int i, j, u;
u = capacity;

for (i = 0; i < n; i++)
x[i] = 0.0;

for (i = 0; i < n; i++) {
if (weight[i] > u)
break;
else {
x[i] = 1.0;
tp = tp + profit[i];
u = u - weight[i];
}
}

if (i < n)
x[i] = u / weight[i];

tp = tp + (x[i] * profit[i]);

printf("\nThe result vector is:- ");
for (i = 0; i < n; i++)
printf("%f\t", x[i]);

printf("\nMaximum profit is:- %f", tp);

}

int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;

printf("\nEnter the no. of objects:- ");
scanf("%d", &num);

printf("\nEnter the wts and profits of each object:- ");
for (i = 0; i < num; i++) {
scanf("%f %f", &weight[i], &profit[i]);
}

printf("\nEnter the capacityacity of knapsack:- ");
scanf("%f", &capacity);

for (i = 0; i < num; i++) {
ratio[i] = profit[i] / weight[i];
}

for (i = 0; i < num; i++) {
for (j = i + 1; j < num; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;

temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;

temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}

knapsack(num, weight, profit, capacity);
return(0);
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
-3
Responder
sin imagen de perfil

aiuudaa

Publicado por antonio (19 intervenciones) el 12/05/2023 12:15:28
Este es un código c++ no es HTML, tampoco has dicho cual era la pregunta el código funciona, otra cosa es que no desarrolla lo que tu quieres que realice dicho código. Así que podrías responder cual era la pregunta o también puedes hacerla en la sección de c++. Un saludo.
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
#include <iostream>
 
# include<stdio.h>
 
void knapsack(int n, float weight[], float profit[], float capacity) {
float x[20], tp = 0;
int i, j, u;
u = capacity;
 
for (i = 0; i < n; i++)
x[i] = 0.0;
 
for (i = 0; i < n; i++) {
if (weight[i] > u)
break;
else {
x[i] = 1.0;
tp = tp + profit[i];
u = u - weight[i];
}
}
 
if (i < n)
x[i] = u / weight[i];
 
tp = tp + (x[i] * profit[i]);
 
printf("\nThe result vector is:- ");
for (i = 0; i < n; i++)
printf("%f\t", x[i]);
 
printf("\nMaximum profit is:- %f", tp);
 
}
 
int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
 
printf("\nEnter the no. of objects:- ");
scanf("%d", &num);
 
printf("\nEnter the wts and profits of each object:- ");
for (i = 0; i < num; i++) {
scanf("%f %f", &weight[i], &profit[i]);
}
 
printf("\nEnter the capacityacity of knapsack:- ");
scanf("%f", &capacity);
 
for (i = 0; i < num; i++) {
ratio[i] = profit[i] / weight[i];
}
 
for (i = 0; i < num; i++) {
for (j = i + 1; j < num; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
 
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
 
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
 
knapsack(num, weight, profit, capacity);
return(0);
}
Captura
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar