Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- typedef struct{
- float peso, valor;
- }item;
- void cadastrar_itens(int n, item v[]);
- float maior(float a, float b);
- float mochila(int n, item v[], float peso_max);
- int main()
- {
- item v[1000];
- int n;
- float peso_max;
- printf("Informe a qtd de itens: ");
- scanf("%d", &n);
- cadastrar_itens(n, v);
- printf("Informe o peso maximo da mochila: ");
- scanf("%f", &peso_max);
- printf("O maior valor que a mochila suporta vale: %f\n", mochila(n, v, peso_max));
- }
- void cadastrar_itens(int n, item v[])
- {
- for(int i = 0; i < n; i++)
- {
- printf("Informe o peso do item: ");
- scanf("%f", &v[i].peso);
- printf("Informe o valor do item: ");
- scanf("%f", &v[i].valor);
- }
- }
- float maior(float a, float b)
- {
- if(a > b)
- {
- return(a);
- }
- else
- {
- return(b);
- }
- }
- float mochila(int n, item v[], float peso_max)
- {
- if(n < 0)
- {
- return(0);
- }
- else
- {
- if(v[n].peso <= peso_max)
- {
- return( maior( ( v[n].valor + mochila(n - 1, v, peso_max - v[n].peso)) , (mochila(n-1, v, peso_max)) ) );
- }
- else
- {
- return(mochila(n-1, v, peso_max));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement