I must meet the condition that if 0 is my first value, I must read another instead

Asked

Viewed 38 times

1

The program must enter notes between 2 and 50 reais (2, 5, 10, 20, 50) and calculate the change value in these informed notes to 100 reais. If the first value is 0 I must read again another value but keep the others. Ex: {0 10 20}, ask to read another instead of 0, I enter with 5, stay {5 10 20} ( the change will be 20 20 20 20 10 5), output will be right, I just can’t fit this condition. You must maintain the condition that if 0 enters after a valid value the program does not read the variables after it. Ex: {10 0 20 50}, the program only considers before 0, ie 10. Contextualizing only for the same understanding, the result has to come out decreasing, as in the example I put, the higher value has priority, need to use the maximum of it and so on, if it is Ex: 20 and 10, the output will be {20 20 20 20 10 10} and if enter combinations not valid, the program has to print a message, ex: 50 20, does not give exact value to 100, so "not valid values". :)


#include <stdio.h>

int main(){
int troca_nota = 100, v[]={50,0,20,0,10,0,5,0,2,0},nota1,i,x,j;

scanf("%i", &nota1);

while(nota1 == 0){
    scanf("%i", &nota1);
    
}

while(nota1 != 0){
    troca_nota = troca_nota - nota1;
    if(nota1 == 50){
        v[1] = 1;
    } if(nota1 == 20){
        v[3] = 1;
    } if(nota1 == 10){
        v[5] = 1;
    } if(nota1 == 5){
        v[7] = 1;
    } if(nota1 == 2){
        v[9] = 1;
    }
    scanf("%i", &nota1);
} 

for(i=0;i<10;i++){
    if(i%2 == 1){
        if(v[i] > 0){
            if(troca_nota >= v[i-1]){
                x = troca_nota/v[i-1];
                troca_nota = troca_nota - (v[i-1]*x);
                v[i] = v[i] + x ;
            }
        }
    }
}

if(troca_nota != 0){
    printf("troca nao permitida");
} else{
    for(i=0;i < 10; i++){
        if(i % 2 == 1){
            if(v[i] > 0){
                for(j = 0; j < v[i];j++){
                    printf("%i \n", v[i-1]);
                }
            }   
        }
    }
}

return 0;

}

  • Is the algorithm required to use all the informed notes or can it use only a few? For example, the user informs {2, 5, 10}, but the algorithm uses only those of 10. This is valid?

  • No, it is required. You as a user can for as many as 5 want, but the ones you report have to be used.

  • If notes that enter do not generate exact change, you print a message "invalid values"

1 answer

0

It’s not complete, but it already has a basis:

/*
 * /questions/485948/preciso-atender-a-condi%c3%a7%c3%a3o-de-que-se-0-for-meu-primeiro-valor-eu-devo-ler-outro
 *
 * O usuário escolhe as notas e o algoritmo se vira para chegar em R$ 100 usando
 * todas as notas informadas.
 *
 * Notas permitidas: 2, 5, 10, 20 e 50.
 *
 * Cada nota só pode ser escolhida uma vez.
 */

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int _2;
    int _5;
    int _10;
    int _20;
    int _50;
} Notas;


int main() {
    const int objetivo = 100;

    // Indica as notas que devem ser usadas.
    Notas flag = (Notas) {1, 1, 1, 1, 1};

    // Indica a quantidade de cada nota que foi usada.
    Notas notas = (Notas) {5, 2, 1, 1, 1};

    // Se não puder usar notas de 50, usamos duas de 20 e uma de 10
    if (flag._50 == 0) {
        while (notas._50 > 0) {
            notas._50 -= 1;
            notas._20 += 2;
            notas._10 += 1;
        }
    }

    // Se não puder usar notas de 20, usamos duas de 10.
    if (flag._20 == 0) {
        while (notas._20 > 0) {
            notas._20 -= 1;
            notas._10 += 2;
        }
    }

    // Se não puder usar notas de 10, usamos duas de 5
    if (flag._10 == 0) {
        while (notas._10 > 0) {
            notas._10 -= 1;
            notas._5 += 2;
        }
    }

    // Se não puder usar notas de 5
    if (flag._5 == 0) {
        if (notas._5 % 2 == 0) {
            // Quantidade par de nota de 5, indica que podemos usar notas de 2
            while (notas._5 > 0) {
                notas._5 -= 2;
                notas._2 += 5;
            }
        } else {
            fprintf(stderr, "[ E ]: Impossivel resolver. \n");
            exit(EXIT_FAILURE);
        }
    }

    // Se não puder usar notas de 2, só podemos trocar por notas de 5 ou 10
    if (flag._2 == 0) {
        if (notas._2 % 5 == 0) {
            // Podemos trocar por notas de 5 ou 10
            if (flag._5 == 1) {
                // Vamos trocar por notas de 5
                while (notas._2 > 0) {
                    notas._2 -= 5;
                    notas._5 += 2;
                }
            } else if (flag._10 == 1) {
                // Vamos trocar por notas de 10
                while (notas._2 > 0) {
                    notas._2 -= 5;
                    notas._10 += 1;
                }
            } else {
                // Não dá para trocar
                fprintf(stderr, "[ E ]: Impossivel resolver. \n");
                exit(EXIT_FAILURE);
            }
        } else {
            fprintf(stderr, "[ E ]: Impossivel resolver. \n");
            exit(EXIT_FAILURE);
        }
    }

    printf("----------------------------------- \n");
    printf("%2d x R$  2.00 \n", notas._2);
    printf("%2d x R$  5.00 \n", notas._5);
    printf("%2d x R$ 10.00 \n", notas._10);
    printf("%2d x R$ 20.00 \n", notas._20);
    printf("%2d x R$ 50.00 \n", notas._50);
    printf("----------------------------------- \n");
    return EXIT_SUCCESS;
}

Browser other questions tagged

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