C++三角形图案程序(围绕0的对称图像)


假设有一个正数值n,任务是生成一个三角形图案,即打印数字的对称图像并显示结果

范例

Input-: n = 6
Output-:

Input-: n = 3
Output-:

以下程序采用的方法如下

  • 输入n的值,作为正整数
  • 遍历一个i循环,作为模式中的行数,即n
  • 遍历一个j循环,作为模式中的空格数
  • 遍历另一个循环,作为模式中的数字

算法

START
Step 1-> declare function to print mirror image of triangular pattern
   void print_mirror(int n)
   declare and set int temp = 1 and temp2 = 1
      Loop for int i = 0 and i < n and i++
         Loop For int j = n - 1 and j > i and j—
            print space
         End
         Loop For int k = 1 and k <= temp and k++
            print abs(k - temp2)
         End
         Set temp += 2
         increment temp2++
         print \n
Step 2-> In main()
   Declare int n = 6
   print_mirror(n)
STOP

范例

#include <bits/stdc++.h>
using namespace std;
//function to print mirror image of triangular pattern
void print_mirror(int n) {
   int temp = 1, temp2 = 1;
   for (int i = 0; i < n; i++) {
      for (int j = n - 1; j > i; j--) {
         cout << " ";
      }
      for (int k = 1; k <= temp; k++) {
         cout << abs(k - temp2);
      }
      temp += 2;
      temp2++;
      cout << "\n";
    }
}
int main() {
   int n = 6;
   print_mirror(n);
   return 0;
}

输出

更新于: 2020年7月9日

268次浏览

推动你的 职业生涯

通过完成课程获得认证

开始吧
广告
© . All rights reserved.