fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int N; // 總共有N顆球
  5. vector<int> box; // 未選中的號碼
  6. vector<int> bag; // 我們選中的號碼
  7. vector<int> notes(8 );//紀錄是否取過
  8. void PickBall(int bag_pos){
  9. //放滿球就印出來
  10. if(bag_pos==N){
  11. for(int i=0;i<N;i++)
  12. cout<<bag[i];
  13. cout<<'\n';
  14. return;
  15. }
  16. for(int i=0;i<N;i++){
  17. if(notes[box[i]-1]==0){//該位子的球沒取過
  18. bag.push_back( box[i] );//趕緊放進袋子裡
  19. notes[box[i]-1]=1;//標記為[已取過]
  20. PickBall(bag_pos+1);//開始取下一位置的球
  21. bag.pop_back();//從袋子放回箱子
  22. notes[box[i]-1]=0;//標記為[未取過]
  23. }
  24. }
  25. }
  26. int main() {
  27. ios::sync_with_stdio(0),
  28. cin.tie(0),cout.tie(0);
  29. while(cin>>N){
  30. box.clear();
  31. for(int i=N;i>0;i--)
  32. box.push_back(i);
  33. PickBall(0);
  34. }
  35.  
  36. }
Success #stdin #stdout 0s 4400KB
stdin
3
2
stdout
321
312
231
213
132
123
21
12