當前位置:首頁 » 編程語言 » 楊輝三角c語言程序隊列
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

楊輝三角c語言程序隊列

發布時間: 2023-02-03 04:23:06

❶ 利用隊列列印出n行楊輝三角形.用c語言

其實根據原理來就可以了,程序在附件中,運行結果如下:

❷ 楊輝三角隊列的實現

#include <iostream>
#include <conio.h>
using namespace std;
#define TRUE 1
#define FALSE 0
#define Maxsize 200
typedef unsigned long int LINT;
typedef struct
{
LINT data[Maxsize];
int front,rear;
}Sequeue;
Sequeue *Sq,mysqueue;
void Set_queue_Null(Sequeue *sq); //將隊列置空隊列
int Empty_queue(Sequeue *sq); //判斷隊列是否為空
int In_queue(Sequeue *sq,LINT x); //隊中元素入隊
LINT Out_queue(Sequeue *sq); //隊中元素出隊
void Clear_screen(); //清屏函數
void main()
{
LINT n,i,j,temp1,temp2;

Sq=&mysqueue;
Next: //goto循環的開頭
Set_queue_Null(Sq);
cout<<"請輸入楊輝三角的階數:n=";
cin>>n;
In_queue(Sq,1);
cout<<endl;
for(i=1;i<=n;i++)
{
temp2=0;
for(j=1;j<i;j++)
{
temp1=Out_queue(Sq);
printf("%-5lu",temp1);
temp2+=temp1;
In_queue(Sq,temp2);
temp2=temp1;
}
Out_queue(Sq);
printf("1\n");
temp2++;
In_queue(Sq,temp2);
In_queue(Sq,1);

}
Clear_screen();
printf("你是否還要繼續進行\n Y(是) or N(否)?\n");
char response;
cin>>response;
if(response=='Y'||response=='y')
goto Next;
else{
cout<<"程序結束..."<<endl;
exit(0);
}
}

void Set_queue_Null(Sequeue *sq)
{
sq->front=Maxsize-1;
sq->rear=Maxsize-1;
}
int Empty_queue(Sequeue *sq)
{
if(sq->rear==sq->front)
return TRUE;
else
return FALSE;
}
int In_queue(Sequeue *sq,LINT x)
{
if(sq->front==(sq->rear+1)%Maxsize)
{
cout<<"隊列已滿!\n";
return FALSE;
}
else
{
sq->rear=(sq->rear+1)%Maxsize;
sq->data[sq->rear]=x;
return TRUE;
}
}

LINT Out_queue(Sequeue *sq)
{
if(Empty_queue(sq))
{
cout<<"隊列是空的!\n";
return FALSE;
}
else
{
sq->front=(sq->front+1)%Maxsize;
return (sq->data[sq->front]);
}
}
void Clear_screen(){
cout<<"是否清除屏幕?(Y/N)"<<endl;
if(getche()=='y'||getche()=='Y')
system("cls");
else
cout<<"繼續操作..."<<endl;
}

❸ 用c語言編寫程序 輸出楊輝三角

程序:

#include<stdio.h>

int main()

int n,i,j,a[100];

n=10;

printf(" 1");

printf(" ");

a[1]=a[2]=1;

printf("%3d%3d ",a[1],a[2]);

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

{

a[1]=a[i]=1;

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

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

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

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

printf(" ");

}

return 0;

}

應用

與楊輝三角聯系最緊密的是二項式乘方展開式的系數規律,即二項式定理。例如在楊輝三角中,第3行的三個數恰好對應著兩數和的平方的展開式的每一項的系數(性質 8),第4行的四個數恰好依次對應兩數和的立方的展開式的每一項的系數。

以上內容參考:網路-楊輝三角

❹ 採用隊列,編寫程序列印出楊輝三角形 數據結構(C語言版) 幫幫忙啊~~

#include <iostream>
#include<iomanip>
using namespace std;

int main()
{
int i,j;
int yang[10][10];
int n;

cout<<"Please input the value of the line(<=10)!" <<endl;
cin>>n;

if(n>10 || n<=0)
{
cout<<"Data error!"<<endl;
}

else
{
for(i=0;i<n;i++)
{
yang[i][i]=1;
yang[i][0]=1;
}
for(i=2;i<n;i++)
{
for(j=1;j<n;j++)
{
yang[i][j]=yang[i-1][j-1]+yang[i-1][j];
}
}

for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
cout<<setw(6)<<yang[i][j];
cout<<'\n';
}
}
}

return 0;
}

❺ c語言程序楊輝三角

#include <stdio.h>
main()
{ int i,j,n=0,a[17][17]={0};
while(n<1 || n>16)
{ printf("請輸入楊輝三角形的行數(大於0,小於17):");
scanf("%d",&n);
}
for(i=0;i<n;i++)
a[i][0]=1; /*第一列全置為一*/
for(i=1;i<n;i++)
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;j++)
printf("%5d",a[i][j]);
printf("\n");
}
}

