【问题标题】:Multiplication table in C++C++中的乘法表
【发布时间】:2020-02-27 10:51:56
【问题描述】:

我想做一个12X12的乘法表:

#include <iostream>
using namespace std;

int main(){

    for (int j = 1; j < 13; j++)
        for (int i =1; i < 13; i++)
            cout << j * i << " ";
    cout << endl;
    return 0;
}

1 2 3 4 5 6 7 8 9 10 11 12 2 4 6 8 10 12 14 16 18 20 22 24 3 6 9 12 15 18 21 24 27 30 33 36 4 8 12 16 20 24 28 32 36 40 44 48 5 10 15 20 25 30 35 40 45 50 55 60 6 12 18 24 30 36 42 48 54 60 66 72 7 14 21 28 35 42 49 56 63 70 77 84 8 16 24 6 62 40 8 48 8 6 42 40 8 48 8 18 27 36 45 54 63 72 81 90 99 108 10 20 30 40 50 60 70 80 90 100 110 120 11 22 33 44 55 66 77 88 99 110 121 132 12 24 36 48 60 72 84 96 108 120 132 144 p >

这是我得到的,但我希望它看起来更好,例如:

1.  1 2 3 4 5 6
2.
3.
4.

【问题讨论】:

    标签: c++


    【解决方案1】:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        for (int j = 1; j < 13; j++)
        {
            cout << j << ".";
            for (int i=1; i < 13; i++)
            {
                cout << i *j << " ";
            }
            cout << endl;
        }
        return 0;
    }
    

    【讨论】:

    • 您能否详细说明您的答案。 SO 通常不鼓励所有代码答案
    • 解释一下。
    【解决方案2】:

    有一百万个答案,问题并不那么精确。所以,我需要对对齐和输出格式做一些假设。

    此外,我将使用现代 C++ 功能使其更加灵活。

    首先我们定义一个 Lampda。在那里,我们将使用 ostringstream 来使用流库更轻松地输出。然后我们简单地循环请求的元素数量并创建一行。此外,我们还会做一些格式化工作。

    最后,我们使用 STL 中的算法来创建完整的表。

    #include <iostream>
    #include <algorithm>
    #include <iterator>
    #include <iomanip>
    
    constexpr int Max{12};
    
    int main()
    {
        // Define generator function Lambda
        auto generator = [row = 1]() mutable -> std::string  {
            // We will store a temporary result for one line in an ostringstream
            std::ostringstream oss{};
    
            // Output and formatting
            oss << std::right << std::setw(2) << row << ". "; 
            for (int i=1; i<=Max; ++i)
                oss << std::setw(3) << i*row << " ";
            oss << "\n";
            ++row;
            // The resulting line
            return oss.str();
        };
    
        // Generate the table
        std::generate_n(std::ostream_iterator<std::string>(std::cout, " "), Max,generator);
    }
    
    

    【讨论】:

      【解决方案3】:
      #include <iostream>
      using namespace std;
      int main() {
      
          for (int j = 1; j < 13; j++)
          {
              cout << j << "x12 TABLE:\t";
              for (int i = 1; i < 13; i++)
              {
                  cout << i *j << "\t";
              }
              cout << endl;
          }
          return 0;
      }
      
      //OUTPUT:
      //1x12 TABLE : 1       2       3       4       5       6       7       8       9       10      11      12
      //2x12 TABLE : 2       4       6       8       10      12      14      16      18      20      22      24
      //3x12 TABLE : 3       6       9       12      15      18      21      24      27      30      33      36
      //4x12 TABLE : 4       8       12      16      20      24      28      32      36      40      44      48
      //5x12 TABLE : 5       10      15      20      25      30      35      40      45      50      55      60
      //6x12 TABLE : 6       12      18      24      30      36      42      48      54      60      66      72
      //7x12 TABLE : 7       14      21      28      35      42      49      56      63      70      77      84
      //8x12 TABLE : 8       16      24      32      40      48      56      64      72      80      88      96
      //9x12 TABLE : 9       18      27      36      45      54      63      72      81      90      99      108
      //10x12 TABLE : 10      20      30      40      50      60      70      80      90      100     110     120
      //11x12 TABLE : 11      22      33      44      55      66      77      88      99      110     121     132
      //12x12 TABLE : 12      24      36      48      60      72      84      96      108     120     132     144
      
      

      【讨论】:

      • 用 '\t' 格式化的输出看起来不错,但我的建议是为每个数字使用预定义的宽度。见this topic
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 2015-03-10
      • 1970-01-01
      相关资源
      最近更新 更多