问题描述
想法是取一个数字作为输入并打印一个盒子的模式
如果输入为2,则两个盒子要打印在另一个内部
最小的盒子大小为3 * 3,下一个大盒子将是5 * 5,下一个盒子将是7 * 7,依此类推/>
输入1,然后画一个尺寸为3 * 3的盒子
对于输入2,外盒将是5 * 5,内部将是3 * 3
对于输入3,外盒将是7 * 7,另外还有2个内盒
因此对于n,最外面的盒子将是n * 2 + 1大小,带有(n1)个内盒子
所有盒子将左上角对齐,如图所示
输入格式:
第一行输入包含一个数字N
输出格式:
打印N个嵌套的盒子
限制条件:
1. 0< N< 25
我的尝试:
Idea is to take a number as input and print a pattern of boxes
If input is 2, two boxes are to be printed one inside the other
Smallest box will be of size 3*3, the next bigger box will be 5*5, the next one will be 7*7, so on and so forth
For input 1, then draw a box of dimensions 3*3
For input 2, outer box will be 5*5, inner will be 3*3
For input 3, outer box will be 7*7, with 2 more inner boxes
So for n, outermost box will be n*2 +1 in size, with (n1) inner boxes
All boxes will be top left aligned as shown in the figure
Input Format:
First line of input contains a number N
Output Format:
Print N nested boxes
Constraints:
1. 0 < N < 25
What I have tried:
#include<stdio.h>
#include<conio.h>
int main()
{
int j,i,n;
scanf("%d",&n);
for(j=0;j<n-2;j++)>
printf("-");
printf("\n");
for(i=0;i<n;i++)>
{
printf("-");
for(j=0;j<n-2;j++)>
printf(" ");
printf("-\n");
}
for(j=0;j<n;j++)>
printf("-");
printf("\n");
//}
return(0);
}
推荐答案
***
* *
***
*****
*****
** **
*****
*****
*******
*******
*******
*** ***
*******
*******
*******
而且很明显发生了什么:所有的行都是实心的,除了中间的一行,中间只有一个空白。
从那,它是非常简单:一个外部循环绘制每一行,两个内部循环绘制线的左半部分和右半部分,但决定在行的中间打印星形或空格。
试一试:它并不像你想象的那么难!
And it's pretty obvious what is happening: all the rows are solid, except the centre one which contains a single blank in the middle.
From that, it's pretty simple: One outer loop to draw each line, and two inner loops to draw the left and right halves of the line but a decision to print a star or a space in the middle of the line.
Try it: it isn't as difficult as you think!
+---+
|+-+|
|| ||
|+-+|
+---+
并推断您需要编程以再现相同的图纸。
and deduce what you need to program to reproduce the same drawing.
这篇关于我如何...盒子在盒子里,因为我只有一个盒子,但内盒没有被填满的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!