如何在C ++中构造一个复合循环? [英] How to construct a compound for loop in C++?
本文介绍了如何在C ++中构造一个复合循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
for
循环的计数器部分(第三部分)中是否允许 c>在我试图获得优雅的书面代码产生一个直角三角形,我写了这个,但它不会编译:
Is another for
loop allowed in the counter section (third part) of a for
loop? In my attempt to get elegant in writing code to produce a right triangle, I wrote this but it wouldn't compile:
#include <stdio.h>
int main()
{
int i, j, N = 5;
for (i = 1;
i <= N;
(for (j = 1; j <= i; j++, printf("%c", '0'));), i++)
printf("\n");
}
return 0;
}
推荐答案
=http://stackoverflow.com/users/12711/michael-burr> Michael Burr 建议使用 lambda
。感谢评论者请求我使用 putchar()
。
Hat tip to Michael Burr for the suggestion to use lambda
. And thanks to the commentators requesting me to use putchar()
.
#include <stdio.h>
int main()
{
int N;
scanf ("%d",&N);
for (int i = 0;
i < N;
i++,
[i] {for (int j = 1;
j <= i;
j++,
putchar('0'));}(),
printf("\n"));
return 0;
}
这篇关于如何在C ++中构造一个复合循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文