我使用python脚本帕斯卡三角
我已经做了,直到这里,而且不知道如何在
numstr= raw_input("please enter the height:")
height = int()
tri = []
row1 = [1]
row2 = [1, 1]
tri.append(row1)
tri.append(row2)
while len(tri) < height:
我使用python脚本帕斯卡三角
我已经做了,直到这里,而且不知道如何在
numstr= raw_input("please enter the height:")
height = int()
tri = []
row1 = [1]
row2 = [1, 1]
tri.append(row1)
tri.append(row2)
while len(tri) < height:
添加你将不得不采取的最后一排有在寻找杨辉三角形三角形,创造下一个这样的:
你也可以计算出使用binomial coefficients新号码的结束,尽管这可能是更多的工作得到正确的。
其实,下一行越过最后一排的轴总和。例如,如果最后一行是[1,1],下一行是:
[1, 1]
+ [1, 1]
-----------
= [1, 2, 1]
[1, 2, 1]
+ [1, 2, 1]
--------------
= [1, 3, 3, 1]
因此,循环体可以是这样的:
tri.append(map(lambda x, y: x + y, [0] + tri[-1], tri[-1] + [0]))
这里是我的解决方案产生一个帕斯卡三角
def factorial(x):
return 1 if x == 0 else x * factorial(x - 1)
def triangle(n):
return [[factorial(i)/(factorial(j) * factorial(i - j)) for j in range(i + 1)] for i in range(n)]
尝试scipy pascal模块:
from scipy.linalg import pascal
pascal(6, kind='lower')
输出:
array([[ 1, 0, 0, 0, 0, 0],
[ 1, 1, 0, 0, 0, 0],
[ 1, 2, 1, 0, 0, 0],
[ 1, 3, 3, 1, 0, 0],
[ 1, 4, 6, 4, 1, 0],
[ 1, 5, 10, 10, 5, 1]], dtype=uint64)
// C++ code for pascal triangle
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
long unsigned int Factorial(long unsigned int Number)
{
long unsigned int Fact=0;
if (Number==0)
return (long unsigned int) 1;
else
{ Fact=Number*Factorial(Number-1);
return Fact;
}
}
long unsigned int Combination(long unsigned int num1,long unsigned int num2)
{
long unsigned int Comb,num3;
long unsigned int Factor1, Factor2,Factor3;
Factor1=Factorial(num1);
Factor2=Factorial(num2);
num3=num1-num2;
Factor3=Factorial(num3);
Comb=Factor1/(Factor2*Factor3);
return(Comb);
}
int main()
{
long unsigned int i,j,Num=0;
long unsigned int **Matrix;
clrscr();
printf(" %d\n " ,sizeof(long unsigned int));
printf("Enter Index of Square Matrix Num =: ");
scanf ("%lu",&Num);
Matrix=(long unsigned int **) malloc(Num*Num*sizeof(long unsigned int *));
for(i=0;i<Num;i++)
{ for (j=0;j<Num;j++)
{ *(*(Matrix+i)+j)=0;
}
}
for(i=0;i<Num;i++)
{ for(j=0;j<=i;j++)
{ printf(" %lu " , *(*(Matrix+i)+j)); }
printf("\n");
}
for(i=0;i<Num;i=i+1)
{
for(j=0;j<=i;j=j+1)
{
*(*(Matrix+i)+j)=Combination(i,j);
}
printf("\n");
}
for(i=0;i<Num;i++)
{
for(j=0;j<=i;j++)
{
// printf(" \n %lu %lu \n",i,j);
printf(" %lu ",*(*(Matrix+i)+j));
}
printf("\n");
}
getch();
return(0);
}
虽然不希望采取任何从你的努力了,到目前为止,有*是*一个杨辉三角的代码高尔夫球:http://stackoverflow.com/questions/1242073/code - 高尔夫球-生成帕斯卡三角 – pavium 2009-11-16 07:12:46