Advertisement
bartekltg

numerologia 1

Aug 31st, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <utility>
  4. #include <tuple>
  5. #include <vector>
  6. #include <stack>
  7. #include <algorithm>
  8. #include <iterator>
  9.  
  10.  
  11. using namespace std;
  12.  
  13.  
  14. int numfun (string s){
  15.     int accu=0;
  16.     for (auto c : s){
  17.         accu+= toupper(c)-'A'+1;
  18.     }
  19.     return accu;
  20. }
  21.  
  22.  
  23. struct szperacz_kombinatoryczny{
  24.     vector <string> wyrazy;
  25.     vector <int> wartosci;
  26.  
  27.     int cel;
  28.     int max_l;
  29.     szperacz_kombinatoryczny(int cel_, int max_l_);
  30.     void wypluj();
  31.  
  32.     vector<int > St; //stos na indeksu wyrazów
  33.  
  34.     void iter(int j, int sum , int level);
  35.  
  36. };
  37.  
  38. void szperacz_kombinatoryczny::iter(int j, int sum, int level){
  39.     if (sum==cel){
  40.         wypluj();
  41.         return;
  42.     }
  43.     if (level>=max_l) return;
  44.  
  45.     for (int i=j; i<wartosci.size();i++){
  46.         if (sum+wartosci[i]>cel) continue;
  47.         St.push_back(i);
  48.         iter(i+1,sum+wartosci[i], level+1);
  49.         St.pop_back();
  50.     }
  51.  
  52.  
  53. }
  54.  
  55. void szperacz_kombinatoryczny::wypluj(){
  56.     for (auto i: St){
  57.         cout<<wyrazy[i]<<" ";
  58.     }
  59.     cout<<"\n";
  60.  
  61. }
  62.  
  63. szperacz_kombinatoryczny::szperacz_kombinatoryczny(int cel_, int max_l_){
  64.     string s;
  65.  
  66.     cel = cel_;
  67.     max_l = max_l_;
  68.     while (cin){
  69.         cin>>s;
  70.         wyrazy.push_back(s);
  71.     }
  72.     sort(begin(wyrazy), end(wyrazy));
  73.     wyrazy.erase( unique(begin(wyrazy), end(wyrazy)), end(wyrazy) );
  74.  
  75.     cout<<"załadowano "<<wyrazy.size()<<" unikalnych wyrazów"<<endl;
  76.  
  77.     transform( begin(wyrazy), end(wyrazy), back_inserter(wartosci),  [](const string &s){ return numfun(s);} );
  78. }
  79.  
  80.  
  81.  
  82.  
  83. int main(int argc, char *argv[])
  84. {
  85.     szperacz_kombinatoryczny sp(666,5);
  86.  
  87.     sp.iter(0,0,0);
  88.  
  89.  
  90.  
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement