Algorithm questions for recent interviews

Source: Internet
Author: User

A set of x has n elements that are different from each other. It uses an indefinite number of elements in the set to form a sequence for s and obtain all the matching sequences. The elements can be reused, sequence is not considered if the number of elements is the same.

For example, if the set is x = {2, 3, 4, 5, 7}; n = 5, s = 12, the following sequence can be obtained:

2 2 2 2 2
2 2 2 2 4
2 2 2 3 3
2 2 3 5
2 2 4 4
2 3 3 4
2 3 7
2 5 5
3 3 3 3
3 4 5
4 4 4
5 7


 

#include <iostream>   #include <vector>   using namespace std;  int PushVector(int *x,int n,int s,vector<int> &tablelist,int Position)  {      if(s<0)      {          tablelist.clear();          return 0;      }      else if(s==0)      {          for(int i=0;i<tablelist.size();i++)          {              cout<<tablelist[i]<<"\t";          }          cout<<"\n";                    return 0;      }      else if(s>0)      {          for(int i=Position;i<n;i++)          {              vector<int> newtablelist;              for (int j=0;j<tablelist.size();j++)              {                  newtablelist.push_back(tablelist[j]);              }              newtablelist.push_back(x[i]);              int news = s-x[i];              //cout<<i<<","<<news<<"\n";               PushVector(x,n,news,newtablelist,i);          }          }      else      {          }      return 0;  }      /* run this program using the console pauser or add your own getch, system("pause") or input loop */  int FindResult(int * x,int n,int s)  {      vector<int> tablelist;      tablelist.clear();      PushVector(x,n,s,tablelist,0);      return 0;  }          int main(int argc, char** argv)   {      int x[]= {2,3,4,5,7};      int n =5;      int s=12;      FindResult(x,n,s);      system("pause");      return 0;  }  #include <iostream>#include <vector>using namespace std;int PushVector(int *x,int n,int s,vector<int> &tablelist,int Position){if(s<0){tablelist.clear();return 0;}else if(s==0){for(int i=0;i<tablelist.size();i++){cout<<tablelist[i]<<"\t";}cout<<"\n";return 0;}else if(s>0){for(int i=Position;i<n;i++){vector<int> newtablelist;for (int j=0;j<tablelist.size();j++){newtablelist.push_back(tablelist[j]);}newtablelist.push_back(x[i]);int news = s-x[i];//cout<<i<<","<<news<<"\n";PushVector(x,n,news,newtablelist,i);}}else{}return 0;}/* run this program using the console pauser or add your own getch, system("pause") or input loop */int FindResult(int * x,int n,int s){vector<int> tablelist;tablelist.clear();PushVector(x,n,s,tablelist,0);return 0;}int main(int argc, char** argv) {int x[]= {2,3,4,5,7};int n =5;int s=12;FindResult(x,n,s);system("pause");return 0;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.