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;
}