Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*****************************************************************************
- * Contact : codebits@esdis.es *
- * This program is free software; you can redistribute it and/or *
- * modify it under the terms of the GNU General Public License *
- * version 3 as published by the Free Software Foundation. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with this program; if not, write to the Free Software *
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, *
- * USA. *
- * *
- * The above copyright notice and this permission notice shall be included in *
- * all copies or substantial portions of the Software. *
- *****************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- // Desde aqui se puede tocar
- int billetes_atm[7]={1,1,0,0,0,0,1000}; // Numero de billetes de cada tipo, coincidir en tamaño con otros vectores
- int billetes_valor[7]={500,200,100,50,20,10,5}; // Valor de los billetes
- int billetes_resultado[7]={0,0,0,0,0,0,0}; // Vector donde va el resultado
- int objetivo=5000; // Target, cantidad de dinero a retirar. Es un INT, máximo 32767, por favor
- // Hasta aqui se puede tocar
- int max_billetes;
- int profundidad=0;
- void echar_billetes() {
- int size, x, t_linea, t_total=0;
- size=sizeof(billetes_atm) / sizeof(billetes_atm[0]);
- for(x=0 ; x<size ; x++) {
- if(billetes_resultado[x]) {
- t_linea=billetes_valor[x] * billetes_resultado[x];
- t_total+=t_linea;
- printf("Billetes de %d\tx %d\t= %d\n", billetes_valor[x], billetes_resultado[x], t_linea);
- }
- }
- printf("---------------------------------\n");
- printf("Total\t\t\t= %d\n", t_total);
- }
- void calculo_billetes(int objetivo) {
- int size, x, y;
- size=sizeof(billetes_atm) / sizeof(billetes_atm[0]);
- if(profundidad>=max_billetes) exit(-1); // No solución
- for(x=0 ; x<size ; x++) {
- if(billetes_valor[x]<=objetivo && billetes_atm[x]>0) {
- billetes_atm[x]--; billetes_resultado[x]++; objetivo-=billetes_valor[x]; profundidad++;
- if(!objetivo) {
- echar_billetes(); // Solución encontrada
- exit(-1);
- } else {
- calculo_billetes(objetivo);
- billetes_atm[x]++; billetes_resultado[x]--; objetivo+=billetes_valor[x]; profundidad--;
- }
- }
- }
- }
- int main() {
- int size, x;
- size=sizeof(billetes_atm) / sizeof(billetes_atm[0]);
- for(x=0 ; x<size ; x++) {
- max_billetes+=billetes_atm[x]; // Calculo del numero de billetes que hay en el cajero
- }
- calculo_billetes(objetivo); // Llamada a la función recursiva
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement