obtain number of groups of the summarization in given value in a fixed array

 The outputs:

Inputs 
5 15         //first value is size of array, the second value is the value of summarization 
5 5 10 2 3  // the specific value in array 
outputs
4           //the Numbers of groups in this array the value of which is equal to the value 
             of summarization 

The corresponding codes 
 

# include <iostream>
# include <iomanip>
# include <vector>
using namespace std;
void initialization(int n, int sum, vector<int> &arr, vector<vector<int> > &dp); // initialize values in array 
void display_vec_matrix(vector<vector<int> >& dp , int row, int col);       // test the initialization function work or not 
int solutions(vector<int> &arr, vector<vector<int> > & dp);
int main()
{
    int n = 0, sum = 0;
    cin >> n >> sum; // the first element is the length of the vector; the second element 
                     // is the summarization of the subvector
    vector<int> arr;
    vector<vector<int>> dp;
    initialization(n, sum, arr, dp); 
    //display_vec_matrix( dp, n+1, sum + 1 );
    //cout << endl << arr[1] ;                
    /* The above two lines are created to test the initialization function work or not */
    cout << solutions(arr, dp) << endl;
    return 0;
}
void initialization(int n, int sum, vector<int> &arr, vector<vector<int> > &dp)
{
    int temp = 0;
    arr.push_back(0);
    for (int i = 0; i < n; i++){
        cin >> temp;
        arr.push_back(temp);
    }
    dp.resize(n+1, vector<int>(sum +1, 1));
    for (int i = 1; i < sum+1; i++){
        dp[0][i] = 0;
    }
}
void display_vec_matrix(vector<vector<int> > & dp, int row, int col)
{
    for (int i = 0; i < row; i++){
        for( int j = 0; j <col; j++){
            cout << setw(3) << dp[i][j];
        }
        cout << endl;
    }
}
int solutions(vector<int> &arr, vector<vector<int> > & dp)
{
    for (int i = 1; i < arr.size(); i++){
        for (int j = 1; j < dp[0].size(); j++){
            if (arr[i] > j){
                dp[i][j] = dp[i-1][j];
            } else {
                dp[i][j] = max((dp[i-1][j-arr[i]] + dp[i-1][j]), dp[i-1][j]); 
                //Being completely honest, the function of max is no use here 
                   // I just modify it and just use the first element, which turns out ok
            }
        }
    }
    return dp[dp.size()-1][dp[0].size()-1];
}

A little modification 

# include <iostream>
# include <iomanip>
# include <vector>
using namespace std;
void initialization(int n, int sum, vector<int> &arr, vector<vector<int> > &dp); // initialize values in array 
void display_vec_matrix(vector<vector<int> >& dp , int row, int col);       // test the initialization function work or not 
int solutions(vector<int> &arr, vector<vector<int> > & dp);
int main()
{
    int n = 0, sum = 0;
    cin >> n >> sum; // the first element is the length of the vector; the second element 
                     // is the summarization of the subvector
    vector<int> arr;
    vector<vector<int>> dp;
    initialization(n, sum, arr, dp); 
    //display_vec_matrix( dp, n+1, sum + 1 );
    //cout << endl << arr[1] ;                
    /* The above two lines are created to test the initialization function work or not */
    cout << solutions(arr, dp) << endl;
    return 0;
}
void initialization(int n, int sum, vector<int> &arr, vector<vector<int> > &dp)
{
    int temp = 0;
    arr.push_back(0);
    for (int i = 0; i < n; i++){
        cin >> temp;
        arr.push_back(temp);
    }
    dp.resize(n+1, vector<int>(sum +1, 1));
    for (int i = 1; i < sum+1; i++){
        dp[0][i] = 0;
    }
}
void display_vec_matrix(vector<vector<int> > & dp, int row, int col)
{
    for (int i = 0; i < row; i++){
        for( int j = 0; j <col; j++){
            cout << setw(3) << dp[i][j];
        }
        cout << endl;
    }
}
int solutions(vector<int> &arr, vector<vector<int> > & dp)
{
    for (int i = 1; i < arr.size(); i++){
        for (int j = 1; j < dp[0].size(); j++){
            if (arr[i] > j){
                dp[i][j] = dp[i-1][j];
            } else {
                dp[i][j] = dp[i-1][j-arr[i]] + dp[i-1][j];
            }
        }
    }
    return dp[dp.size()-1][dp[0].size()-1];
}

猜你喜欢

转载自blog.csdn.net/weixin_38396940/article/details/120977048