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;
    }
}

posted @ 2020-07-08 17:27  HDAWN  阅读(873)  评论(0编辑  收藏  举报