Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
- int
- sumanPrimos (int a, int b)
- {
- vector < int >primos = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
- int suma = a + b;
- // When the element is not found, std::find returns the end of the range
- if (find (primos.begin (), primos.end (), suma) != primos.end ())
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- int
- printVector (vector < int >&vec)
- {
- for (int i = 0; i < vec.size (); i++)
- {
- cout << vec[i] << " ";
- }
- cout <<endl;
- return 0;
- }
- int
- backtracking (vector < int >&acum, int max, int last)
- {
- if (acum.size () == max)
- {
- if( sumanPrimos (last, acum[0])){
- printVector (acum);
- }
- return 0;
- }
- else
- {
- for (int i = 2; i <= max; i++)
- {
- //si no lo he usado aun
- if (find (acum.begin (), acum.end (), i) == acum.end ())
- {
- if (sumanPrimos (i, last))
- {
- vector < int >acum1 = acum;
- acum1.push_back (i);
- backtracking (acum1, max, i);
- }
- }
- }
- return 0;
- }
- }
- int
- main ()
- {
- int n;
- int cnt = 1;
- while(cin >> n){
- cout << "Case " << cnt << ":" << endl;
- cnt++;
- for (int i = 2; i <= n; i++)
- {
- if (sumanPrimos (1, i)){
- vector < int >acum = { 1, i };
- backtracking (acum, n, i);
- }
- }
- cout<< endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement