曾經vb,c ,c++,python,寫過的楊輝三角,致敬楊老前輩

// 楊輝三角形,又稱賈憲三角形,帕斯卡三角形,是二項式係數在三角形中的一種幾何排列。楊輝三角形同時對應於二項式定理的係數。




直角三角形楊輝三角

//c語言,求直角的

#include<stdio.h>

#define M 10

void main()

{

int a[M][M], i , j ;

for(i=0;i<M;i++)

for(j=0;j<=i;j++)

{

if(i==j||j==0)

a[i][j]=1;

else

a[i][j]=a[i-1][j]+a[i-1][j-1];

printf("%d",a[i][j]);

楊輝三角形 if(i==j)printf("\n");

}

}

使用數組打印金字塔型楊輝三角

#include<stdio.h>

void main()

{

int a,i,j;

for(i=0;i<10;i++)

{

for(j=10;j>=i;j--)

printf("%2c",' ');/*兩個空格*/

for(j=0;j<=i;j++)

{

i f(i==j||j==0)

a[i][j]=1;

else

a[i][j]=a[i-1][j]+a[i-1][j-1];

printf("%3d ",a[i][j]); /*%3d後一個空格*/

if(i==j)

printf("\n");

}

}

}

不用數組輸出金字塔形楊輝三角

#include<stdio.h>

#define N 10

void main()

{

unsigned int i,j,k;

unsigned int b,c;

for(i=0;i<N;i++)

{

for(j=N;j>i;j--)

printf("");

for(j=0;j<=i;j++)

{

b=c=1;

if(j>=1)

{

for(k=i-j+1;k<=i;k++)

b*=k;

for(k=1;k<=j;k++)

c*=k;

}

printf("%4d",b/c);

}

printf("\n");

}

}

註解:

在打印楊輝三角時通常用到楊輝三角的兩個性質。

第一個就是楊輝三角中除了最外層(不包括楊輝三角底邊)的數爲1外,其餘的數都是它肩上兩個數之和。用數組輸出楊輝三角就用這個性質。

第二個性質是楊輝三角的第n行恰好是C(n,0)~C(n,n)。這裏的C表示組合。不用數組輸出楊輝三角就用這個性質。把楊輝三角的前15行保存在文本文件中 #include

#include<stdlib.h>

#define M 15

void main()

{

FILE *out;

if((out=fopen("D:\\text_1.txt","w"))==NULL)

{

printf("Error!\n");

exit(0);

}

int a[M][M],i,j;

for(i=0;i<M;i++)

for(j=0;j<=i;j++)

{

if(i==j||j==0)

a[i][j]=1;

else

a[i][j]=a[i-1][j]+a[i-1][j-1];

fprintf(out,"%5d",a[j]);

if(i==j)

fputc('\n',out);

}

fclose(out);

}

用二維數組輸出前十行:

#include <stdio.h>

void main ()

{

int a,i,j;

for(i=0;i<10;i++)

{

a[i][i]=1;

a[i][0]=1;

}

for (i=2;i<10;i++)

for (j=1;j<=i-1;j++)

a[i][j]=a[i-1][j-1]+a[i-1][j];

for(i=0;i<10;i++)

{

for (j=0;j<=i;j++)

printf("%6d",a[i][j]);

printf("\n");

}

printf("\n");

return 0;

}

菱形楊輝三角

main()

{

int i,j;

int a;//該程序輸出爲6層,可根據需要更改數組大小

clrscr();

printf("\n\n\n");

for(i=0;i<6;i++)

for(j=0;j<=i;j++)

{

if(j==0||j==i)

a[i][j]=1;

else a[i][j]=a[i-1][j-1]+a[i-1][j];

}

for(i=0;i<6;i++)

{

for(j=0;j<=6-i;j++)

printf("%2c",' ');

for(j=0;j<=i;j++)

printf("%4d",a[i][j]);

楊輝三角 printf("\n");

}

for(i=4;i>=0;i--)

{

for(j=0;j<=6-i;j++)

printf("%2c",' ');

for(j=0;j<=i;j++)

printf("%4d",a[i][j]);

printf("\n");

}

getch();

}

c#輸出

class Program

{

public void yanghui(int value)

{

if (value < 3)

{

Console.WriteLine("請重新輸入數組大於3的值!");

}

else

{

int[,] arry = new int[value, value];

Console.WriteLine("數組爲:");

for (int i = 0; i < value; i++)

{

string str = "";

   str = str.PadLeft(value - i);

   Console.Write(str);

for (int j = 0; j <= i; j++)

{

if (i == j || j == 0)

{

arry[i, j] = 1;

}

else

{

arry[i, j] = arry[i - 1, j - 1] + arry[i - 1, j];

}

Console.Write(arry[i, j] + " ");

}

Console.WriteLine();

}

}

}

static void Main(string args)

{

Program p = new Program();

Console.WriteLine("請輸入數組值:");

string str_name = Console.ReadLine();

int value = Convert.ToInt16(str_name);

p.yanghui(value);

Console.Readkey();

}

}

