Advertisement
Guest User

J problema

a guest
Oct 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int
  9. sumanPrimos (int a, int b)
  10. {
  11.   vector < int >primos = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
  12.  
  13.  
  14.   int suma = a + b;
  15.  
  16. // When the element is not found, std::find returns the end of the range
  17.   if (find (primos.begin (), primos.end (), suma) != primos.end ())
  18.     {
  19.       return 1;
  20.     }
  21.   else
  22.     {
  23.       return 0;
  24.     }
  25. }
  26.  
  27. int
  28. printVector (vector < int >&vec)
  29. {
  30.   for (int i = 0; i < vec.size (); i++)
  31.     {
  32.        
  33.       cout << vec[i] << " ";
  34.     }
  35.  
  36.     cout <<endl;
  37.     return 0;
  38. }
  39.  
  40. int
  41. backtracking (vector < int >&acum, int max, int last)
  42. {
  43.  
  44.   if (acum.size () == max)
  45.     {
  46.         if( sumanPrimos (last, acum[0])){
  47.            
  48.             printVector (acum);
  49.      
  50.         }
  51.         return 0;
  52.     }
  53.   else
  54.     {
  55.       for (int i = 2; i <= max; i++)
  56.     {
  57. //si no lo he usado aun
  58.       if (find (acum.begin (), acum.end (), i) == acum.end ())
  59.         {
  60.           if (sumanPrimos (i, last))
  61.         {
  62.           vector < int >acum1 = acum;
  63.           acum1.push_back (i);
  64.           backtracking (acum1, max, i);
  65.         }
  66.         }
  67.     }
  68.       return 0;
  69.     }
  70. }
  71.  
  72. int
  73. main ()
  74. {
  75.   int n;
  76.   int cnt = 1;
  77.   while(cin >> n){
  78.     cout << "Case " << cnt << ":" << endl;
  79.     cnt++;
  80.     for (int i = 2; i <= n; i++)
  81.         {
  82.         if (sumanPrimos (1, i)){
  83.  
  84.             vector < int >acum = { 1, i };
  85.             backtracking (acum, n, i);
  86.         }
  87.     }
  88.     cout<< endl;
  89.  
  90.   }
  91.   return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement