C++ 无表情脸部图案打印程序
c++server side programmingprogramming更新于 2024/10/5 2:44:00
给定一个数字 n;任务是创建最多 n 行的无表情脸部图案并显示结果。无表情脸部是使用特殊字符创建的,使用特殊字符的无表情脸部看起来像:"*_*"。
示例
输入:n = 6 输出:
输入:n = 8 输出:
算法
Start Step 1-> In function print_stars(int i) Loop For j = 1 and j <= i and j++ Print “*” Step 2-> In function print_pattern(int rows) Loop For i = 1 and i <= rows and i++ Call function print_stars(i) Print “_” Call print_stars(rows - i + 1) Print “_” Call print_stars(rows - i + 1) Print ”_” Call print_stars(i) Print newline Step 3-> In function int main() Declare and set rows = 8 Call print_pattern(rows) Stop
示例
#include <bits/stdc++.h> using namespace std; //打印星星的函数 void print_stars(int i) { for (int j = 1; j <= i; j++) cout << "*"; } void print_pattern(int rows) { for (int i = 1; i <= rows; i++) { print_stars(i); cout << "_"; print_stars(rows - i + 1); cout << "_"; print_stars(rows - i + 1); cout << "_"; print_stars(i); cout << endl; } } int main() { int rows = 8; print_pattern(rows); return 0; }