fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int N; // 總共有N顆球(號碼是1-N)
  6. int M; // 挑出M顆球
  7. vector<int> box; // 未選中的號碼
  8. vector<int> bag;// 我們選中的號碼
  9. vector <bool>chosen;
  10. void PickBall(int bag_pos,int box_pos){ // box_pos 代表藍色框框
  11. // 停止條件(挑選滿M顆球就找到一組合法的組合)
  12. // :輸出袋子裡的內容
  13. if(bag_pos==M){
  14. // for (int j=0;j<M;j++){
  15. // for (int k=j+1;k<M;k++){
  16. // if (bag[j]==bag[k]){
  17. // return;
  18. // }
  19. // }
  20. // }
  21. for(int i=0;i<M;i++){
  22. cout<<bag[i];
  23. }
  24. cout<<'\n';
  25. return;
  26. }
  27. // 同一層遞迴
  28. // 剪枝:box_pos=0,i可以選擇的範圍:[0,3] => [0,1]
  29. for(int i=0;i<N;i++){ // i 代表這次選到的位置
  30. if (chosen[box[i]]==false){ //检查是否被选过
  31. chosen[box[i]]=true;
  32. // 從藍色框框的範圍內選擇一個號碼把他複製到 bag
  33. bag.push_back( box[i] );
  34. // 遞迴呼叫自己:
  35. PickBall(bag_pos+1,i+1);
  36. // 離開遞迴後把這次選到的球從 bag 中放回去
  37. bag.pop_back();
  38. chosen[box[i]]=false;
  39. }
  40. }
  41. }
  42. int main() {
  43. while(cin>>N){
  44. M=N;
  45. box.clear();
  46. bag.clear();
  47. chosen.clear();
  48. for(int i=N;i>=1;i--){
  49. box.push_back(i);
  50. chosen.push_back(false);
  51. }
  52. chosen.push_back(false);
  53. // 初始情況
  54. PickBall(0,0);
  55. }
  56. }
Compilation error #stdin compilation error #stdout 0s 4408KB
stdin
3 2
compilation info
prog.cpp:4:1: error: specializing member ‘std::basic_ios<char>::sync_with_stdio’ requires ‘template<>’ syntax
 std::ios::sync_with_stdio(false);
 ^~~
stdout
Standard output is empty