【问题标题】:How to print this table in combined form C++? [closed]如何以组合形式 C++ 打印此表? [关闭]
【发布时间】:2018-10-09 11:22:09
【问题描述】:

我需要打印这张表

2
0
4 2
3 3
6 4 2
6 6 6
8 6 4 2
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;
}

我的结果:

0
3 3
6 6 6
9 9 9 9

2
4 2
6 4 2
8 6 4 2

所需结果:

2
0
4 2
3 3
6 4 2
6 6 6
8 6 4 2
9 9 9 9

【问题讨论】:

  • 1) 改变你的逻辑来创建数字,这样你的外循环索引对应于行号 2) 将两者合二为一
  • 我问我如何组合它们,你的回答是,组合它们!哇
  • 再仔细阅读评论......
  • 我建议创建子函数:1 创建 3 的倍数,on 用于减少偶数。然后编写两个循环以使 i = 0 -> 4。然后合并循环。
  • #include &lt;iostream&gt; using namespace std; int main(){ const int N = 9; for(int i = 2; i &lt;= N; i += 2){ for (int j = i; j &gt; 0; j -= 2) { cout &lt;&lt; j &lt;&lt; " "; } cout &lt;&lt; endl; for(int j = 0; j &lt;= i; j +=3) { cout &lt;&lt; j &lt;&lt; " "; } cout &lt;&lt; endl; } return 0; }

标签: c++ c++11 sequence tabular


【解决方案1】:
#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;
}

编辑:已更正...

有点接近答案
2
0 ==> 真
4 2
0 3 ==> 应该是 3 3
6 4 2
0 3 6 ==> 应该是 6 6 6
8 6 4 2
0 3 6 ==> 应该是 9 9 9

【讨论】:

  • 您应该将此添加到问题中,而不是放在此处。
  • 现在可以用了吗?
  • 9 9 9 还是9 9 9 9,因为您的问题看起来不同...而且它对我有用...
  • 然后在第二个 for 循环条件中使用 (i == 2 ? i : i + 2) 而不是 i。我也在你的答案中编辑了它。
  • 好的,现在你的代码可以正常工作了……
【解决方案2】:

当您提供重复次数时,这是另一种方法(例如在@Ruks 答案中):

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++;
    }
}

【讨论】:

    【解决方案3】:

    使用这个:

    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);
    }
    

    编辑:我能想到的最短最强的方法……

    然后这样称呼它:

    print_sequence();

    如果你不想要 4 次:

    print_sequence(10)

    它会重复你想要的次数......

    亲切的问候,

    鲁克斯。

    【讨论】:

    • 我还没有达到 C++ 的水平。我们只是通过 for 循环。而这个问题需要用简单的for循环来解决。
    • @TrevorPhilip 哪一部分是“那个级别的C++”?如果你愿意,你可以删除 for 循环中的 if 语句,这不会有很大的不同(会在每行的末尾添加额外的空格)...
    • 你可以看到我的回答。这就是我对C++的理解程度
    • 我不认为它只是简单的算术和 for 循环在语法上看起来总是一样的,没有什么新东西。试着仔细阅读并告诉我你在哪里停下来。
    • 那么你能帮我纠正我的答案而不是理解你的答案吗?因为我没有时间,我已经接近答案了。
    猜你喜欢
    • 2013-02-16
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2013-08-02
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多