fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int N;
  6. vector<int> box;
  7. vector<int> bag;
  8. vector<bool> picked(N+1); //有無選過
  9.  
  10. void PickBall(int bag_pos){
  11. if(bag_pos==N){
  12. for(int i=0;i<N;i++)
  13. cout<<bag[i];
  14. cout<<'\n';
  15. return;
  16. }
  17. for(int i=0;i<N;i++){
  18. if(picked[box[i]-1]==false){//無
  19. bag.push_back( box[i] );//選
  20. picked[box[i]-1]=true;//變有
  21. PickBall(bag_pos+1);
  22. bag.pop_back();
  23. picked[box[i]-1]=false;//恢復
  24. }
  25. }
  26. }
  27. int main() {
  28. ios::sync_with_stdio(0),
  29. cin.tie(0), cout.tie(0);
  30. while(cin>>N){
  31. box.clear();
  32. bag.clear();
  33. for(int i=N;i>0;i--){
  34. box.push_back(i);
  35. }
  36. PickBall(0);
  37. }
  38. }
Success #stdin #stdout 0s 4396KB
stdin
3
2
stdout
321
312
231
213
132
123
21
12