仅使用嵌套 while 循环打印 table

Printing a table using nested while loops only

我正在尝试用 C++ 打印以下 table:

     1    2    3    4    5    6    7    8    9    10    
1    1    2    3    4    5    6    7    8    9    10    
2    2    4    6    8    10   12   14   16   18   20    
3    3    6    9    12   15   18   21   24   27   30    
4    4    8    12   16   20   24   28   32   36   40    
5    5    10   15   20   25   30   35   40   45   50    
6    6    12   18   24   30   36   42   48   54   60    
7    7    14   21   28   35   42   49   56   63   70    
8    8    16   24   32   40   48   56   64   72   80    
9    9    18   27   36   45   54   63   72   81   90    
10   10   20   30   40   50   60   70   80   90   100   

仅使用嵌套 while 循环。


我这里有两个主要问题:

这是我试过的

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  int k=0;
  while(k<=10)
  {cout << k << setw(5);
      k++;
  };
  cout << "\n";

  int i=1;
  while(i<=10){
      cout << i << setw(5);
      int j=1;
      while(j<=10){
          cout<< i*j << setw(5);
          j++;
      }
      cout << "\n";
    i++;
  }  
  return 0;
}

但是,如前所述,我在开始时使用了非嵌套的 while,并且输出是:

0    1    2    3    4    5    6    7    8    9   10    
1    1    2    3    4    5    6    7    8    9   10    
2    2    4    6    8   10   12   14   16   18   20    
3    3    6    9   12   15   18   21   24   27   30    
4    4    8   12   16   20   24   28   32   36   40    
5    5   10   15   20   25   30   35   40   45   50    
6    6   12   18   24   30   36   42   48   54   60    
7    7   14   21   28   35   42   49   56   63   70    
8    8   16   24   32   40   48   56   64   72   80    
9    9   18   27   36   45   54   63   72   81   90    
10   10   20   30   40   50   60   70   80   90  100 

两位数字没有以正确的方式对齐。另一方面,我想不出一种方法来修改循环以仅增加两位数的 space,而不使用 if 语句。

所以我是不是遗漏了什么,或者如果不使用 if 或非嵌套 while 就无法打印上面的 table?

我想你想隐藏第一个 0 值。为了隐藏它,我使用了一些位操作。没有 if 语句和嵌套 while 中的所有打印。

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  int i = 0, k = 0;
  while (i < 10){
      int j = 0;
      while (j <= 10){
          cout << left << setw(5);
          (i || j || k) && cout << j + i * j + !j * (i + 1);
          !(i || j || k) && cout << "";
          j++;
      }
      cout << "\n";
      i += k++ > 0;
  }  
  return 0;
}

输出为

     1    2    3    4    5    6    7    8    9    10   
1    1    2    3    4    5    6    7    8    9    10   
2    2    4    6    8    10   12   14   16   18   20   
3    3    6    9    12   15   18   21   24   27   30   
4    4    8    12   16   20   24   28   32   36   40   
5    5    10   15   20   25   30   35   40   45   50   
6    6    12   18   24   30   36   42   48   54   60   
7    7    14   21   28   35   42   49   56   63   70   
8    8    16   24   32   40   48   56   64   72   80   
9    9    18   27   36   45   54   63   72   81   90   
10   10   20   30   40   50   60   70   80   90   100

解释:

  1. 我使用 j 作为种子,使用 i * j 作为第 (j + 1) 列的新值。 它适用于从第一行第二列到最后一行最后一列。
  2. 对于第一列,j 始终为 0。为了使其正常工作,我使用了 !j * (i + 1),它为第一列打印 0 到 10。
  3. 要隐藏第一行第一列的0,i、j、k都为0。对于所有其他单元格,至少有一个单元格的值不是0。

如果还不清楚,我会举例说明。

需要考虑的事项:

  • 输出制表符而不是尝试设置宽度要容易得多, 尽管您对输出外观的控制较少(它将 取决于控制台的制表位宽度)。但是你的标准制表符停止 将处理您的值范围。
  • 你的问题陈述没有说明你可以使用多少个 while 循环
  • 您可以使用 while 循环来模拟 if 语句,如下所示:

    while(j == 0){
        cout << i << '\t';
        break;
    }
    

剩下的留给你做练习。