cout执行时间的问题
想问一下为什么9和输出金字塔之间会有换行?
我试过了用C来编的话是不会出现这个换行的,就是用cout出现了问题~~先谢谢啦
35、打印数字金字塔
#include <iostream>
using namespace std;
int main()
{
int n,i,j,p;cin>>n;
for(i=0;i<n+1;i++)
{
for(p=0;p<2*(n-i);p++){cout<<" ";}
for(j=1;j<i+1;j++)
cout<<j<<" ";
for(j=i-1;j>0;j--)
cout<<j<<" ";
cout<<endl;
}
return 0;
}
图形
9
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
请按任意键继续. . .
**9和下面的金字塔之间有一个空行是因为先执行了for之后的cout<<endl然后再按正常顺序执行,初步判断是由于cout缓冲区的问题
如下所示:
9
*
1 *
1 2 1 *
1 2 3 2 1 *
1 2 3 4 3 2 1 *
1 2 3 4 5 4 3 2 1 *
1 2 3 4 5 6 5 4 3 2 1 *
1 2 3 4 5 6 7 6 5 4 3 2 1 *
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 *
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 *
请按任意键继续. . . 代码如下:
#include <iostream>
using namespace std;
int main()
{
int n,i,j,p,k;
cin>>n;
for(i=0;i<n+1;i++)
{
for(p=0;p<2*(n-i);p++)
{cout<<" ";}
for(j=1;j<i+1;j++)
cout<<j<<" ";
for(j=i-1;j>0;j--)
cout<<j<<" ";
cout<<"*"<<endl;
}
return 0;
}