C++ 如何在for循环中写入此表?
期望输出:C++ 如何在for循环中写入此表?,c++,loops,for-loop,C++,Loops,For Loop,期望输出: 2 4 2 6 4 2 8 6 4 2 2 4 2 6 4 2 8 6 4 2 这是我的密码: for(int i = 0; i <= 7; i += 2) { for (int j = 0; j <= i; j += 2) cout << j + 2 << " "; cout << endl; } for(int i=0;i您使用双for循环的方法是一个好主意。下面是我的通用尝试,它允
2
4 2
6 4 2
8 6 4 2
2
4 2
6 4 2
8 6 4 2
这是我的密码:
for(int i = 0; i <= 7; i += 2) {
for (int j = 0; j <= i; j += 2)
cout << j + 2 << " ";
cout << endl;
}
for(int i=0;i您使用双for循环的方法是一个好主意。下面是我的通用尝试,它允许在n
行中打印您想要的内容,而不仅仅是4行:
#include <iostream>
using namespace std;
int main() {
int n = 4;
for(int i = 0; i < n; ++i) {
for(int j = i; j >= 0; --j) {
cout << 2 * (j + 1) << " ";
}
cout << "\n";
}
return 0;
}
PS:这种方法可以最小化(如果不是消灭的话)幻数的使用。欢迎使用堆栈溢出。
你只是用你的内环绕了一圈
#include <iostream>
using namespace std;
int main()
{
for(int i = 0; i <= 7; i += 2){
for (int j = i; j >= 0; j -= 2) {
cout << j + 2 << " ";
}
cout << endl;
}
return 0;
}
Trevor Philip欢迎使用Stack Overflow。您的问题已被多次回答,因此请确保接受其中一个问题。
2
4 2
6 4 2
8 6 4 2