UVA - 488 - Triangle Wave

本文探讨了在处理三角波绘制任务时,通过优化输出方式(如避免使用endl),显著提升程序运行速度的方法。详细介绍了如何从0.288s优化至0.000s,以及在三角波绘制算法中直接使用' '或putchar(' ')的技巧。

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

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=429


题意:根据输入的幅值和频率,输出对应的三角波。水题,注意空格。


收获:

  cout<<endl;每次都会刷新缓冲区,如果经常使用endl,会很慢,直接2.072s。

  改用cout <<'\n' 或者putchar('\n'),快很多,直接0.288s。

不知道还有哪些地方可以改的很快?别人可以0.000s过。


#include <iostream>
#include <stdio.h>

using namespace std;


void drawWave(int Amplitude)
{
	int tmp;
	for ( int i=1; i<=Amplitude; i++ )
	{
		tmp = i;
		while ( tmp-- )
		{
			cout <<i;
		} // end while
		cout <<'\n';
// 		putchar('\n');
	} // end for

	for ( int i=Amplitude-1; i>0; i-- )
	{
		tmp = i;
		while ( tmp-- )
		{
			cout <<i;
		} // end while
		cout <<'\n';
// 		putchar('\n');
	} // end for
}


int main()
{
	int nCase;
	cin >>nCase;
	while ( nCase-- )
	{
		int Amplitude, Frequence;
		cin >>Amplitude >> Frequence;
		for ( int i=0; i<Frequence; i++ )
		{
			drawWave(Amplitude);
			if ( i != Frequence-1 )
			{
				cout <<'\n';
			} // end if
		} // end for

		if ( nCase != 0)
		{
			cout <<'\n';
		} // end if
	} // end while
	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值