Last active
December 31, 2015 17:19
-
-
Save vlakam/8019183 to your computer and use it in GitHub Desktop.
Даны k предметов с массами m1, m2,...mk. Выделите все группы предметов, у которых сумма масс равна S.
http://breedpmnr.ru/i/6aea94f653115c4e53bbfc4d8e2d.jpg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <stack> | |
using namespace std; | |
stack<int> m; | |
vector<pair<bool,int>> items; | |
int ss=0; | |
inline int summ(std::stack<int> in) | |
{ | |
int s=0; | |
while(!in.empty()) | |
{ | |
s+=in.top(); | |
in.pop(); | |
} | |
return s; | |
} | |
inline void printstack(std::stack<int> in) | |
{ | |
while(!in.empty()) | |
{ | |
std::cout<< in.top() << " "; | |
in.pop(); | |
} | |
std::cout<<endl; | |
} | |
void push() | |
{ | |
for(size_t i=0;i<items.size();i++) | |
{ | |
if(items[i].first==false) | |
{ | |
items[i].first=true; | |
m.push(items[i].second); | |
int tsumm=summ(m); | |
if(tsumm==ss) | |
{ | |
printstack(m); | |
m.pop(); | |
items[i].first=false; | |
continue; | |
} | |
else if(tsumm>ss) | |
{ | |
m.pop(); | |
items[i].first=false; | |
continue; | |
} | |
else push(); | |
m.pop(); | |
items[i].first=false; | |
} | |
} | |
} | |
int main() | |
{ | |
ss=6; | |
items.push_back(pair<bool,int>(false,1)); | |
items.push_back(pair<bool,int>(false,3)); | |
items.push_back(pair<bool,int>(false,5)); | |
items.push_back(pair<bool,int>(false,2)); | |
items.push_back(pair<bool,int>(false,4)); | |
items.push_back(pair<bool,int>(false,6)); | |
push(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment