在 C++ 中打印小木屋图案的程序
在本教程中,我们将讨论打印房屋图案的程序。
为此,我们将提供要打印的房屋的宽度(例如 N)。我们的任务是用星形打印一个给定宽度的房屋结构,并在房屋内用线条字符创建一个门。
范例
#include <iostream>
using namespace std;
//printing the given hut structure
int print_hut(int n){
int i, j, t;
if (n % 2 == 0) {
n++;
}
for (i = 0; i <= n - n / 3; i++) {
for (j = 0; j < n; j++) {
t = 2 * n / 5;
if (t % 2 != 0) {
t--;
}
//calculating the distance from the initial
//character
//and printing the outer boundary of the hut
if (i == n / 5
|| i == n - n / 3
|| (j == n - 1 && i >= n / 5)
|| (j >= n / 5 && j < n - n / 5 && i == 0)
|| (j == 0 && i >= n / 5)
|| (j == t && i > n / 5)
|| (i <= n / 5 && (i + j == n / 5 || j - i == n / 5))
|| (j - i == n - n / 5)) {
cout << "*";
}
//printing the structure of the door
else if (i == n / 5 + n / 7 && (j >= n / 7 && j <= t - n / 7)) {
cout << "_";
}
else if (i >= n / 5 + n / 7 && (j == n / 7 || j == t - n / 7)) {
cout << "|";
}
else {
cout << " ";
}
}
cout << "\n";
}
}
int main(){
int n = 12;
print_hut(n);
return 0;
}输出
********** * * * ************* *___* * *| |* * *| |* * *| |* * *| |* * *| |* * *************
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP