A job week3

The meaning of problems:
Given n, k, and the number S n, then the number n take any number of k, so that the sum is equal to s.
input:
the number of the input line of the first group of
the second row input n, k, s
third row of n data input
output:
output quantity intended to meet the problem
Sample input:
. 1
10. 3 10
. 1. 5. 4. 3 2. 6. 7. 9 10. 8
Sample the Output:
4
ideas:
using recursive method to solve it. The retrieval of data need to be calculated is determined continuously inserted into a container. When the content of the container to determine the amount of data selected. Defining a function, by recursively call this function, when the data has not traverse end, and the retrieved data amount greater than 0 and less than k, recursively updated once i. Traversing the data into the array to store, in a recursive call, I continue to add a value for the sum of s- pushed updated data, the data in the pop out, if the content of the container is equal to the last memory data k and sum value becomes 0 at this time it indicates that the data is the combination of the container in question is intended to claim all of the final output quantity intended to meet the requirements of the problem.

Code:

#include<iostream>
#include<algorithm> 
#include<vector>
using namespace std;
int a,b,c,cnn=0;
int v[16];
void f(int i,int sum,vector<int> &cm)
{
 if(cm.size()==b&&sum==0)
 {
  cnn++;
  return ;
 }
 if(i>=a||cm.size()>b||sum<0)
 return;
 f(i+1,sum,cm);
 cm.push_back(v[i]);
 f(i+1,sum-v[i],cm);
 cm.pop_back();
}
int main()
{
 int n;
 cin>>n;
 while(n--)
 {
  cnn=0;
  vector<int> temp;
  cin>>a>>b>>c;
  for(int i=0;i<a;i++)
  {
   cin>>v[i];
  }
  f(0,c,temp);
  cout<<cnn<<endl;
 }
 return 0; 
}
Published 19 original articles · won praise 0 · Views 215

Guess you like

Origin blog.csdn.net/weixin_45117273/article/details/104771466