#include <iostream>
#include <iomanip>
using namespace std;
int com(int y,int x) //组合
{
if(x==0||x==y)return 1;
else if(x<=y)
{
int temp=1,temp2=1;
for(int i=1;i<=x;++i)temp=temp*i;
for(int j=y;x>0;--x,--j)temp2=temp2*j;
return temp2/temp;
}
else return -1; //错误
}
main()
{
int n,j;
cout<<"输入行数";
cin>>n;
j=n;
for(int i=0;i<n;++i,--j)
{
int temp=j;
while(--temp){cout<<" ";}
for(int k=0;k<=i;++k)
{
cout<<setw(4)<<com(i,k);
}
cout<<endl;
}
}
c++ 杨辉三角的输出,不用数组。
最新推荐文章于 2023-04-12 20:11:51 发布