C++ 嵌套 for 循环,用于集合 base/exponent 的指数

C++ Nested for loop for exponents with set base/exponent

所以我需要一些帮助。我想打印出 2 到 2^20 之间的所有整数,它们是 2 的整数次方。我发现我每次都需要将次方增加 1,但我似乎无法弄清楚内部 for 循环中的内容.我不能使用 pow() 函数

c = 2;    
cout << "\nPROBLEM C" << endl;
for (int powerC = 1; powerC <= 20; powerC++) // powerC is exponent
{ 
  cout << setw(5) << powerC << " ";
  counterC++;
  for (int x = 1; x <= 20; x++) // where I am having trouble with
  {
     c = (c*powerC);
     cout << setw(5) << c;
  } // end inner for loop
    if (counterC % 8 == 0)
    {
        cout << endl;
    }
}
cout << "\nNumber of numbers = " << counterC;

使用 << 运算符会简单得多。

因为 2 是 2^1,所以你想打印从 2^1 到 2^20 的所有整数(包括这两个整数),或者 20 个数字:

int c = 2;
for (int i=0; i<20; i++)
{
    std::cout << c << std::endl;

    c <<= 1;
}