题目: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");/*注意题目要求每个图形后面有再加一行空白行哦,
所以最后要再打印一个换行*/
}
}