我现在有一个困难的时间来增加一个盒子的形状。下面是这张图片:
This is the problem
这是我到目前为止的代码:
#include <iostream>
using namespace std;
int box(int);
int main() {
int input;
cout << "Enter input: ";
cin >> input;
cout << endl;
box(input);
return 0;
}
int box(int input) {
if (input == 1 || input == 2 || input == 3) {
switch(input) {
case 1:
for (int a=1; a<=3; a++) {
cout << "*";
}
cout << endl;
for (int b=1; b<=1; b++) {
for (int c=1; c<=1; c++) {
cout << "*";
}
for(int d=1; d<=1; d++) {
cout << " ";
}
cout << "*" << endl;
}
for (int e=1; e<=3; e++) {
cout << "*";
}
break;
}
}
else {
cout << "Wrong input." << endl;
}
return input;}
我对C++编程是个新手。我正在尝试理解这里的逻辑和代码。但这对我来说太难了。我正在尝试输入1,该框将出现。我想用一个函数重新创建代码,该函数的输入将递增该框。
发布于 2015-10-08 13:54:34
你开始解决这个问题的时候太具体了:从一个盒子开始。试着看看他们都有什么共同点。以N作为输入,
代码对输出有3、5、7行:2 * N + 1
"*",然后是成对的其他行N N
"*",然后N乘以<"*">d16 (4个空格)或<N>d17 (3个空格和一个星号)。对于I-th线对,前者有I,后者有N - I,其中I从0到N - 1.
"*",然后是" " (4个空间)或" * *",前一个线的I + 1,后一个线的N - (I + 1)。<代码>H230<代码>F231现在你甚至可以画4个或5个方框--它们都遵循相同的规则。
https://stackoverflow.com/questions/33007508
复制相似问题