C++ 如何以组合形式C+打印此表+;?

C++ 如何以组合形式C+打印此表+;?,c++,c++11,sequence,tabular,C++,C++11,Sequence,Tabular,我需要打印这张表 2 0 4.2 3 6.4.2 6 8642 9 9 9 9 我已经为以下结果编写了这段代码 #include <iostream> using namespace std; int main(){ const int N = 9; for(int i = 0; i <= N; i += 3){ for (int j = 0; j <= i; j +=3) { cout <<

我需要打印这张表

2
0
4.2
3
6.4.2
6
8642
9 9 9 9

我已经为以下结果编写了这段代码

#include <iostream>
using namespace std;

int main(){
    const int N = 9; 
    for(int i = 0; i <= N; i += 3){   
        for (int j = 0; j <= i; j +=3) {
            cout << i << " ";  
        }       
        cout << endl; 
    }
    cout << "\n";
    for(int i = 2; i <= N; i += 2){    
        for (int j = i; j > 0; j -= 2) {
            cout << j  << " ";    
        }         
        cout << endl; 
    }
    return 0;
}
#包括
使用名称空间std;
int main(){
常数int N=9;
对于(int i=0;i使用以下公式:

void print_sequence(unsigned long long const repeat = 4, unsigned long long const i = 0)
{
    for (auto j = (i + 1ull) * 2ull; j > 2ull; j -= 2ull)
        std::cout << j << " ";
    std::cout << (repeat > 0ull && repeat < -1ull ? "2\n" : "");
    for (auto j = 0ull; j < i; j++)
        std::cout << i * 3ull << " ";
    std::cout << (repeat > 0ull && repeat < -1ull ? std::to_string(i * 3ull) + "\n" : "");
    if (repeat < -1ull && i + 1ull < repeat)
        print_sequence(repeat, i + 1ull);
}
void print_序列(无符号长常量repeat=4,无符号长常量i=0)
{
对于(自动j=(i+1ull)*2ull;j>2ull;j-=2ull)
std::cout
#包括
使用名称空间std;
int main(){
常数自动n=4;
自动计数=0;
对于(自动i=2;i 0;j-=2)

std::cout当您提供重复次数时,这里有另一种方法:

void打印序列(无符号整数重复)
{
int n=2;
for(int i=1;i0;j--)
{

标准::cout 1)更改逻辑以创建数字,这样两个数字的外部循环索引都对应于行号2)将两者合并为一我问如何将它们合并,你的答案是,合并它们!我再次更仔细地添加注释…我建议创建子函数:1创建3的倍数,on减少偶数。然后,将两个循环都写为i=0>4。然后合并循环。<代码>包括使用命名空间STD;int({)const int n=9;for(int i=2;i 0;j=2){cOUT,我不在C++的那个级别。我们只是循环使用。这个问题需要用简单的for循环来解决。@ TrvOffiLip它的一部分是“这一级别的C++”?如果需要,可以删除for循环中的If语句,这不会有太大区别(会在每行末尾添加额外的空格)…你可以看到我的答案。这是我对C++的理解水平。我认为它只不过是简单的算术,循环的语法看起来总是一样的,没有什么新的。只要仔细阅读,告诉我你停在哪里。那么你能帮我更正我的答案而不是理解你的答案吗?因为我没有时间,而且我快到了答案。你应该把它添加到问题中,而不是放在这里。它现在起作用了吗?它是
9
还是
9
,因为你的问题看起来不同…而且它对我有效…然后使用
(i==2?i:i+2)
而不是第二个for循环条件中的
i
。我也在你的答案中编辑了它。好的,现在你的代码工作正常了。。。
#include <iostream>
using namespace std;

int main(){
    const auto n = 4;
    auto count = 0;
    for (auto i = 2; i <= n * 2; i += 2)
    {
        for (auto j = i; j > 0; j -= 2)
            std::cout << j << " ";
        std::cout << std::endl;
        for (auto j = 0; j < (i == 2 ? i : i + 2); j += 3)
            std::cout << count * 3 << " ";
        ++count;
        std::cout << std::endl;
    }


  return 0;
}
void printSequence(unsigned int repeats)
{
    int n = 2;
    for(int i = 1; i < repeats; i++)
    {
        n+=2*i;
    }
    //n - number of all numbers in sequence for given number of repeats

    int step = 0;
    int numsPerRow = 1;

    for(int i = 0; i < n; i+=step)
    {
        for(int j = numsPerRow; j > 0; j--)
        {
            std::cout << 2*j << " ";
        }
        std::cout << std::endl;
        for(int j = 0; j < numsPerRow; j++)
        {
            std::cout << step+numsPerRow-1 << " ";
        }
        std::cout << std::endl;

        step+=2;
        numsPerRow++;
    }
}