Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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++来创建这个金字塔,但我有点卡住了。我可以得到一些帮助来完成我的例子吗 6***** *6**** **6*** ***6** ****6* *****6 6***** *6**** **6*** ***6** ****6* *****6 intnum1; cout_C++ - Fatal编程技术网

如何用C++;看起来像我的例子? 我想用C++来创建这个金字塔,但我有点卡住了。我可以得到一些帮助来完成我的例子吗 6***** *6**** **6*** ***6** ****6* *****6 6***** *6**** **6*** ***6** ****6* *****6 intnum1; cout

如何用C++;看起来像我的例子? 我想用C++来创建这个金字塔,但我有点卡住了。我可以得到一些帮助来完成我的例子吗 6***** *6**** **6*** ***6** ****6* *****6 6***** *6**** **6*** ***6** ****6* *****6 intnum1; cout,c++,C++,数字前后都需要星号。那么,让我们来扩展您的想法: int num1; cout<<"please enter a size between 1-9: "<<flush; cin>>num1; for(int i = 0; i < num1; i++) { //Asterisks before the number for(int j = 0; j < i; j++) { cout<<"*";

数字前后都需要星号。那么,让我们来扩展您的想法:

int num1;
cout<<"please enter a size between 1-9: "<<flush;
cin>>num1;

for(int i = 0; i < num1; i++)
{
    //Asterisks before the number
    for(int j = 0; j < i; j++)
    {
        cout<<"*";
    }

    cout <<num1;

    //Asterisks after the number
    for(int j = i; j < num1; j++)
    {
        cout<<"*";
    }

    cout << "\n";
}
intnum1;

cout有两种方法可以处理此问题:

  • 使用两个单独的循环,一个用于数字前面的星星,另一个用于数字后面的星星,例如:
#包括
使用名称空间std;
int main()
{
int num1;
cout>num1;
对于(int i=0;icout您可以使用带有计数和字符的
std::string
构造函数为每一行创建整个字符串

基本上是这样的模式:

  • 打印0颗星,数字6,然后
    num-1
    stars

  • 打印1颗星,数字6,然后
    num-2
    stars

  • 打印2颗星,数字6,然后打印
    num-3
    stars

    等等

  • 因此,模式是构建一个由前面的星星、数字6、后面的星星组成的字符串,对于每一行,增加前面的星星,减少后面的星星

    下面是一个例子:

    #include <string>
    #include <iostream>
    
    int main()
    {
      int num1 = 6;
      int stars_before = 0;
      int stars_after = num1 - 1;
      for (int i = 0; i < num1; ++i)
         std::cout << std::string(stars_before++, '*') << '6' << std::string(stars_after--, '*') << "\n";
    }
    

    在cout@amchacon修复之前,您可能需要一个for循环。*******7***********7**7*****7*****7*****7*******我该如何编写此循环,包括此代码工作良好我会将
    writeRow()
    重命名为更有意义的内容,如
    writeStars()
    writesterisks())
    。这是有效的。我该如何做相反的事情呢?这个问题让我对你的代码的工作方式产生了困惑。*****7****7*******7**7*****7*****7******
    int num1;
    cout<<"please enter a size between 1-9: "<<flush;
    cin>>num1;
    
    for(int i = 0; i < num1; i++)
    {
        //Asterisks before the number
        for(int j = 0; j < i; j++)
        {
            cout<<"*";
        }
    
        cout <<num1;
    
        //Asterisks after the number
        for(int j = i; j < num1; j++)
        {
            cout<<"*";
        }
    
        cout << "\n";
    }
    
    void writeRow(int length)
    {
        for (int i = 0; i < length; i++)
            std::cout << "*";
    }
    
    // Some code...
    
    for(int i = 0; i < num1; i++)
    {
        //Asterisks before the number
        writeRow(i);
    
        cout <<num1;
    
        //Asterisks after the number
        writeRow(num1-i);
    
        cout << "\n";
    }
    
    #include <string>
    #include <iostream>
    
    int main()
    {
      int num1 = 6;
      int stars_before = 0;
      int stars_after = num1 - 1;
      for (int i = 0; i < num1; ++i)
         std::cout << std::string(stars_before++, '*') << '6' << std::string(stars_after--, '*') << "\n";
    }
    
    6*****
    *6****
    **6***
    ***6**
    ****6*
    *****6