用C语言打印杨辉三角(简单,高分)

ztj7831 2004-10-19 11:57:29
#include<iostream.h>
void main()
{
char a[5][5];
int i;
int j;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
a[i][j]=' ';
for(i=0;i<5;i++)
{
a[i][0]='*';
a[i][i]='*';
}
for(i=0;i<5;i++)
{
for(j=0;j<i;j++)
{
a[i][j]=a[i-1][j-1]+a[i-1][j];
cout<<a[i][j]<<"\c";
}
cout<<endl;
}
}

要求:
1、用一个三角打印出来
2、用一层循环控制打多少行(即一共打五行)然后再用一个循环控制打出每一行的*
3、弄出一个公式,在第二层循环中,能控制第一行打出一个*,第二行打二个,第三行打三个,第四行又打二个,第五行打一个。
...全文
1017 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
fire314159 2004-10-20
  • 打赏
  • 举报
回复
注意位数不够的溢出问题!!!!!
注意位数不够的溢出问题!!!!!
注意位数不够的溢出问题!!!!!
注意位数不够的溢出问题!!!!!
xcyxl 2004-10-20
  • 打赏
  • 举报
回复
我忘了是什么造型了?
有哪位大哥给打印一下
athena301 2004-10-20
  • 打赏
  • 举报
回复
接分的。。。
ztj7831 2004-10-20
  • 打赏
  • 举报
回复
大家都来看看!!!!
下午送分开始!!!
请高手都来喝汤!!!
lo_vuvk 2004-10-20
  • 打赏
  • 举报
回复
曾经写过,现在不记得了
malligator 2004-10-20
  • 打赏
  • 举报
回复
D
junyh 2004-10-20
  • 打赏
  • 举报
回复
好东东
xuzheng318 2004-10-19
  • 打赏
  • 举报
回复
杨辉三角是二项式展开的系数。
也可以根据数学公式来算C(n,m)=C(n-1,m)+C(n-1,m-1)


main()
{
int a[100][100],i,j,n;

clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
{
a[i][0]=1;
a[i][i]=1;
for(j=1;j<i;j++)
{
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
for (i=0;i<n;i++)
{
for (j=0;j<i+1;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
王旺旺旺 2004-10-19
  • 打赏
  • 举报
回复
靠。
我学PASCAL时编的一个小小的习题。
lovessm 2004-10-19
  • 打赏
  • 举报
回复
是不是给我送分的?
insulator 2004-10-19
  • 打赏
  • 举报
回复
如果要输出为*只要把
for(i=0;i<5;i++)
{
cout<<endl;
for(j=0;j<=i;j++)
cout<<a[i][j]<<" ";
}

改为
for(i=0;i<5;i++)
{
cout<<endl;
for(j=0;j<=i;j++)
cout<<"*"<<" ";
}
insulator 2004-10-19
  • 打赏
  • 举报
回复
#include <iostream.h>
int main()
{
int a[5][5];
int i;
int j;
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
a[i][j]=0;
for(i=0;i<5;i++)
a[i][0]=1;
a[0][0]=1;
for(i=1;i<5;i++)
{
for (j=1;j<=i;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
for(i=0;i<5;i++)
{
cout<<endl;
for(j=0;j<=i;j++)
cout<<a[i][j]<<" ";
}
getchar();
}
hcj2002 2004-10-19
  • 打赏
  • 举报
回复
确实是“简单,高分”

:(
yangsongx 2004-10-19
  • 打赏
  • 举报
回复
到本论坛搜一下,这方面的内容已经太多的了~~~
jp1984 2004-10-19
  • 打赏
  • 举报
回复
dp
albertlee 2004-10-19
  • 打赏
  • 举报
回复
我的天, 这题目是初中时在小霸王学习机上用 FBasic作过的。
黄有才 2004-10-19
  • 打赏
  • 举报
回复
asdrfasdfasdfasdf
davidhua2000 2004-10-19
  • 打赏
  • 举报
回复
22222222222222
davidhua2000 2004-10-19
  • 打赏
  • 举报
回复
1111111111
eaglessky 2004-10-19
  • 打赏
  • 举报
回复
这是我的一个 c++程序:
适当修改一下就可以了:


// * 打印杨三角 *


#include <iomanip.h>
#include <iostream.h>

void main()
{
int *asingle=NULL;
int *adouble=NULL;
int n,i=1,p=0;

cout<<"please input the numbers of the rows : n=";

cin>>n;
int wide=n*5;
cout<<"\n the yanghui sanjiao is: \n"<<endl;

asingle=new int[i]; // apply the first row ,
asingle[p]=1;
cout<<setiosflags(ios::right)<<setw(wide-2*i-1)<<asingle[p]<<endl;

i++;
while(i<=n)
{
adouble=new int[i]; //apply the double row ; the numbers in the row are create form the last one .
for(p=0;p<i;p++)
{
if(p==0)
{
adouble[p]=asingle[0];
cout<<setiosflags(ios::right)<<setw(wide-3*i)<<adouble[p]<<" ";
continue;
}
if(p==i-1)
{
adouble[p]=asingle[i-2];
cout<<setw(3)<<adouble[p]<<endl;
continue;
}
adouble[p]=asingle[p-1]+asingle[p];
cout<<setw(3)<<adouble[p]<<" ";
}
i++;

asingle=new int[i]; // apply the single row;
for(p=0;p<i;p++)
{
if(p==0)
{
asingle[p]=adouble[0];
cout<<setiosflags(ios::right)<<setw(wide-3*i)<<asingle[p]<<" ";
continue;
}
if(p==i-1)
{
asingle[p]=adouble[i-2];
cout<<setw(3)<<asingle[p]<<endl;
continue;
}
asingle[p]=adouble[p-1]+adouble[p];
cout<<setw(3)<<asingle[p]<<" ";
}
i++;
}
delete[] asingle;
delete []adouble;
}
加载更多回复(8)

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