Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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++程序,但是我得到的输出是意外的(如下所示)。_C++_Nested Loops - Fatal编程技术网

写一个C++;使用嵌套循环将以下数字输出到屏幕的程序? 我写了一个C++程序,但是我得到的输出是意外的(如下所示)。

写一个C++;使用嵌套循环将以下数字输出到屏幕的程序? 我写了一个C++程序,但是我得到的输出是意外的(如下所示)。,c++,nested-loops,C++,Nested Loops,#包括 #包括 #包括 使用名称空间std; main() { INTA,b; 对于(a=3;ainta,b=0; 对于(a=3;a#包括 使用名称空间std; int main(int,char**) { int i=3; int numcols=1; 而(i如果你看第二个循环,它从1开始,如果你将它设置为b=a,它将帮助你分配,因为你只需要std::cout< 上述内容可能是您需要进行的唯一更改。Catch #include <iostream> #include <iom

#包括
#包括
#包括
使用名称空间std;
main()
{
INTA,b;
对于(a=3;a
inta,b=0;
对于(a=3;a
#包括
使用名称空间std;
int main(int,char**)
{
int i=3;
int numcols=1;

而(i如果你看第二个循环,它从1开始,如果你将它设置为b=a,它将帮助你分配,因为你只需要std::cout< 上述内容可能是您需要进行的唯一更改。

Catch

#include <iostream>
#include <iomanip>

int main() 
{
    while ( true )
    {
        const size_t N = 24;

        std::cout << "Enter a non-negative number less than " << N << " (0 - exit): ";

        size_t n = 0;
        std::cin >> n;

        if ( !n ) break;

        if ( N < n  ) n = N;

        std::cout << std::endl;

        for ( size_t i = 0, x = 3; i < n; i++, x += 2 )
        {
            for ( size_t j = 0; j <= i; j++ ) std::cout << std::setw( 4 ) << x + j;
            std::cout << std::endl;
        }

        std::cout << std::endl;
    }

    return 0;
}
循环也可以通过以下方式编写

    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j <= i; j++ )
        {
            std::cout << std::setw( 4 ) << 2 * i + 3 + j;
        }
        std::cout << std::endl;
    }
(大小i=0;i {
对于(size_t j=0;j这是您想要实现的吗

#include<stdio.h>
#include<iostream>
#include<iomanip>
using namespace std;
main()
{
int a,b;
for(a = 1 ;a <= 10; a++)
{
    for(b = 2 * a + 1;b <= 3 * a; b++)
    {
        cout<<setw(2)<<b<<" ";
    }
    printf("\n");
}
cout<<"\n";
}
#包括
#包括
#包括
使用名称空间std;
main()
{
INTA,b;

对于(a=1;a在你的问题中提供a,而不是要求人们阅读网站资源(或图片)中的链接。你给出的方式实际上很酷,我只是在运行程序时不需要输入数字是的,此方法的输出与Paul(y)给出的类似是的,两种解决方案都是正确的,但对我来说,我的代码更清晰。保罗的解决方案也很好。@OdieArdhia,您可以通过单击复选框接受答案(我的或保罗的),因此在将来,有类似问题的人将被引导到解决方案。
#include <iostream>
#include <iomanip>

int main() 
{
    while ( true )
    {
        const size_t N = 24;

        std::cout << "Enter a non-negative number less than " << N << " (0 - exit): ";

        size_t n = 0;
        std::cin >> n;

        if ( !n ) break;

        if ( N < n  ) n = N;

        std::cout << std::endl;

        for ( size_t i = 0, x = 3; i < n; i++, x += 2 )
        {
            for ( size_t j = 0; j <= i; j++ ) std::cout << std::setw( 4 ) << x + j;
            std::cout << std::endl;
        }

        std::cout << std::endl;
    }

    return 0;
}
Enter a non-negative number less than 24 (0 - exit): 10
   3
   5   6
   7   8   9
   9  10  11  12
  11  12  13  14  15
  13  14  15  16  17  18
  15  16  17  18  19  20  21
  17  18  19  20  21  22  23  24
  19  20  21  22  23  24  25  26  27
  21  22  23  24  25  26  27  28  29  30

Enter a non-negative number less than 24 (0 - exit): 0
    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j <= i; j++ )
        {
            std::cout << std::setw( 4 ) << 2 * i + 3 + j;
        }
        std::cout << std::endl;
    }
#include<stdio.h>
#include<iostream>
#include<iomanip>
using namespace std;
main()
{
int a,b;
for(a = 1 ;a <= 10; a++)
{
    for(b = 2 * a + 1;b <= 3 * a; b++)
    {
        cout<<setw(2)<<b<<" ";
    }
    printf("\n");
}
cout<<"\n";
}