Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在for循环中写入此表?_C++_Loops_For Loop - Fatal编程技术网

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