c_cpp Min Coins

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp Min Coins相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

void minCoins(vector <int> coins, int sum){
    int n = coins.size();
    vector <int> res(sum+1, 10000), sol(sum+1, -1);
    res[0] = 0;
    for(int i=0; i<n; i++){
        for(int j=1; j<=sum; j++){
            if(coins[i] <= j){
                int temp = 1+res[j-coins[i]];
                if(temp < res[j]){
                    res[j] = temp;
                    sol[j] = i;
                }
            }
        }
    }
    if(res[sum] == 10000) cout << "Not possible" << endl;
    else{
        cout << res[sum] << " coins needed\nand the coins are ";
        int i = sum;
        do{
            cout << coins[sol[i]] << " ";
            i -= coins[sol[i]];
        }while(i>0);
    }
}

int main(){
    int n, sum;
    cout << "enter no. of types of coins" << endl;
    cin >> n;
    vector <int> coins(n);
    cout << "enter values of coins" << endl;
    for(int i=0; i<n; i++) cin >> coins[i];
    cout << "enter total sum" << endl;
    cin >> sum;
    minCoins(coins,sum);

    return 0;
}

以上是关于c_cpp Min Coins的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 155. Min Stack

c_cpp #rcpp #min #max

c_cpp Rcpp min,max

c_cpp dlib min_cut

c_cpp Cpp / C ++ min,max

c_cpp #cpp#min_max2