C++ 只有使用while循环才能实现吗?

C++ 只有使用while循环才能实现吗?,c++,while-loop,C++,While Loop,我的导师给了我一个作业,我必须编写一个只包含while循环和打印的代码: 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 我试了100次,但失败了100次。由于我的知识有限,我开始认为我的导师只是在搞乱我的大脑。如果可能的话,请给我介绍一个按顺序打印数字的代码。 谢谢…试试下面的代码。我还在这里创建了一个示例工作代码 向您的导师问好:您知道打印此图案的其他解决方案吗,如使用for loop?或者你能展示一些你尝试过的代码吗?数字前的空格是故

我的导师给了我一个作业,我必须编写一个只包含while循环和打印的代码:

        1
      2 1
    3 2 1
  4 3 2 1 
5 4 3 2 1
我试了100次,但失败了100次。由于我的知识有限,我开始认为我的导师只是在搞乱我的大脑。如果可能的话,请给我介绍一个按顺序打印数字的代码。
谢谢…

试试下面的代码。我还在这里创建了一个示例工作代码


向您的导师问好:

您知道打印此图案的其他解决方案吗,如使用for loop?或者你能展示一些你尝试过的代码吗?数字前的空格是故意的吗?我还没有开始使用for循环。有很多方法可以做到这一点。一个关键决定是将每次通过循环输出的值保留为std::string还是int。对于前者,可以在连续字符“2”、“3”、“4”等前面加上前缀。或者,如果将其存储为空格,则使用索引la[n]覆盖下一个空格;对于后者,添加20、300、4000等…添加您的最佳尝试代码可以帮助我们确定您的解决方案中缺少什么。我刚刚数了前面的spaceson第1行,总共是8行,这真的会产生规定的输出吗?我希望它只打印1。不过,这真的会产生规定的输出吗?看起来它会打印出1、12、123等等。嘿,天空之王……我也在筑巢来做这项工作。我定义了两个父级while循环,一个包含负责打印空格的while,另一个父级while包含负责打印整数的子级。仍然无法生成所需的输出。为什么这个答案被否决?它编译并给出正确的输出。成功了…谢谢你。当然……别告诉他你帮了他-
#include <iostream>

using namespace std;

int main()
{
int start_Index=1, maximum_Index=5;
while(start_Index<=maximum_Index)
{
   //below loop prints leading whitespaces
   //note that there are two whitespaces per number
   int temp_var=start_Index;

   while (maximum_Index-temp_var>0)
   {
     cout <<"  ";
     temp_var++;//note the post increment operator.
   }

   //below whiel loop prints lagging numbers with single whitespace before them
   temp_var=start_Index;
   while(temp_var>0)
   {
        cout<<" "<<temp_var--;//note the post decrement operator.
   }
   //Now we start over to next line
   cout<<endl;      
   //Increment the start_index by 1 
   start_Index++;
 }
 return 0;
}
int main(void)
{
    char str[11] = "          ";// Filled with 10 blank spaces
    int i=0;
    while(i < 5)
    {
        str[9 - 2*i] = (i+1) + 48;// +48 will give equivalent ASCII code
        printf("%s\n",str);
        i++;
    }
    return 0;
}
    int i = 1;
    int LIMIT = 5;
    while (i <= LIMIT)
    {
        int j = 1;
        while (j <= LIMIT -i)   //Loop to print the desired space.
        {
            cout << " ";
            j++;
        }
        int k = i;             
        while(k)
        {
            cout<<k;               //Printing the digits
            k--;
        }
        cout << endl;         //Adding new line character at the end.
        i++;
    }