在 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