Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- using namespace std;
- string IntToStr(int i)
- {
- stringstream ss;
- ss << i;
- return ss.str();
- }
- string calculate(int *mas, int i, int max, string s)
- {
- if (mas[i]<=max && max!=0)
- {
- if (i>0 && mas[i]!=max)
- {
- string old_s = s;
- for (int d=max/mas[i]; d>=0; d--)
- {
- s= old_s;
- if (d>0) s.append(IntToStr(mas[i])+ "(" + IntToStr(d) + ") ");
- calculate(mas, i-1, max-(d*mas[i]), s );
- }
- return "";
- }
- else
- {
- if (((max+mas[i])%mas[i])==0)
- {
- s.append(IntToStr(mas[i])+"(" + IntToStr(max/mas[i])+ ")");
- cout << s << endl;
- return "";
- }
- else {calculate(mas, i-1, max, s); return "";}
- }
- }
- else
- {
- if (max==0) {cout << s << endl; return "";}
- else if (i>0) { calculate(mas, i-1, max, s); return ""; }
- else return "";
- }
- }
- int C_PACK[] = {1,2,5,10};
- int mas_size = sizeof(C_PACK)/sizeof(int);
- int apples = 36;
- int main()
- {
- cout<<"Начало:"<<endl;
- calculate(C_PACK, mas_size-1, apples, "Такой вариант: ");
- cout<<"Конец."<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement