杭电2052——Picture

本文介绍了一道ACM编程竞赛中的简单题目——矩形绘制,通过两种方法实现:一是构造特定字符串,二是使用嵌套循环。两种方法均采用C语言实现,适合初学者练习基本的输入输出及循环控制。

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

题目:Picture

Problem Description

Give you the width and height of the rectangle,darw it.

Input
Input contains a number of test cases.For each case ,there are two numbers n and m (0 < n,m < 75)indicate the width and height of the rectangle.Iuput ends of EOF.

Output

For each case,you should draw a rectangle with the width and height giving in the input.
after each case, you should a blank line.

Sample Input

3 2

Sample Output

±–+
| |
| |
±–+
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=2052


思路:

水题一道~
这道题就是用到简单的for循环就可以解决。而我用了两种方法来解决这道题。

  • 方法一:将这个图形看成是由两种数量不同的字符串组成的。
    对应的AC代码:
#include<stdio.h>
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		char w[80],h[80];
		int i;
		//构造两种字符串w,h
		for(i=0;i<n+2;i++)
		{
			if(i==0||i==n+1)
			{
				w[i]='+';
				h[i]='|';
			}
				
			else
			{
				w[i]='-';
				h[i]=' ';
			}
				
		}
		//注意注意:构造字符串的最后要记得加上\0哦~
		w[n+2]='\0';
		h[n+2]='\0';
		//分情况输出字符串
		for(i=0;i<m+2;i++)
		{
			if(i==0||i==m+1)
				printf("%s\n",w);
			else
			printf("%s\n",h);
		}
		printf("\n");/*注意题目要求每个图形后面有再加一行空白行哦,
		             所以最后要再打印一个换行*/
	}
}
  • 方法二:用一次嵌套循环,利用这些字符的位置特点,来限定条件,将这些字符一一打出来,最后构成图形
    对应的AC代码:
#include<stdio.h>
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		int i,j;
		for(i=0;i<m+2;i++)
		{
			for(j=0;j<n+2;j++)
			{
				if(i==0&&j==0||i==0&&j==n+1||
				i==m+1&&j==0||i==m+1&&j==n+1)
					printf("+");
				else if(i==0||i==m+1)
					printf("-");
				else if(j==0||j==n+1)
					printf("|");
				else
					printf(" ");
			}
			printf("\n");//注意打印每一行后要加换行符
		}
		printf("\n");/*注意题目要求每个图形后面有再加一行空白行哦,
		             所以最后要再打印一个换行*/
			
	}	
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值