VB輸出

Private Sub Form_click()

n = Val(Text1.Text)

ReDim a(n + 1, n + 1), b(n + 1, n + 1)

Cls

k = 8

For i = 1 To n

Print String((n - i) * k / 2 + 1, " ");

For j = 1 To i

a(i, 1) = 1

a(i, i) = 1

a(i + 1, j + 1) = a(i, j) + a(i, j + 1)

b(i, j) = Trim(Str(a(i, j)))

Print b(i, j); String(k - Len(b(i, j)), " ");

Next j

Print

Next i

End Sub

創建一個text和command,在text中輸入所需行數,點擊command即可。一個數在楊輝三角出現的次數 由1開始,正整數在楊輝三角形出現的次數爲∞:1, 2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 2, 4, ... (OEIS:A003016)。最小而又大於1的數在賈憲三角形至少出現n次的數爲2, 3, 6, 10, 120, 120, 3003, 3003, ... (OEIS:A062527)

除了1之外,所有正整數都出現有限次。

只有2出現剛好一次。

6,20,70等出現三次。

出現兩次和四次的數很多。

還未能找到出現剛好五次的數。

120,210,1540等出現剛好六次。(OEIS:A098565)

因爲丟番圖方程

有無窮個解,所以出現至少六次的數有無窮個多。

其解答,

楊輝三角其中Fn表示第n個斐波那契數(F1 = F2 = 1)。

3003是第一個出現八次的數。

一道NOIP楊輝三角題目:

#include<stdio.h>

#define maxn 50

const int y=2009;

int main()

