输出数字横板“金字塔”

一个试题,本来挺简单的,由于时间久了没写连输入函数都不会用了,记录一下

试题如下:
// 写一个C语言程序,编译二进制文件为program,以下是运行和输出结果的例子,比如输入5:
// $./program 5
// 1
// 1 2
// 1 2 3
// 1 2 3 4
// 1 2 3 4 5
// 1 2 3 4
// 1 2 3
// 1 2
// 1

#include <stdio.h>

int main()
{
    
    
	int n = 0;

	printf("请输入一个大于0的数字:");
	scanf_s("%d", &n);
	while (1)
	{
    
    
		if (n > 0)
		{
    
    
			break;
		}
		else
		{
    
    
			printf("输入错误,请再试一次:");
			scanf_s("%d", &n);
		}
	}

	//上半部分
	for (int i = 1; i < n+1; i++)
	{
    
    
		for (int j = 1; j < i + 1; j++)
		{
    
    
			printf("%d ", j);
		}
		printf("\n");
			
	}
	//下半部分
	while (n > 0)
	{
    
    
		for (int i = 1; i < n; i++)
		{
    
    
			printf("%d ", i);
		}
		printf("\n");
		n = n - 1;
	}

	return 0;
}

输出结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43689161/article/details/123905144