Advertisement
yukisaw

36 apples, 4 different backets, all variants

Oct 22nd, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4.  
  5. string IntToStr(int i)
  6. {
  7.     stringstream ss;
  8.     ss << i;
  9.     return ss.str();
  10. }
  11.  
  12. string calculate(int *mas, int i, int max, string s)
  13. {
  14.     if (mas[i]<=max && max!=0)
  15.     {
  16.         if (i>0 && mas[i]!=max)
  17.         {
  18.             string old_s = s;
  19.             for (int d=max/mas[i]; d>=0; d--)
  20.             {
  21.                 s= old_s;
  22.                 if (d>0) s.append(IntToStr(mas[i])+ "(" + IntToStr(d) + ") ");
  23.                 calculate(mas, i-1, max-(d*mas[i]), s );
  24.             }
  25.             return "";
  26.         }
  27.         else
  28.         {
  29.             if (((max+mas[i])%mas[i])==0)
  30.             {
  31.                 s.append(IntToStr(mas[i])+"(" + IntToStr(max/mas[i])+ ")");
  32.                 cout << s << endl;
  33.                 return "";
  34.             }
  35.             else {calculate(mas, i-1, max, s); return "";}
  36.         }
  37.     }
  38.     else
  39.     {
  40.         if (max==0) {cout << s << endl; return "";}
  41.         else if (i>0) { calculate(mas, i-1, max, s); return ""; }
  42.         else return "";
  43.     }
  44.  
  45. }
  46.  
  47. int C_PACK[] = {1,2,5,10};
  48. int mas_size = sizeof(C_PACK)/sizeof(int);
  49. int apples = 36;
  50.  
  51. int main()
  52. {
  53. cout<<"Начало:"<<endl;
  54. calculate(C_PACK, mas_size-1, apples, "Такой вариант: ");
  55. cout<<"Конец."<<endl;
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement