用c++实现3个循环的平行四边形
Parallelogram with 3 loops with c++
这是我学计算机科学的第一年,我遇到了一些问题。教员要求编写平行四边形的代码:
输入行数:13
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
强制奇数进入(如4变为5)。规则是-我不能使用炖菜-必须只使用3个循环绘制形状-加上一个循环用于强制输入(r在3到23之间)-必须使用总行数或当前行进行所有计算(不能使用前一行,也不能生成自己的数字)
int main() {
int control = 0;
int i = 0, j = 0, k = 0, l = 0;
int r = 0, c = 0, crntRow = 0, crntRow2 = 0,
cuur_8r = 0, space = 0, star = 0;
char a = '-', b = '+';
//cin >> r;
r = 11;
if (!(r % 2))
r++;
c = 0;
//cout << c << r;
for (i = 0; i < r; i++)
{
space = r / 2;
star = r / 2;
crntRow = i;
while (crntRow > space)
{
space++;
cout << a;
}
//cout << j;
for (int j = 0; j < c; j++)
{
if (star > j)
{
cout << b;
}
}
c++;
cout << 'n';
}
}
TLDR:这是我到目前为止想出的可怕的代码,我不知道如何缩小rows/2
之后的恒星数量
您的讲师所指的三个循环是:
- 线路上的外环
- 一个循环,将空格作为每行的前缀(前半部分为0个空格)
- 在每行上打印星形的循环(这总是非零的)
这里有一个非常精简的例子:
int i, j, k, sp, st;
int r = 11;
// 1. An outer loop over the lines
for (i = 0; i < r; i++)
{
if(i <= r/2) {
sp = 0; // No spaces in the first half
st = i + 1; // Increasing stars in the first half
} else {
sp = i - r / 2; // Increasing spaces in the second half
st = r - i; // Decreasing stars in the second half
}
// 2. A loop to prefix spaces to each line (0 spaces for the first half)
for(j = 0; j < sp; j++) cout << ' ';
// 3. A loop to print stars on each line (this is always non-zero)
for(k = 0; k < st; k++) cout << '*';
cout << 'n';
}
作为一项练习,你可以在两个循环中做同样的事情:
- 线路上的外环
- 每行字符上的内部循环
在这种情况下,您必须选择在内部循环的每次迭代中打印哪个字符。
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 在平行块中循环不平行
- CPP中的平行OpenMP循环
- 在C 中平行循环的最小代码
- 在平行数组中重新启动循环
- OpenMP如何平行这些循环
- 用于平行区域内的循环
- 用c++实现3个循环的平行四边形
- 如何在OpenCV中将平行四边形图像转换为矩形图像