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", ¬a1);
while(nota1 == 0){
scanf("%i", ¬a1);
}
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", ¬a1);
}
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?
– user201641
No, it is required. You as a user can for as many as 5 want, but the ones you report have to be used.
– Vitória Rocha
If notes that enter do not generate exact change, you print a message "invalid values"
– Vitória Rocha