Advertisement
anas_harby

Untitled

Mar 24th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int A[5], vis[5], vis2[5], per[5];
  6. bool possible = false;
  7.  
  8. void solve2(int ans)
  9. {
  10.         bool f = false;
  11.         for(int i=1; i<5; i++)
  12.             if(!vis[i])
  13.                 f = true;
  14.         if(!f)
  15.             if(ans==23)
  16.                 possible = true;
  17.  
  18.     if(!possible)
  19.     {
  20.         for(int i=1; i<5; i++)
  21.         {
  22.             if(!vis[i])
  23.             {
  24.                 vis[i] = true;
  25.                 solve2(ans+per[i]);
  26.                 solve2(ans*per[i]);
  27.                 solve2(ans-per[i]);
  28.                 vis[i] = false;
  29.             }
  30.         }
  31.     }
  32. }
  33.  
  34. void solve(int k)
  35. {
  36.     if(!possible)
  37.     {
  38.         if(k==5)
  39.             solve2(per[0]);
  40.  
  41.         for(int i=0; i<5; i++)
  42.         {
  43.             if(!vis2[i])
  44.             {
  45.                 vis2[i] = true;
  46.                 per[k] = A[i];
  47.                 solve(k+1);
  48.                 vis2[i] = false;
  49.             }
  50.         }
  51.     }
  52.  
  53. }
  54.  
  55. int main()
  56. {
  57.     bool output[27]; int op;
  58.     for(int i=0; i<27; i++)
  59.     {
  60.         bool f = false;
  61.         for(int j=0; j<5; j++)
  62.         {
  63.             cin >> A[j];
  64.             if(A[j]!=0)
  65.  
  66.                 f = true;
  67.     }
  68.     if(!f)
  69.     {
  70.         op = i;
  71.         break;
  72.     }
  73.     solve(0);
  74.     if(possible)
  75.         output[i] = true;
  76.     else
  77.         output[i] = false;
  78.     possible = false;
  79.     }
  80.  
  81.     for(int i=0; i<op; i++)
  82.     {
  83.         if(output[i])
  84.             cout << "Possible" << endl;
  85.         else
  86.             cout << "Impossible" << endl;
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement