Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector<int> a(5);
  6. bool flag = false;
  7.  
  8. // s: dau
  9. /*
  10.     0 -> +
  11.     1 -> -
  12.     2 -> *
  13.  */
  14. // a: phan tu
  15.  
  16. void calculate(string s){
  17.     int ans = 0;
  18.     if (s[0] == '0') ans = a[0] + a[1];
  19.     if (s[0] == '1') ans = a[0] - a[1];
  20.     if (s[0] == '2') ans = a[0] * a[1];
  21.     for(int i=1;i<s.length();i++){
  22.         if (s[i] == '0') ans += a[i+1];
  23.         if (s[i] == '1') ans -= a[i+1];
  24.         if (s[i] == '2') ans *= a[i+1];
  25.     }
  26.     if (ans == 23) flag = true;
  27. }
  28.  
  29. void Try(string s, int n){
  30.     if (n == s.length()){
  31.         calculate(s);
  32.     }
  33.     else{
  34.         Try(s+"0",n);
  35.         Try(s+"1",n);
  36.         Try(s+"2",n);
  37.     }
  38. }
  39.  
  40. void subprocess(){
  41.     Try("",4);
  42. }
  43.  
  44. void process(){
  45.     flag = false;
  46.     cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4];
  47.     while(next_permutation(a.begin() , a.end())){
  48.         subprocess();
  49.     }
  50.     if (flag) cout << "YES";
  51.     else cout << "NO";
  52.     cout << endl;
  53.  
  54. }
  55.  
  56. int main(){
  57.  
  58.     int T;
  59.     cin >> T;
  60.     while(T--) process();
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement