#を表示する3
問題
以下を表示せよ
# #
## ##
### ###
########
########
### ###
## ##
# #
解答と過程
d•#を表示する
d•スペースを表示する(Sで可視化する)
d•繰り返し横に#を表示する
d•繰り返し縦に#を表示する
d•#で長方形を作成する
d•繰り返しの回数を計算する
*/
//横の繰り返しをするupRow関数を作成してやり、簡単にデザインできるようにした。
//行が切り替わるごとに、upRowに入れる変数だけを考えればよい
#include <iostream>
using namespace std;
void upRow(int repeatNum, char str);
void downRow(int repeatNum, char str);
int main(){
for (int i = 1; i <= 4; i++) {//改行ループ
upRow(i, ' ');
upRow(i, '#');
upRow(8 - (2 * i), ' ');
upRow(8 - (2 * i), ' ');
upRow(i, '#');
upRow(i, ' ');
cout << endl;//改行ループ終わり
}
for (int i = 4; i >= 1; i--) {//改行ループ
upRow(i, ' ');
upRow(i, '#');
upRow(8 - (2 * i), ' ');
upRow(8 - (2 * i), ' ');
upRow(i, '#');
upRow(i, ' ');
cout << endl;//改行ループ終わり
}
}
void upRow(int repeatNum, char str) {
for (int repeat = 1; repeat <= repeatNum; repeat++) {
cout << str << flush;
}
}
//upRowと同じ
void downRow(int repeatNum, char str) {
for (int repeat = repeatNum; repeat >= 1; repeat--) {
cout << str << flush;
}
}
/*
//横ピラミッド
#include <iostream>
using namespace std;
void upCount(int maxNum, char str){
for (int i = 1; i <= maxNum; i++) {
for (int repeat = 1; repeat <= i; repeat++) {
cout << str << flush;
}
cout << endl;
}
}
void downCount(int maxNum, char str){
for (int i = maxNum; i >= 1; i--) {
for (int repeat = 1; repeat <= i; repeat++) {
cout << str << flush;
}
cout << endl;
}
}
int main() {
downCount(4, 's');
downCount(4, 's');
}
*/
/*
//#で長方形を作成する
#include <iostream>
using namespace std;
int main() {
for (int row = 0; row < 8; row++) {
for (int hashNum = 0; hashNum < 16 ; hashNum++) {
cout << "#" << flush;
}
cout << endl;
}
}
*/
/*
//繰り返し縦に#を表示する
#include <iostream>
using namespace std;
int main() {
for (int row = 0; row < 8; row++) {
cout << "#" << endl;
}
}
*/
/*
//繰り返し横に#を表示する
#include <iostream>
using namespace std;
int main() {
for (int hashNum = 0; hashNum < 16 ; hashNum++) {
cout << "#" << flush;
}
cout << endl;
}
*/
/*
//スペースを表示する(Sで可視化する)
#include <iostream>
using namespace std;
int main() {
cout << "s" << endl;
}
*/
/*
//#を表示する
#include <iostream>
using namespace std;
int main() {
cout << "#" << endl;
}
*/
感想
前回よりもトリッキーな形なので少々戸惑いました。
でも、とりあえず小さな問題に分解。
小さい問題を解いている途中で横に任意の文字を指定個数を表示する関数があれば、
楽に解けるのではないかとやってみたところずいぶん楽に解く事が出来ました。
これで#を図形にする問題は行が変わるごとに出力する文字列のパターンを
分析するだけで解けるようになりました。