{

int n,c[maxn][maxn],i,j,s=0;

scanf("%d",&n);

c[0][0]=1;

for(i=1;i<=n;i++)

{

c[i][0]=1;

for(j=1;j<i;j++)

c[i][j]=c[i-1][j-1]+c[i-1][j];

c[i][i]=1;

}

for(i=0;i<=n;i++)

s=(s+c[n][i])%y;

printf("%d\n",s);

return 0;

此爲利用數組求和

C++輸出

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

//單數組動態規劃輸出楊輝三角,以下截止第31行

#include <iostream>

using namespace std;

#define MAXH 31

int main()

{

int i,j;

unsigned long num[MAXH] = {0};

num[0] = 1;

for (i = 0; i < MAXH; i++) {

for (j = i; j > 0; j--) {

num[j] = num[j] + num[j - 1]; // A[i,j]=A[i,j-1]+A[i,j]

cout<

}

cout<<"1"<<endl;

}

return 0;

}

數組輸出

/直角三角形/

#include<iostream>

using namespace std;

int main()

{

int h,i,j;

cout<<"請輸入楊輝三角的高度:"<<endl;

cin>>h;

int a;

for(i=0;i<10;i++)

{

a[i][i]=1;

a[i][0]=1;

}

for(i=2;i<10;i++)

for(j=1;j<=i-1;j++)

a[i][j]=a[i-1][j-1]+a[i-1][j];

for(i=0;i<=h;i++)

{

for(j=0;j<=i;j++)

cout<<a[i][j]<<'\t';

cout<<endl;

}

return 0;

}

/等腰三角形/

#include<iostream>

using namespace std;

int main()

{

int i,j,h,a[10][10];

cout<<"請輸入楊輝三角的高度:"<<endl;

cin>>h;

for(i=0;i<=h;i++)

{

for(j=0;j<=i;j++)

{

if(i==j||j==0)

a[i][j]=1;

else

楊輝三角 a[i][j]=a[i-1][j]+a[i-1][j-1];

}

}

for(i=0;i<=h;i++)

{

for(j=h;j>=i;j--)

cout<<" ";

for(j=0;j<=i;j++)

{

cout<<a[i][j]<<'\t';

if(i==j)

cout<<endl;

}

}

return 0;

}

直角輸出

#include<iostream>

using namespace std;

int computeTriangleElement(int level,int index);

void yanghuiTriangle(int level);

void yanghuiTriangle(int level)

{

for(int i=1;i<=level;i++)

{

for(int j=1;j<=i;j++)

{

cout<<computeTriangleElement(i,j)<<' ';

}

cout<<endl;

}

}

int computeTriangleElement(int level,int index)

{

if(index==1||index==level)

return 1;

return computeTriangleElement(level-1,index-1)+computeTriangleElement(level-1,index);

}

int main()

{

int level;

cout<<"請輸入楊輝三角的高度:"<<endl;

cin>>level;

yanghuiTriangle(level);

return 0;

}

隊列輸出

#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

#define ERROR 0

#define OK 1

#define OVERFLOW -1

#define MAX_QUEUE 100

typedef int DataType;

typedef struct

{

DataType elem[MAX_QUEUE];

int front;

int rear;

}LinkQueue;

int InitQueue(LinkQueue *);

void EnQueue(LinkQueue *,DataType);

void DeQueue(LinkQueue *,DataType *);

void GetFront(LinkQueue,DataType *);

int QueueEmpty(LinkQueue);

void YangHuiTriangle(int );

int main()

{

int n=1;

printf("please enter a number: ");

scanf("%d",&n);

if(n<=0)

{

printf("ERROR!\n");

exit(0);

}

YangHuiTriangle(n);

return 0;

}

int InitQueue(LinkQueue *Q)

{

Q->front=Q->rear=-1;

return 1;

}

void EnQueue(LinkQueue *Q,DataType e)

{

if((Q->rear+1)%MAX_QUEUE==Q->front)

exit(OVERFLOW);

else

{

Q->rear=(Q->rear+1)%MAX_QUEUE;

Q->elem[Q->rear]=e;

}

}

void DeQueue(LinkQueue *Q,DataType *e)

{

if(QueueEmpty(*Q))

{

printf("queue is empty\n");

exit(0);

}

else

{

Q->front=(Q->front+1)%MAX_QUEUE;

*e=Q->elem[Q->front];

}

}

void GetFront(LinkQueue Q,DataType *e)

{

if(QueueEmpty(Q))

{

printf("queue is empty\n");

exit(0);

}

else

*e=Q.elem[(Q.front+1)%MAX_QUEUE];

}

int QueueEmpty(LinkQueue Q)

{

if(Q.front==Q.rear)

return 1;

else

return 0;

}

void YangHuiTriangle(int n)

{

LinkQueue Q;

int i,j,k,t,s,e;

InitQueue(&Q);

for(i=0;i<n;i++)

printf(" ");

printf(" 1\n");

EnQueue(&Q,1);

EnQueue(&Q,1);

for(i=1;i<n;i++)

{

for(k=0;k<n-i;k++)

printf(" ");

EnQueue(&Q,1);

for(j=0;j<i;j++)

{

DeQueue(&Q,&t);

printf(" %3d ",t);

GetFront(Q,&s);

e=t+s;

EnQueue(&Q,e);

}

EnQueue(&Q,1);

DeQueue(&Q,&t);

printf(" %d\n",t);

}

}

SQL輸出

--返回某一數的階乘

create function Ji (@count int) returns int

as

begin

declare @u int,@index int

set @u = 1 --初使值爲 1

set @index = 1

while(@index <= @count)

begin

set @u = @u * @index

set @index = @index + 1

end

return (@u)

end

create function Va(@num int,@count int) returns int

as

begin

declare @up int,@L1 int,@R1 int,@I int

set @up = dbo.Ji(@count)

set @L1 = dbo.Ji(@num)

set @R1 = dbo.Ji(@count - @num)

set @I = @up/(@L1 * @R1)

return (@I)

end

create function PrintRow(@num int) returns nvarchar(100)

as

begin

declare @i int

declare @str nvarchar(100)

set @str = '';

set @i = 1

while (@i < @num)

begin

set @str = @str + replace(str(dbo .Va(@i,@num)),' ','') + ' ,'

set @i = @i + 1

end

return (@str)

end

create proc PrintJiCeng(@num int)

as

begin

declare @i int

set @i = 1

while(@i <= @num )

begin

if (@i = 0 )

print 1 + ','

else if (@i = 1)

print '1,1,'

else

print '1,' + dbo.PrintRow(@i) + '1,'

set @i = @i + 1

end

end

--計算 10 以內的揚輝三角

exec PrintJiCeng 10

PHP輸出

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

php

$a[0][0] = 1;

$row = 20; //所要輸出的行號

for ($i = 0; $i < $row; $i++) {

for ($j = 0; $j <= $i; $j++) {

if($j == 0 || $i == $j){

print $a[$i][$j] = 1;

} else {

print $a[$i][$j] = $a[$i-1][$j-1] + $a[$i-1][$j];

}

print ' ';

}

print "\n";

}

?>

Java實現

代碼:

public class YhuiTest {

public static void main(String[] args) {

final int Row = 6;

int yh = new int[Row][Row];

for (int i = 0; i < Row; i++) {

yh[i][0] = 1;

yh[i][i] = 1;

}

for (int i = 2; i < Row; i++) {

for (int j = 1; j < Row; j++) {

yh[i][j] = yh[i - 1][j - 1] + yh[i - 1][j];

}

}

for (int i = 0; i < Row; i++) {

for (int j = 0; j <= i; j++) {

System.out.print(yh[i][j] + " ");

}

System.out.println();

}

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章