C++輸出三角圖形


輸出像這樣的三角圖形

3
           1
          1 1
         1    1
        1 1 1 1
       1          1
      1 1       1 1
     1    1    1    1
    1 1 1 1 1 1 1 1

 int a[1025]={1};

void triangle(int n) {
     for (int i = 0; i < 1 << n; ++i) {
        for (int j = 1; j < (1 << n) - i; ++j)
            cout << " ";//前導空格
       
for (int j = i; j >= 0; --j)
            a[j] ^= a[j - 1];//修改數組
       
for (int j = 0; j <= i; j++) {
            if (a[j] % 2 == 1)
                if (a[j]) cout << a[j] << " ";
                else cout << " ";
            else {
                if (a[j]) cout << a[j];
                else cout << "  ";
            }
        }
        cout << endl;
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM