C编程嵌套for循环打印半金字塔
C programming nested for loop print half pyramid
我在尝试用 C 编程打印半个金字塔时遇到了一些问题。当我将高度设置为 7 时的输出应该是这样的:
1
22
333
1111
22222
333333
1111111
我的代码为:
int main()
{
int height, i, row, j;
printf("Enter the height: ");
scanf("%d", &height);
for (i = height; i >= 0; i--) //print the number of rows based on input
{
for (row = 0; row < i-1; row++) {
printf(" "); // print spaces for each rows
//Edited as I just realized that I did it in the Java way. So sorry
/*for (j = row-1; j < row; j++) {
printf("%d", j);
}*/
}
printf("\n");
}
return 0;
}
我现在设法打印出每行的倒排空格。但我有点卡在如何打印出每一行的数字,因为我注释掉的那一行使我的 exe 停止工作。
这是一个简单的问题,正确的代码是:
int main()
{
int height, i, row, j,no=1;
printf("Enter the height: ");
scanf("%d", &height);
for (i = height; i > 0; i--) //print the number of rows based on input
{
if(no>3)
no=1;//so that numbers never exceed 3
for (row = 0; row < i-1; row++)
printf(" "); // print spaces for each rows
for(j=height+1;j>i;--j)
printf("%d",no);//print the numbers
no++;
printf("\n");
}
return 0;
}
您注释的 for 循环不应嵌套,您只需要获取 i
的值并从那里一直到 height
。
此外,顶层 for 循环中的条件应为 i > 0
。
int main()
{
int height, i, row, j;
printf("Enter the height: ");
scanf("%d", &height);
for (i = height; i > 0; i--) //print the number of rows based on input
{
for (row = 0; row < i; row++) {
printf(" ");
}
for (j = i; j <= height; j++) {
printf("%d", (height - i) % 3 + 1);
}
printf("\n");
}
return 0;
}
这输出:
Enter the height: 7
1
22
333
1111
22222
333333
1111111
另一个代码。
#include <stdio.h>
int main()
{
int i, j;
int width = 7;
int height = 7;
int b = 3;
for(i = 1; height >= i; ++i) {
for(j = 1; width >= j; ++j) {
if(width - i < j) {
int x = (i % b);
printf("%d", x ? x : b);
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
接住!:)
#include <stdio.h>
int main( void )
{
while ( 1 )
{
printf( "Enter a non-negative number (0-exit): " );
unsigned int n = 0;
scanf( "%u", &n );
if ( !n ) break;
printf( "\n" );
for ( unsigned int i = 0; i < n; i++ )
{
const unsigned int k = i % 3 + 1;
printf( "%*d", n - i, k );
for ( unsigned int j = 0; j < i; j++ ) printf( "%d", k );
printf( "\n" );
}
printf( "\n" );
}
}
如果按顺序进入
10 7 0
输出将是
Enter a non-negative number (0-exit): 10
1
22
333
1111
22222
333333
1111111
22222222
333333333
1111111111
Enter a non-negative number (0-exit): 7
1
22
333
1111
22222
333333
1111111
Enter a non-negative number (0-exit): 0
只是为了表明您可以在 printf
中使用宽度参数。
#include <stdio.h>
int main() {
int height, j, n = 0;
printf("Enter the height: ");
scanf("%d", &height);
for (j = 0; j < height; n *= 10)
{
printf("%*d\n", height+1, ++n * (1 + j++ % 3));
}
return 0;
}
我在尝试用 C 编程打印半个金字塔时遇到了一些问题。当我将高度设置为 7 时的输出应该是这样的:
1
22
333
1111
22222
333333
1111111
我的代码为:
int main()
{
int height, i, row, j;
printf("Enter the height: ");
scanf("%d", &height);
for (i = height; i >= 0; i--) //print the number of rows based on input
{
for (row = 0; row < i-1; row++) {
printf(" "); // print spaces for each rows
//Edited as I just realized that I did it in the Java way. So sorry
/*for (j = row-1; j < row; j++) {
printf("%d", j);
}*/
}
printf("\n");
}
return 0;
}
我现在设法打印出每行的倒排空格。但我有点卡在如何打印出每一行的数字,因为我注释掉的那一行使我的 exe 停止工作。
这是一个简单的问题,正确的代码是:
int main()
{
int height, i, row, j,no=1;
printf("Enter the height: ");
scanf("%d", &height);
for (i = height; i > 0; i--) //print the number of rows based on input
{
if(no>3)
no=1;//so that numbers never exceed 3
for (row = 0; row < i-1; row++)
printf(" "); // print spaces for each rows
for(j=height+1;j>i;--j)
printf("%d",no);//print the numbers
no++;
printf("\n");
}
return 0;
}
您注释的 for 循环不应嵌套,您只需要获取 i
的值并从那里一直到 height
。
此外,顶层 for 循环中的条件应为 i > 0
。
int main()
{
int height, i, row, j;
printf("Enter the height: ");
scanf("%d", &height);
for (i = height; i > 0; i--) //print the number of rows based on input
{
for (row = 0; row < i; row++) {
printf(" ");
}
for (j = i; j <= height; j++) {
printf("%d", (height - i) % 3 + 1);
}
printf("\n");
}
return 0;
}
这输出:
Enter the height: 7
1
22
333
1111
22222
333333
1111111
另一个代码。
#include <stdio.h>
int main()
{
int i, j;
int width = 7;
int height = 7;
int b = 3;
for(i = 1; height >= i; ++i) {
for(j = 1; width >= j; ++j) {
if(width - i < j) {
int x = (i % b);
printf("%d", x ? x : b);
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
接住!:)
#include <stdio.h>
int main( void )
{
while ( 1 )
{
printf( "Enter a non-negative number (0-exit): " );
unsigned int n = 0;
scanf( "%u", &n );
if ( !n ) break;
printf( "\n" );
for ( unsigned int i = 0; i < n; i++ )
{
const unsigned int k = i % 3 + 1;
printf( "%*d", n - i, k );
for ( unsigned int j = 0; j < i; j++ ) printf( "%d", k );
printf( "\n" );
}
printf( "\n" );
}
}
如果按顺序进入
10 7 0
输出将是
Enter a non-negative number (0-exit): 10
1
22
333
1111
22222
333333
1111111
22222222
333333333
1111111111
Enter a non-negative number (0-exit): 7
1
22
333
1111
22222
333333
1111111
Enter a non-negative number (0-exit): 0
只是为了表明您可以在 printf
中使用宽度参数。
#include <stdio.h>
int main() {
int height, j, n = 0;
printf("Enter the height: ");
scanf("%d", &height);
for (j = 0; j < height; n *= 10)
{
printf("%*d\n", height+1, ++n * (1 + j++ % 3));
}
return 0;
}