heranchris0430

二元樹

May 6th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. int main(){
  8.     cout << "這是DC-porg4,請選擇你要執行的項目:"<<endl;
  9.     int a;
  10.     cin >> a;
  11.     if (a != 1 && a != 0){
  12.         cout << "重新輸入";
  13.     }
  14.     while (a==1||a==0){
  15.         while (a==1){
  16.             int de,ba,de1,k,m = 0;
  17.             int n1=1,n2=1,n3,n4,n5,n7;
  18.             cout << "輸入深度:" <<endl;
  19.             cin >> de;
  20.             cout << "輸入球數:" <<endl;
  21.             cin >> ba;
  22.             while (de != 0 && ba != 0){
  23.                 k = pow(2,de);
  24.                 int n0[k];
  25.                 int n6[k] = {0};
  26.                 n7 = k;
  27.                 for (int i = 0; i < k; i++){
  28.                     n0[i] = 0;
  29.                 }
  30.                 int i = 1; //設定i的初值 計算球數量
  31.                 while (i <= ba){ //當i<=球的數量
  32.                     n1 = 1; //n0陣列的計算
  33.                     n2 = 1; //深度計算
  34.                     while (n2 < de){ //當n2<=深度
  35.                         if (n0[n1] == 0){ //判斷n0中的n1陣列是否為0
  36.                             n0[n1] = 1;
  37.                             n1 *= 2;
  38.                             n6[n2] = n1;
  39.                             n2++;
  40.                         }
  41.                         else if (n0[n1] == 1){ //判斷n0中的n1陣列是否為1
  42.                             n0[n1] = 0;
  43.                             n1 *= 2;
  44.                             n1 += 1;
  45.                             n6[n2] = n1;
  46.                             n2++;
  47.                         }
  48.                     }
  49.                     i++;
  50.                 }
  51.                 cout << "1";
  52.                 for (int w = 1;w < de ;w++){
  53.                     cout << "->";
  54.                     cout << n6[w];
  55.                 }
  56.             system("PAUSE");
  57.             return 0;
  58.             }
  59.         }
  60.         while (a==0){
  61.             goto END;
  62.         }
  63.     }
  64.     END:
  65.     cout << "";
  66. }
Add Comment
Please, Sign In to add comment