Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用某种逻辑创造一个数字金字塔_C++ - Fatal编程技术网

C++ 用某种逻辑创造一个数字金字塔

C++ 用某种逻辑创造一个数字金字塔,c++,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

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

         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;

}
#包括
使用名称空间std;
int main(){
int i,j,k,b,a,空间=10;

对于(int i=0;i要正确格式化棱锥体,假设您使用的是固定宽度字符,您需要事先知道一些信息,例如:

  • 您要打印的最大数字是多少
  • 多少个数字有多宽
由于金字塔向下增加,因此在打印最后一行时,此信息可用

因此,您需要首先计算(当然不是输出)最后一行。假设您想要五行,那么中间的数字将是2^(5-1),即16。因此您必须输出1 2 4 8 16。列位置将是0(开始),2(0加“1”加1空格的长度),4(2加1加1空格),6(4加1加1加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;
}
intmain(){
内线;
//从标准输入读取输入
cin>>线路;
//我们通过给每个数字分配一个固定的宽度来输出金字塔。
//这需要事先知道哪一个将是最大的数字。
//我们可以观察到,在每一行中,最大的数字是2对1
//该行号的幂:在第0行,最大的数字是2^0
//第1行是2,第4行是16
//也就是2^4。如果我们有五行(从0到4),最大的
//数字是2到4。
//现在,以10为底的数字的长度由对数给出
//以该数字的10为基数,截断后加1。例如
//1000正好是3,3+1是4位数字。999的Log10是
//2.9995654…截断为2,2+1为3,999为3位数字。
//这里我们的数字是(第1行)的2次方。
//利用对数的性质
//这与(第1行)*log10(2)相同,log10(2)约为0.3。
//所以我们将(第1行)乘以log10(2),截断为整数,再加上1
//(反之亦然:我们加1,然后指定宽度,这是一个
//整数,从而将值截断为整数。
//但是我们需要为填充空间添加另一个1(我们想要1 2 4
//21,不是12421…。所以在赋值之前,我们加上2,不是1。
内部宽度=2+(第1行)*0.3010299956639811952137389472449;
//////////////////////
//TODO:我们将输出2*行+1个字符串,每个“宽度”都很宽。
//因此,如果(2*line+1)*width>80,我们最好说出来并停下来,或者
//由于一个终端只有80个字符,所以输出将非常混乱
//最大宽度。这意味着N=9是我们能得到的最大值
//可以打印出来,仍然是“好”。
//由于没有人要求我们这样做,我们改为继续。
//////////////////////
//对于我们需要输出的每一行。。。
对于(int i=0;icout检查代码。这将给出所需的输出

#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;

}

您的程序没有接近您试图解决的问题的解决方案…首先,您应该读取输入(使用cin),然后写入解决方案的每一行(不仅是空格,还应该写入相应的数字).提示:每一行只有2的幂。你的问题是什么?这不是一个导师招募场所或聊天室,你知道。@SaraVF:实际上,从stdin中提取输入对MCVE来说是一件好事。我们鼓励它。你用
line=5
试过吗?当某个数字超过一位数时,OP的问题就会出现。是的。我想我我只需要纠正这些错误。它应该基于中间最大的数而不是行数。无论如何,谢谢你。我不知道代码有什么问题。但是我不能用我的DEVC++和CUT来编译它。宽度(宽度)对我来说是新的。你能更详细地解释一下吗。谢谢。我太笨了。我忘了包含。它工作得很好。但是你能详细解释cout.width(width)吗。它具体做什么。因为我们的教授要求我们提供课程的跟踪。同时提供流程图。非常感谢。听着,这个练习的目的是了解你为什么做某件事,以及它是如何做的。如果我提供这些,首先我会骗过你练习中有价值的一部分,其次是你在剩下的课程中,你的表现会更糟——你必须在这部分基础上进行构建。不过,我会在代码中添加一些注释。
        1
      1 2 1
    1 2 4 2 1
  1 2 4 8 4 2 1