//夠簡單了吧?

❻ c語言的楊輝三角程序

c語言的楊輝三角程序如下:

#include<stdio.h>

#include<stdlib.h>

intmain()

{

ints=1,h;//數值和高度

inti,j;//循環計數

scanf("%d",&h);//輸入層數

printf("1 ");//輸出第一個1

for(i=2;i<=h;s=1,i++)//行數i從2到層高

{

printf("1");//第一個1

for(j=1;j<=i-2;j++)//列位置j繞過第一個直接開始循環

//printf("%d",(s=(i-j)/j*s));

printf("%d",(s=(i-j)*s/j));

printf("1 ");//最後一個1,換行}

getchar();//暫停等待

return0;

}

(6)楊輝三角c語言程序隊列擴展閱讀:

楊輝三角概述

前提:每行端點與結尾的數為1.

  • 每個數等於它上方兩數之和。

  • 每行數字左右對稱,由1開始逐漸變大。

  • 第n行的數字有n項。

  • 第n行數字和為2n。

  • 第n行的m個數可表示為 C(n-1,m-1),即為從n-1個不同元素中取m-1個元素的組合數。

  • 第n行的第m個數和第n-m+1個數相等 ,為組合數性質之一。

  • 每個數字等於上一行的左右兩個數字之和。可用此性質寫出整個楊輝三角。即第n+1行的第i個數等於第n行的第i-1個數和第i個數之和,這也是組合數的性質之一。即 C(n+1,i)=C(n,i)+C(n,i-1)。

  • (a+b)n的展開式中的各項系數依次對應楊輝三角的第(n+1)行中的每一項。

  • 將第2n+1行第1個數,跟第2n+2行第3個數、第2n+3行第5個數……連成一線,這些數的和是第4n+1個斐波那契數;將第2n行第2個數(n>1),跟第2n-1行第4個數、第2n-2行第6個數……這些數之和是第4n-2個斐波那契數。

❼ 誰能告訴一下用循環隊列實現列印楊輝三角形的C語言代碼

#include <stdio.h>
#include <iostream.h>
#include "queue.h"
void YANGHUI(int n) {
SeqQueue q(n+2); //隊列初始化p121
q.EnQueue(1); q.EnQueue(1);
int s = 0, t;
for (int i = 1; i <= n; i++) { //逐行計算
cout << endl;
q.EnQueue(0);
for (int j = 1; j <= i+2; j++) { //下一行
q.DeQueue(t);
q.EnQueue(s + t);
s = t;
if (j != i+2) cout << s << ' ';
}
}
}
課件地址:http://wenku..com/view/cb953d5e804d2b160b4ec0ba.html
3.3.4 隊列的應用:列印楊輝三角形。

❽ 用C語言編寫5行`楊輝三角~~急````

#include "stdlib.h"

//聲明隊列類型
typedef struct node
{
int data[21];
int head,rear;
}sequeue;

int num;//楊輝三角的層數

//隊列的初始化
void initial(sequeue *sq)
{
sq->head=-1;
sq->rear=-1;
}

//進隊操作
void ensequeue (sequeue *sq,int data1,int data2)
{
sq->rear++;
sq->data[sq->rear]=data1+data2;
}

//出隊操作
int desequeue(sequeue *sq)
{
return sq->data[++sq->head];
}

//求揚輝三角的第n行,將其存入隊列sq2指向的結點中
sequeue * fun(int n,sequeue *sq1)
{
int data1,data2=0,i;
sequeue *sq2;

sq2=(sequeue *)malloc(sizeof(sequeue));
initial(sq2);

//輸出每行前的空格以形成三角格式
for (i=0;i<num-n;i++)
printf(" ");

//輸出存儲在隊列中的某行元素,並計算下一行數
while(sq1->head!=sq1->rear)
{
data1=desequeue(sq1);

//處理兩樹之間的間隔距離
if (data1<10) printf(" %d",data1);
else if (data1<100) printf(" %d",data1);
else if (data1<1000) printf(" %d",data1);
else printf(" %d",data1);

ensequeue(sq2,data1,data2);
data2=data1;
}
sq2->data[++sq2->rear]=1;
printf("\n");
free(sq1);
return sq2;
}

int main(int argc,char *argv[])
{
int i;
sequeue *sq=(sequeue *)malloc (sizeof(sequeue));
initial(sq);
sq->rear++;
sq->data[sq->rear]=1; //將第一行存入隊列中,第一行只有1一個元素

printf("please put in a integer number(no more then 15)!\n\t");
scanf("%d",&num);

//fun()函數實現輸出一行元素並計算出下一行各個元素,經過num次調用fun函數輸出num層楊輝三角
for (i=0;i<num;i++)
sq=fun(i,sq);

return 0;
}