用一定的逻辑创建数字金字塔

时间:2015-09-29 10:12:37

标签: c++

我需要一个能输出这个数字的程序:

         1
      1  2  1
   1  2  4  2  1
1  2  4  8  4  2  1

如果在两端添加数字,它将在其旁边打印输出(向内)。然后你还将添加这两笔钱并再次向内打印。另一件事,输入应该是最大的数字(在这种情况下,数字8)它可能大于8,如下图所示。

            1
         1  2  1
      1  2  4  2  1
   1  2  4  8  4  2  1
1  2  4  8  16 8  4  2  1

在这种情况下输入为16.依此类推。这是我的最新课程。

#include<iostream>
using namespace std;

int main(){
  int i, j, k, b, a, space=10;

  for(int i=0;i<=5;i++){
    for(k=0;k<space;k++){
      cout<<"   ";
    }
    for(j=1;j<=2*i-1;j=j*2){
      cout<<j<<"  ";
    }
    space--;
    cout<<endl;
  }

  system("pause");
  return 0;

}

请帮我改进一下。它还不是金字塔。帮助我至少输出所需的数字。

2 个答案:

答案 0 :(得分:2)

要正确设置金字塔格式,假设您使用的是固定宽度字符,您需要事先知道一些信息,例如:

  • 您要打印的最大数字是多少。
  • 有多少个数字有哪个宽度。

由于金字塔向下增加,因此当您打印 last 行时,此信息可用。

所以你需要做的是先计算最后一行(但不是输出)。假设你想要五行,那么中间数字将是2 ^(5-1),即16.所以你必须输出1 2 4 8 16.列位置将是0(开始),2(0加长度“1”加1空格),4(2加1加1空格),6(4加1加1),8,11(8加长“16”,即2,加1空格),13 ,15,17。

此时,您开始输出第一行,从第5列开始,即在第8位。

第二行将从第4列开始,即在第6列。

等等。

另一种可能性是想象你正在填充一个表(就好像你正在生成一个HTML表): - 从上到下填充 - 以任何顺序以与上述相同的方式“探索”每个单元格大小 - 相应地生成列位置 - 从上到下打印桌面

这只需要一轮计算,但需要对表本身进行内存存储。

快捷方式用于验证您要打印的最大数字是什么,并使用该宽度格式化所有列。在这种情况下,16是2个字符,因此您添加一个空格填充并输出填充为3个字符宽度的所有列。 这可能会浪费不必要的空间

后一种情况可以使用cout.width

来实现
int main() {

    int line;

    // Read input from standard input
    cin >> line;

    // We output the pyramid by allocating a fixed width to each number.
    // This requires to know beforehand which will be the largest number.
    // We can observe that at every line, the largest number is 2 to the
    // power of that line number: on line 0, the largest number is 2^0
    // which is 1, on line 1 it is 2 which is 2^1... on line 4 it is 16
    // which is 2^4. So if we have five lines (from 0 to 4), the largest
    // number will be 2 to the 4th.

    // Now the length of a number in base 10 is given by the logarithm
    // base 10 of that number, truncated, plus 1. For example log10 of
    // 1000 is exactly 3, and 3+1 is 4 digits. Log10 of 999 is
    // 2.9995654... which truncates to 2, 2+1 is 3 and 999 is 3 digits.

    // Here our number is 2 to the power of (line-1).
    // By the properties of the logarithm
    // this is the same as (line-1)*log10(2), and log10(2) is around 0.3.

    // So we multiply (line-1) by log10(2), truncate to integer and add 1
    // (or vice versa: we add 1 and then assign to width, which is an
    // integer, thereby truncating the value to integer.

    // But we need to add another 1 for the padding space (we want 1 2 4
    // 2 1, not 12421...). So before assigning, we add 2, not 1.

    int width = 2+(line-1)*0.30102999566398119521373889472449;

    //////////////////////
    // TODO: we're gonna output 2*line+1 strings, each 'width' wide.
    // So if (2*line+1)*width > 80 we'd better say it and stop, or the
    // output will be sorely messed up, since a terminal is only 80 chars
    // wide at the most. Which means that N=9 is the maximum number we
    // can print out and still be "nice".
    // Having not been asked to do this, we proceed instead.
    //////////////////////

    // For every line that we need to output...
    for (int i = 0; i < line; i++) {
        // Pad line-i empty spaces
        for (int j = 0; j < (line-i); j++) {
            // Set the width of the next cout to "width" bytes
            cout.width(width);
            cout<<" ";
        }
        int n = 1;
        // output the forward sequence: 1, 2, 4... doubling each time
        for (int j = 0; j < i; j++) {
            cout.width(width);
            cout <<n;
            n *= 2;
        }
        // output the top number, which is the next doubling
        cout.width(width);
        cout <<n;

        // output the sequence in reverse. Halve, output, repeat.
        for (int j = 0; j < i; j++) {
            n /= 2;
            cout.width(width);
            cout<<n;
        }
        // Now n is 1 again (not that we care...), and we output newline
        cout <<"\n";
    }

    // Return 0 to signify "no error".
    return 0;
}

答案 1 :(得分:0)

检查代码。这将给出欲望输出。

#include<iostream>
using namespace std;

int main(){

    int line = 4;
    for (int i =0; i < line; i++){
        for(int j = line - i; j >0 ; j --){
            cout<<"  ";
        }
        int temp = 1;
        for(int k = 0; k < i + 1; k ++){
            cout << " "<<temp;
            temp = temp *2;
        }
        temp /=2;
        for(int k =0; k < i; k ++){
            temp /=2;
            cout << " "<<temp;
        }
        cout <<"\n";
    }
    return 0;

}

输出:

        1
      1 2 1
    1 2 4 2 1
  1 2 4 8 4 2 1