Advertisement
Guest User

4 bags, 9 balls, odd count in each bag

a guest
Aug 27th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <array>
  5. #include <map>
  6. #include <list>
  7. #include <algorithm>
  8. #include <sstream>
  9. #include <time.h>
  10. #include <memory>
  11. #include <set>
  12. #include <cstring>
  13.  
  14. #include <unordered_map>
  15. #include <string>
  16. #include <type_traits>
  17.  
  18. //#include <boost/thread.hpp>
  19. #include <vector>
  20. #include <iostream>
  21.  
  22. using namespace std;
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <iostream>
  27.  
  28. int countOdd(int n1, int n2, int n3, int n4)
  29. {
  30.     int nCount = 0;
  31.     if (n1 & 1) nCount++;
  32.     if (n2 & 1) nCount++;
  33.     if (n3 & 1) nCount++;
  34.     if (n4 & 1) nCount++;
  35.     return nCount; 
  36. }
  37.  
  38. void print(int count1)
  39. {
  40.     char buff[2];
  41.     cout << "(" << (count1 == 0 ? "" : itoa(count1, buff, 10));
  42. }
  43.  
  44. void print_deep(int count1, int count2, int count3, int count4)
  45. {
  46.     print(count1);
  47.     print(count2);
  48.     print(count3);
  49.     print(count4);
  50.     cout << "))))" << endl;
  51. }
  52.  
  53. void print_deep(int count1, int count2, int count3)
  54. {
  55.     print(count1);
  56.     print(count2);
  57.     print(count3);
  58.     cout << "))";
  59. }
  60.  
  61. void print_deep(int count1, int count2)
  62. {
  63.     print(count1);
  64.     print(count2);
  65.     cout << "))";
  66. }
  67.  
  68. void print_deep(int count1)
  69. {
  70.     print(count1);
  71.     cout << ")";
  72. }
  73.  
  74. void print (int count1, const string & str, int count2)
  75. {
  76.     //cout << "(" << (count1 == 0 ? "" : itoa(count1)) << str << "(" << (count2 == 0 ? "" : itoa(count2));
  77. }
  78.  
  79. int main()
  80. {
  81.     for (int cBag1 = 0; cBag1 <= 9; cBag1++)
  82.     {
  83.         for (int cBag2 = 0; cBag2 <= 9 - cBag1; cBag2++)
  84.         {
  85.             for (int cBag3 = 0; cBag3 <= 9 - cBag1 - cBag2; cBag3++)
  86.             {
  87.                 int cBag4 = 9 - cBag3 - cBag2 - cBag1;
  88.                 int cOdd = countOdd(cBag1, cBag2, cBag3, cBag4);
  89.                
  90.                 if (cOdd == 3)
  91.                 {
  92.                     // Even bag contains 1 or 3 odd bags; if 1, that one MAY contain the other 2 odd bags, or one of the other 2 remaining odd bags may contain the even bag AND other odd bag
  93.                     int even = !(cBag1 & 1) ? cBag1 : (!(cBag2 & 1) ? cBag2 : (!(cBag3 & 1) ? cBag3 : cBag4));
  94.                     vector<int> odd;
  95.                     if (cBag1 & 1) odd.push_back(cBag1);
  96.                     if (cBag2 & 1) odd.push_back(cBag2);
  97.                     if (cBag3 & 1) odd.push_back(cBag3);
  98.                     if (cBag4 & 1) odd.push_back(cBag4);
  99.                    
  100.                     print(even);
  101.                     print_deep(odd[0]);
  102.                     print_deep(odd[1]);
  103.                     print_deep(odd[2]);
  104.                     cout << ")" << endl;
  105.                    
  106.                     print_deep(even, odd[0]);
  107.                     print_deep(odd[1]);
  108.                     print_deep(odd[2]);
  109.                     cout << endl;
  110.                     print_deep(even, odd[1]);
  111.                     print_deep(odd[0]);
  112.                     print_deep(odd[2]);
  113.                     cout << endl;
  114.                     print_deep(even, odd[2]);
  115.                     print_deep(odd[1]);
  116.                     print_deep(odd[0]);
  117.                     cout << endl;
  118.                    
  119.                     print(even);
  120.                     print(odd[0]);
  121.                     print_deep(odd[1]);
  122.                     print_deep(odd[2]);
  123.                     cout << "))" << endl;                  
  124.                     print(even);
  125.                     print(odd[1]);
  126.                     print_deep(odd[0]);
  127.                     print_deep(odd[2]);
  128.                     cout << "))" << endl;              
  129.                     print(even);
  130.                     print(odd[2]);
  131.                     print_deep(odd[1]);
  132.                     print_deep(odd[0]);
  133.                     cout << "))" << endl;
  134.                    
  135.                     print(odd[0]);
  136.                     print_deep(odd[1]);
  137.                     print_deep(even, odd[2]);
  138.                     cout << ")" << endl;
  139.                     print(odd[0]);
  140.                     print_deep(odd[2]);
  141.                     print_deep(even, odd[1]);
  142.                     cout << ")" << endl;
  143.                     print(odd[1]);
  144.                     print_deep(odd[0]);
  145.                     print_deep(even, odd[2]);
  146.                     cout << ")" << endl;
  147.                     print(odd[1]);
  148.                     print_deep(odd[2]);
  149.                     print_deep(even, odd[0]);
  150.                     cout << ")" << endl;
  151.                 }
  152.                 else if(cOdd == 2)
  153.                 {
  154.                     // each even bag contains 1 of the odd bags, OR one even bag contains the other even bag, which itself contains an odd bag, OR one even bag contains an odd AND even bag,
  155.                     // with the contained even bag itself containing the other odd bag
  156.                     vector<int> even;
  157.                     vector<int> odd;
  158.                     (cBag1 & 1) ? odd.push_back(cBag1) : even.push_back(cBag1);
  159.                     (cBag1 & 1) ? odd.push_back(cBag2) : even.push_back(cBag2);
  160.                     (cBag1 & 1) ? odd.push_back(cBag3) : even.push_back(cBag3);
  161.                     (cBag1 & 1) ? odd.push_back(cBag4) : even.push_back(cBag4);
  162.                    
  163.                     print_deep(even[0], odd[0]);
  164.                     print_deep(even[1], odd[1]);
  165.                     cout << endl;
  166.                     print_deep(even[0], odd[1]);
  167.                     print_deep(even[1], odd[0]);
  168.                     cout << endl;
  169.                    
  170.                     print_deep(even[0], even[1], odd[0]);
  171.                     print_deep(odd[1]);
  172.                     cout << endl;
  173.                     print_deep(even[0], even[1], odd[1]);
  174.                     print_deep(odd[0]);
  175.                     cout << endl;
  176.                     print_deep(even[1], even[0], odd[0]);
  177.                     print_deep(odd[1]);
  178.                     cout << endl;
  179.                     print_deep(even[1], even[0], odd[1]);
  180.                     print_deep(odd[0]);
  181.                     cout << endl;
  182.                    
  183.                     print(even[0]);
  184.                     print_deep(odd[0]);
  185.                     print_deep(even[1], odd[1]);
  186.                     cout << ")" << endl;                   
  187.                     print(even[0]);
  188.                     print_deep(odd[1]);
  189.                     print_deep(even[1], odd[0]);
  190.                     cout << ")" << endl;                   
  191.                     print(even[1]);
  192.                     print_deep(odd[0]);
  193.                     print_deep(even[0], odd[1]);
  194.                     cout << ")" << endl;                   
  195.                     print(even[1]);
  196.                     print_deep(odd[1]);
  197.                     print_deep(even[0], odd[0]);
  198.                     cout << ")" << endl;
  199.                    
  200.                    
  201.                 }
  202.                 else // cOdd = 1
  203.                 {
  204.                     // the odd bag is contained in an even bag, which is contained in an even bag, which is itself in an even bag, so
  205.                     // ((((9)))), (2(2(4(1)))), (4((2(3)))), etc
  206.                     int odd = cBag1 & 1 ? cBag1 : (cBag2 & 1 ? cBag2 : (cBag3 & 1 ? cBag3 : cBag4));
  207.                     vector<int> even;
  208.                     if (!(cBag1 & 1)) even.push_back(cBag1);
  209.                     if (!(cBag2 & 1)) even.push_back(cBag2);
  210.                     if (!(cBag3 & 1)) even.push_back(cBag3);
  211.                     if (!(cBag4 & 1)) even.push_back(cBag4);
  212.                    
  213.                     print_deep(even[0], even[1], even[2], odd);
  214.                     print_deep(even[0], even[2], even[1], odd);
  215.                     print_deep(even[1], even[0], even[2], odd);
  216.                     print_deep(even[1], even[2], even[0], odd);                
  217.                     print_deep(even[2], even[0], even[1], odd);
  218.                     print_deep(even[2], even[1], even[0], odd);
  219.                 }              
  220.             }
  221.         }
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement