一道ACM题, 请大家帮帮忙吧,真的要的很急啊.我错在超时上了,我拜托大家了...

Drawtriangle,parallelogramandrectangle.**********************************************... Draw triangle, parallelogram and rectangle.
* **** ****
*** **** ****
***** **** ****
******* **** ****
********* **** ****

Triangle (5) Parallelogram (4x5) Rectangle (4x5)
In the above figure between first brackets the dimension is given. For triangle the height will be given, for parallelogram and rectangle the width and height will be given. The character dot ('.') in the above figure will be represented by ASCII character space in the output.
Input
First line of each input set indicates category C.
If C=1 it is a triangle. Then following line of input indicates the height H ranging 1≤H≤10.
If C=2 it is a parallelogram. Then following line of input have two integers width W and height H ranging 1≤W≤10 and 1≤H≤10.
If C=3 it is a rectangle. Then following line of input have two integers width W and height H ranging 1≤W≤10 and 1≤H≤10.
C= -1 indicates the end of input set. This will not be considered as input.
Output
Output will be drawn by character '*' as following. Print a blank line after each output.
Sample input
1
5
2
5 3
3
4 6
-1
Sample output:
*
***
*****
*******
*********

*****
*****
*****

****
****
****
****
****
****
我的代码如下
#include<iostream>
using namespace std;

int main()
{
int c;
cin >> c;
while ( c == 1 )
{
int m, n, i;
cin >> m;
for ( n = 1; n <= m; n++ )
{
for ( i = 1; i <= m - n; i++ )
{
printf(" ") ;
}
for ( i = 1; i <= 2 * n - 1; i ++ )
{
printf("*");
}
for ( i = 1; i <= m - n; i ++ )
{
printf(" ") ;
}
printf("\n");
}
cin >> c;
}
while ( c == 2 )
{
int a, b, p, q;
cin >> a >> b;
for ( p = 1; p <= b; p++ )
{
for ( q = 1; q <= b - p; q ++ )
{
printf(" ");
}
for ( q = 1; q <= a; q ++ )
{
printf("*");
}
for ( q = 1; q <= p - 1; q ++ )
{
printf(" ") ;
}
printf("\n");
}
cin >> c;
}
while ( c == 3 )
{
int x, y, u, v;
cin >> x >> y;
for ( u = 1; u <= y; u ++ )
{
for ( v = 1; v <= x; v ++ )
{
printf("*");
}
printf("\n");
}
cin >> c;
}
while ( c == -1 )
{
break;
}
return 0;
}
展开
 我来答
liubird
2011-12-07 · TA获得超过1931个赞
知道小有建树答主
回答量:898
采纳率:100%
帮助的人:919万
展开全部
你的打印有问题,不要每次只输出一个字符,io的速度是很慢的,这样会造成超时的。
每次把一行的内容放在一个字符串里面,一行的内容一次输出,这样会大大减少运行时间的。

如下面,是一个采用line作为buffer程序:

#include <stdio.h>
#include <string.h>

int main()
{
int c, w, h, i, j, p, num;
char line[30], *q;
while(true) {
scanf("%d", &c);
if(c == -1) break;
if (c ==1) {
scanf("%d", &h);
for(i=1; i<=h; i++) {
p = 0;
for(j=0; j<h-i; j++) line[p++] = ' ';
for(j=0; j<2*i-1; j++) line[p++] = '*';
line[p] = '\0';
printf("%s\n", line);
}
printf("\n");
}
else if(c==2) {
scanf("%d%d", &w, &h);
for(i=1; i<=h; i++){
p = 0;
for(j=0; j<h-i; j++) line[p++] = ' ';
for(j=0; j<w; j++) line[p++] = '*';
line[p] = '\0';
printf("%s\n", line);
}
printf("\n");
} else if(c==3) {
scanf("%d%d", &w, &h);
for(i=0; i<w; i++) line[w] = '*';
line[w] = '\0';
for (i=0; i<h; i++) printf("%s\n", line);
printf("\n");
}
}
return 0;
}

另外, 一个一个的将字符写到字符串里面也是比较慢的,尤其是这些字符都是一样的,这样的情况下我们可以采用memset方法,也可以加快速度。

如下是用memset的实现:

#include <stdio.h>
#include <string.h>

int main()
{
int c, w, h, i, j, p, num;
char line[30], *q;
while(true) {
scanf("%d", &c);
if(c == -1) break;
if (c ==1) {
scanf("%d", &h);
for(i=1; i<=h; i++) {
memset(line, ' ', h-i);
memset(line + h-i, '*', 2*i-1);
line[h+i-1]='\0';
printf("%s\n", line);
}
printf("\n");
}
else if(c==2) {
scanf("%d%d", &w, &h);
memset(line, ' ', h-1);
memset(line+(h-1), '*', w);
line[w+h-1] = '\0';
for(i=0, q=line; i<h; i++) printf("%s\n", q++);
printf("\n");
} else if(c==3) {
scanf("%d%d", &w, &h);
memset(line, '*', w);
line[w] = '\0';
for (i=0; i<h; i++) printf("%s\n", line);
printf("\n");
}
}
return 0;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式