I have several questions about vectors and Next_Permutation().
I ALMOST wrote a program that prints out permutations from a user input.
A user inputs something, gets stored into a vector, if user inputs 0 = stop.
I push back the values into the vector. Sort it. Print it.
If the user inputs
> 1
> 2
> 3
I don't want to see permutations with number 1. So I did a little if statement.
My problem:
When I enter 1 2 3, it works fine. If I flip it, WITHOUT using the sort(), it doesn't work. No permutations.
Also, I feel like line 30 and 25 are not a good way to do what I want to do. Is there a better way to do it?
To better understand my problem, let me copy some outputs.
1 2 3 4 5 6 7 8 9 10
|
Enter Values:1
Enter Values:2
Enter Values:3
Enter Values:0
2 1 3
2 3 1
3 1 2
3 2 1
|
Ok, that worked fine. Now let's try 2.
1 2 3 4 5 6 7 8
|
Enter Values:2
Enter Values:1
Enter Values:3
Enter Values:0
3 1 2
3 2 1
|
Excuse me? Where is my permutations that start with 1?
1 2 3 4 5 6
|
Enter Values:3
Enter Values:2
Enter Values:1
Enter Values:0
Press any key to continue . . .
|
Now the program had enough of me..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector <int> squares;
// x = user input
int x;
do
{
cout << "Enter Values:";
cin >> x;
if (x != 0)
{
squares.push_back(x);
}
} while (x!=0);
/*//sort those values
sort(squares.begin(), squares.end());*/
int y = squares[0];
//print the permutations
do
{
if (y != squares[0])
{
for (int i = 0; i < squares.size(); i++)
{
cout << squares[i] << " ";
}
}
cout << endl;
} while (next_permutation(squares.begin(), squares.end()));
system("PAUSE");
return 0;
}
|