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;
} 出力
********** * * * ************* *___* * *| |* * *| |* * *| |* * *| |* * *| |* * *************
-
C++でパリティを見つけるプログラム
このチュートリアルでは、パリティを見つけるためのプログラムについて説明します。 このために、番号が提供されます。私たちの仕事は、そのパリティ、つまり、1の数が奇数か偶数かを数えることです。 例 # include<bits/stdc++.h> # define bool int using namespace std; //finding the parity of given number bool getParity(unsigned int n) { bool parity = 0; while (n){ &
-
C++でバイナリツリーを印刷する
これらのルールに基づいて、m *n2D文字列配列に二分木を表示する必要があるとします- 行番号mは、指定された二分木の高さと同じである必要があります。 列番号nは常に奇数である必要があります。 ルートノードの値は、配置できる最初の行の真ん中に配置する必要があります。ルートノードが存在する列と行は、残りのスペースを2つの部分に分割します。これらは左下部分と右下部分です。左下の部分に左のサブツリーを印刷し、右下の部分に右のサブツリーを印刷する必要があります。ここで、左下部分と右下部分は同じサイズである必要があります。一方のサブツリーがnoneで、もう一方がnoneでない場合でも、noneサブツリ