php打印n乘n沙漏形状图形,《算法笔记》3.3小节——入门模拟->图形输出

@[TOC]

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

1933 Problem A 输出梯形

#include

#include

#include

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

{

//空格数,最大为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

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

//1993ProblemBHello World for U

#include

#include

#include

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

{

printf("%c",str[i]);

for(int j=0;j

printf(" ");

printf("%c\n",str[len-1-i]);

}

for(int k=i;k

{

printf("%c",str[k]);

}

}

return 0;

}

2003 Problem C 等腰梯形

//2003ProblemC等腰梯形

#include

#include

#include

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*+]

//2506ProblemD沙漏图形 tri2str [1*+]

#include

#include

#include

using namespace std;

int main()

{

int n;

scanf("%d",&n);

int max=2*n-1;

for(int i=0;i

{

//上方倒置的三角形

for(int j=0;j

{

printf(" ");

}

for(int k=0;k

{

printf("* ");

}

printf("\n");

}

for(int i=n-1;i>0;i--)//注意此处变量起始值为n-1,去除下方三角形的顶点

{

//下方正置的三角形

for(int j=0;j

{

printf(" ");

}

for(int k=0;k

{

printf("* ");

}

printf("\n");

}

return 0;

}

``

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值