Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int main()
- {
- int amount;
- int totalcoin;
- scanf("%d %d", &amount, &totalcoin);
- int coin[11111];
- int i;
- for(i=0;i<totalcoin;i++){
- scanf("%d", &coin[i]);
- }
- int row=totalcoin;
- int col=amount+1;
- int table[row][col];
- int j;
- for(i=0;i<row;i++){
- table[i][0]=0;
- }
- for(j=0;j<col;j++){
- table[0][j]=j/coin[0];
- }
- for(i=1;i<row;i++){
- for(j=1;j<col;j++){
- if(coin[i]>j){
- table[i][j]=table[i-1][j];
- }
- else{
- if(table[i-1][j]<table[i][j-coin[i]]+1){
- table[i][j]=table[i-1][j];
- }
- else{
- table[i][j]=table[i][j-coin[i]]+1;
- }
- }
- }
- }
- printf("Coin needed %d\n", table[row-1][col-1]);
- i=row-1;
- j=col-1;
- int a[1111];
- int idx=0;
- while(1){
- if(table[i][j]==0) break;
- if(table[i][j]!=table[i-1][j]){
- a[idx]=coin[i];
- idx++;
- j-=coin[i];
- }
- else{
- i--;
- }
- }
- printf("Needed coins are: ");
- for(i=idx-1;i>=0;i--){
- printf("%d ", a[i]);
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement