Doubt in the use of the for

Asked

Viewed 54 times

-1

I need to make a code to solve the following question: I have a basket with capacity C, I have 3 types of fruit, I will receive the amount one per line, to put all of them in the basket, the collection of these fruits takes 1 minute. For example:
Basket = 12
A = 4
B = 3
C = 3
Output: 1
Basket = 10
A = 5
B = 5
C = 5
Output = 2 (because it took two trips, ie two minutes.)
How to use for? I seek the simplest form.

Follow the beginning of my code:

#include <stdio.h>

int main() {

    int c = 0, a = 0, b = 0, c = 0, min = 1, sub = 0;

    scanf("%d\n%d\n%d\n%d", & c, & a, & b, & c);

    int vtl = a + b + c; //valor total das frutas

    if (c >= vtl) {

        printf("%d\n", min);

    } else if (vtl > c) {

        for ()

    }
}

1 answer

0


To do this just decrease the maximum capacity of the basket in the total amount of fruit at each loop iteration, example:

quantity of fruit = 12
basket capacity = 6

  • First iteration: 12 - 6 = 6
  • Second iteration: 6 - 6 = 0

As two iterations were used the time required was 2 minutes.

#include <stdio.h>

void get_input(const char *texto, int *variavel);

int main(void)
{
  int fruta_a, fruta_b, fruta_c, capacidade_cesta;

  // Pega as entradas do usuário
  get_input("Digite a capacidade máxima da cesta: ", &capacidade_cesta);
  get_input("Digite a quantidade de fruta A: ", &fruta_a);
  get_input("Digite a quantidade de fruta B: ", &fruta_b);
  get_input("Digite a quantidade de fruta C: ", &fruta_c);

  // Variáveis para calcular o tempo total
  int tempo_total = 0;
  int total_de_frutas = fruta_a + fruta_b + fruta_c;

  // Calcula o tempo total
  // A cada iteração é decrescido do
  // total a capacidade da cesta
  for(; total_de_frutas > 0; total_de_frutas -= capacidade_cesta)
    tempo_total++;


  // Mostra a quantidade total de minutos
  printf("O tempo total foi de: %d minutos\n", tempo_total);

  return 0;
}

void get_input(const char *texto, int *variavel)
{
  printf(texto);
  scanf("%d", variavel);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.