Advertisement
paulomiranda98

Sequencia A

Jun 22nd, 2018
3,422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * Paulo Miranda
  3. */
  4.  
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <string.h>
  8.  
  9. using namespace std;
  10.  
  11. int vetor[40];
  12. int memo[40][40][1010];
  13.  
  14. int existe(int i, int f, int valor){
  15.    
  16.     if(valor < 0)
  17.         return 0;
  18.        
  19.     if(valor == 0)
  20.         return 1;
  21.        
  22.     if(i == f)
  23.         return 0;
  24.        
  25.     if(memo[i][f][valor] != -1)
  26.         return memo[i][f][valor];
  27.  
  28.     int sem = existe(i+1, f, valor);
  29.     int com = existe(i+1, f, valor - vetor[i]);
  30.  
  31.     if(sem == 1 || com == 1)
  32.         return memo[i][f][valor] = 1;
  33.  
  34.     return memo[i][f][valor] = 0;
  35. }
  36.  
  37. int main(){
  38.     int n, caso = 1;
  39.  
  40.     while(cin >> n){
  41.         bool result = true;
  42.         memset(memo, -1, sizeof(memo));
  43.        
  44.         for(int i=0; i<n; i++){
  45.             cin >> vetor[i];
  46.         }
  47.  
  48.         if(vetor[0] < 1)
  49.             result = false;
  50.            
  51.         for(int i=1; result && i<n; i++)
  52.             if(vetor[i] - vetor[i-1] <=0 )
  53.                 result = false;
  54.                
  55.         for(int i=1; result && i<n; i++)
  56.             if(existe(0, i, vetor[i]))
  57.                 result = false;
  58.        
  59.         cout << "Case #"<< caso<<":";
  60.         for(int i=0; i < n; i++)
  61.             cout << " " << vetor[i];
  62.         cout << endl;
  63.        
  64.         if(result)
  65.             cout << "This is an A-sequence." << endl;
  66.         else
  67.             cout << "This is not an A-sequence." << endl;
  68.            
  69.         caso++;
  70.     }
  71.    
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement