使用嵌套循环编写一个 C++ 程序将以下数字输出到屏幕?
Write a C++ program using nested loops to output the following numbers to the screen?
我写了一个C++程序,但是得到的输出却出人意料(如下图)
#include<stdio.h>
#include<iostream>
#include<iomanip>
using namespace std;
main()
{
int a,b;
for(a=3;a<=21;a+=2)
{
printf("\n");
for(b=1;b<a;b+=2)
{
cout<<setw(2)<<a+b-1<<" ";
}
}
cout<<"\n";
}
谁能帮我调整一下,让它像这样运行:
int a,b = 0;
for(a=3;a<=21;a+=2)
{
printf("\n");
b++;
for(int i =0;i < b;i++)
{
cout<<setw(2)<<a+i<<" ";
}
在内部循环之外递增 b
,这样您就可以计算每行需要多少个数字。
#include <iostream>
using namespace std;
int main(int, char**)
{
int i = 3;
int numcols = 1;
while(i <= 21)
{
int j = 0;
for(j = 0; j < numcols; j++)
{
cout << i + j << '\t';
}
cout << '\n';
i += 2;
numcols += 1;
}
}
如果tab
太宽,可以用双space代替。
如果您查看第二个循环,您会发现它从 1 开始,如果您将其设置为 b=a,它将帮助您分配,因为您只需要 std::cout<
以上可能是您需要进行的唯一更改。
接住!
#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;
}
如果输入 10 则输出将是
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";
}
我写了一个C++程序,但是得到的输出却出人意料(如下图)
#include<stdio.h>
#include<iostream>
#include<iomanip>
using namespace std;
main()
{
int a,b;
for(a=3;a<=21;a+=2)
{
printf("\n");
for(b=1;b<a;b+=2)
{
cout<<setw(2)<<a+b-1<<" ";
}
}
cout<<"\n";
}
谁能帮我调整一下,让它像这样运行:
int a,b = 0;
for(a=3;a<=21;a+=2)
{
printf("\n");
b++;
for(int i =0;i < b;i++)
{
cout<<setw(2)<<a+i<<" ";
}
在内部循环之外递增 b
,这样您就可以计算每行需要多少个数字。
#include <iostream>
using namespace std;
int main(int, char**)
{
int i = 3;
int numcols = 1;
while(i <= 21)
{
int j = 0;
for(j = 0; j < numcols; j++)
{
cout << i + j << '\t';
}
cout << '\n';
i += 2;
numcols += 1;
}
}
如果tab
太宽,可以用双space代替。
如果您查看第二个循环,您会发现它从 1 开始,如果您将其设置为 b=a,它将帮助您分配,因为您只需要 std::cout<
以上可能是您需要进行的唯一更改。
接住!
#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;
}
如果输入 10 则输出将是
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";
}