Contest100000577 - 《算法笔记》3.3小节——入门模拟->图形输出

本文介绍四道图形输出问题的算法实现,包括输出梯形、HelloWorldforU、等腰梯形及沙漏图形。通过具体代码示例展示了如何利用循环和字符输出来构造特定形状的图形。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Contest100000577 - 《算法笔记》3.3小节——入门模拟->图形输出

1933 Problem A 输出梯形

来自 http://codeup.cn/contest.php?cid=100000577

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main()
{
	int h;
	//可能有多组数据,所以为 while循环 
	while(scanf("%d",&h) != EOF)
	{
		int row=h;
		int rowmax = h+2*(h-1);//最底一行字符数 
		for(int i=0;i<h;i++)
		{
			//空格数,最大为rowmax-h 
			for(int j=0;j < (rowmax-h-2*i);j++)
			{
				printf(" ");
			}
			//字符数,各行最大为h+2*I 
			for(int k=0;k < (h+2*i);k++)
			{
				printf("*");
			}
			printf("\n");
		}
	}
	return 0;
}

1993 Problem B Hello World for U

来自 http://codeup.cn/contest.php?cid=100000577

题解:题目挺绕人,关键是根据提示将其抽象为数学模型,然后用代码展现

//1993ProblemBHello World for U
#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;

int main()
{
	char str[85];//存储字符串
	while(scanf("%s",str) != EOF)//考虑多个测试用例
	{
	//根据提示抽象出字符长度、两边的字符数,空格数==最后一行除去两边的字符数
		int len = strlen(str);//字符长度
		int side = (len+2)/3;//两边的字符数
		int last = len-side*2;//空格数==最后一行除去两边的字符数
		int i;
		for(i=0;i<side-1;i++)//输出最后一行外的其他行,从字符串两端向中间遍历
		{
			printf("%c",str[i]);
			for(int j=0;j<last;j++)
				printf(" ");			
			printf("%c\n",str[len-1-i]);
		}
		for(int k=i;k<len-i;k++)//输出最后一行
		{
			printf("%c",str[k]);	
		}
	}
	return 0;
}

2003 Problem C 等腰梯形

来自 http://codeup.cn/contest.php?cid=100000577

//2003ProblemC等腰梯形
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

int main()
{
	int m;
	while(scanf("%d",&m) != EOF)
	{
		while(m--)
		{
			int h;
			scanf("%d",&h);
			int max = h+2*(h-1);
			//此处特别注意循环的条件,很容易搞错,而且不同的理解
			//参数循环条件也不变 
			for(int i=1;i<=h;i++)//共h行,遍历 
			{
				//对称结构,则空格前后夹击一串* 
				for(int j=h-i;j>0;j--) 
				//每行两边空格各为 h-1,h-2,h-3'''h-h 
				{
					printf(" ");
				}
				//*符号数为h,h+2,h+4,,,h+2*(i-1) 
				for(int k=1;k<=h+2*(i-1);k++)
				{
					printf("*");
				}
				for(int j=h-i;j>0;j--)
				{
					printf(" ");
				}
				//换行 
				printf("\n");
			} 
		}
	}
	return 0;
}

2506 Problem D 沙漏图形 tri2str [1*+]

来自 http://codeup.cn/contest.php?cid=100000577

//2506ProblemD沙漏图形 tri2str [1*+]
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main()
{
	int n;
	scanf("%d",&n);
	int max=2*n-1;
	for(int i=0;i<n;i++)
	{
		//上方倒置的三角形 
		for(int j=0;j<i;j++)//循环,空" "与"* "反比 
		{
			printf(" ");
		}
		for(int k=0;k<n-i;k++)
		{
			printf("* ");
		}
		printf("\n");
	}
		 for(int i=n-1;i>0;i--)//注意此处变量起始值为n-1,去除下方三角形的顶点 
	 { 
		//下方正置的三角形 
		for(int j=0;j<i-1;j++)
		{
			printf(" ");
		}
		for(int k=0;k<n-i+1;k++)
		{
			printf("* ");
		}
		printf("\n");
	}
	return 0;
} 
``

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李霁明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值