1. 程式人生 > >用 佇列 列印 楊輝三角

用 佇列 列印 楊輝三角

//用佇列列印楊輝三角
//演算法:n=1 or 2 簡單輸出,n>=3-用佇列實現(把楊輝三角按行入隊,再出隊)
//利用佇列 FIFO性質
 
#include<stdio.h>
//範型 
typedef int ElementType;
//節點 
typedef struct {
ElementType data;
struct Node* next;
}Node;
//隊頭 
typedef struct {
  struct Node* front;
struct Node* rear; 
}Q;//queue 


//函式原型宣告
Q* queueBuild();
int isEmpty(Q* queue);
void enQueue(Q* queue,ElementType enter);
ElementType deQueue(Q* queue);


int main()
{
int row;
char ans='y';
while(ans=='y'||ans=='Y')
{
printf("please input the row of this pascal's triangle,row=");
scanf("%d",&row);
getchar();


if(1==row) printf("1\n");
else if(2==row) printf("1\n1   1\n");
else{
Q* queue1=queueBuild();
//一二行入隊 
enQueue(queue1,1); 
enQueue(queue1,1);
enQueue(queue1,1);
//3~n行入隊 
int i,j;
//p,q起始位置 
Node* p=queue1->front;p=p->next;
Node* q=p->next;
for(i=3;i<=row;i++)//第i行 
{    
   enQueue(queue1,1);//此行第一個數 入隊 
for(j=2;j<=i-1;)//2~倒二號數 入隊 
 {     
 enQueue(queue1,p->data+q->data);
 j++;//這也說明了for的靈活性不夠 
 if(j<=i-1)//沒這塊-p,q此後就報廢掉了 
 {
 p=p->next;
 q=q->next;
  }
 } 
enQueue(queue1,1);//最後一個數入隊 

p=q->next;
q=p->next;
}
 
//出隊
//演算法:第i行列印i個元素後,換行 
int count=1;
while(!isEmpty(queue1))
{
for(i=1;i<=count;i++)
{
printf("%5d",deQueue(queue1));
}printf("\n");
count++;
 } 
}//else


//繼續操作選項
A1:printf("do you wanna continue print the Pascal's Triangle,ans=");
   scanf("%c",&ans);
   getchar();


  if(ans=='n'||ans=='N') break;
  if(ans!='y'&& ans!='Y'&&ans!='n'&&ans!='N')
    {
  printf("input error!again!\n");
  goto A1;
    } 
}

getchar();
return 0;
 } 
 
 //函式原型
 //建隊/初始化 
 Q* queueBuild()
 {
 Q* p=(Q*)malloc(sizeof(Q));
if(p==NULL) exit(-1);

p->front=NULL;
p->rear=NULL;
return p; 
 } 
 
 int isEmpty(Q* queue)
 {
 return queue->front==NULL;
 }
 
 void enQueue(Q* queue,ElementType enter)
 {
 Node* p=(Node*)malloc(sizeof(Node));
 if(p==NULL) exit(-1);
 
 p->data=enter;
 p->next=NULL;
 //如果入隊的作為第一個節點-front變動 
if(isEmpty(queue))
{
  queue->front=p;
  queue->rear=p;
} else{
Node* record_rear=queue->rear;
record_rear->next=p;
queue->rear=p;
}
 }
 
 ElementType deQueue(Q* queue)
 {
 //如果是空隊,無法出隊
if(isEmpty(queue)) return NULL; 
 //如果只用一個節點fear-變動 
if(queue->front==queue->rear)
{  
 Node* record_front=queue->front;
 ElementType de=record_front->data; 
 //ElementType de=(queue->front)->data; 
 free(record_front);
 queue->front=NULL;
 queue->rear=NULL;
 return de;
}else{
Node* record_front=queue->front;//記錄第一個節點
ElementType de=record_front->data;//記錄出隊的值 
queue->front=record_front->next;
   free(record_front);
return de; 

//
 }

相關推薦

佇列 列印 三角

//用佇列列印楊輝三角//演算法:n=1 or 2 簡單輸出,n>=3-用佇列實現(把楊輝三角按行入隊,再出隊)//利用佇列 FIFO性質 #include<stdio.h>//範型 typedef int ElementType;//節點 typedef struct {ElementTy

【資料結構佇列的應用】佇列列印三角

數學中的楊輝三角大家都不陌生,那怎樣用程式的方式求n行的楊輝三角呢?方法很多,佇列就是其中的一種。下面給出基於佇列實現的楊輝三角。 # include<stdio.h> # define

迴圈佇列列印三角

#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<algorith

佇列解決三角問題

內容 相信大家都知道楊輝三角是什麼,就不過多介紹了,這篇部落格就是介紹一下,用佇列來解決楊輝三角問題,程式語言是C++,程式碼量不多,關鍵的程式碼就20行左右,思路也很簡單。 思路簡介 我們用一個迴圈,兩個佇列來計算楊輝三角,第一個佇

資料結構實驗八——佇列列印三角

#include <stdio.h> #include <stdlib.h> #define M 50 typedef struct SeqQueue { int element[M]; int front; int rear

資料結構之佇列實現三角

/************************************************************** > File Name: PascalTriangle.c > Author: chengfeiyan > Mail:

資料結構複習---------佇列列印三角

用佇列實現列印楊輝三角 問題介紹: 如果將二項式(a+b)^i(i=2,3,4……)展開,其係數排列成楊輝三角,如何實現各行係數的前n行打印出來,如下所示: 0 1

利用迴圈佇列列印三角(c語言實現)

#include<stdio.h> #include<malloc.h> #include<stdlib.h> #define MAXQSIZE 200 typedef int QElemType; typedef stru

c語言實現列印三角

用c語言實現列印楊輝三角 首先對楊輝三角進行分析 1、每個數等於它上方兩數之和。 2、每行數字左右對稱,由1開始逐漸變大。 3、第n行的數字有n項。 對於這種題目,一定要認真分析列印影象的規律再下手 首先是平面圖形就想到二維陣列 #define N 10 int arr

【資料結構】迴圈佇列的應用(一)列印三角

列印楊輝三角 楊輝三角是比較常見的佇列的應用,下面一行的數是上面2個數字的和,數列首位都是1,高中數學裡牛頓二項式展開式應該有說。 程式碼收穫 這題主要是找規律。利用下面一行比上面一行數字多一個,佇列頭為上一行,除了入隊首尾的1之外,入隊的上一行2個數的和

佇列應用之列印三角_legend

(1)圖解: (2)程式碼實現: #include <iostream> using namespace std; /* 列印楊輝三角; 楊輝三角如:       1     1   1   1   2   1 1   3   3   1 第n行有n個數,兩邊是

利用迴圈佇列實現三角列印

#define MAXSIZE 100 #include <iostream> using namespace std; typedef int SElemType; typedef struct { SElemType *base; int front;

資料結構:使用棧和佇列相關知識列印三角

本文利用資料結構佇列知識程式設計實現列印楊輝三角,原始碼如下: #include <stdio.h> #define MAXSIZE 50 #define N 10 typedef int QueueElementType; typedef

Python 2.7 實現列印三角

題目:打印出楊輝三角形(要求打印出10行) 以下列出的兩種解法涉及了python中兩種建立二維陣列的方法。 建立二維陣列的方法,單獨附文介紹。 解法一中涉及二維陣列的迴圈遍歷。 解法一: # encoding:utf-8 N = 10 YHTriangle = []

C:列印三角

//列印楊輝三角,n為行數 void printPascalTriangle(int n) { int size=n*2-1; int triangle[n][size]; for (int i=0; i<n; i++) { for (int

No.21 我與程式碼的日常:列印三角前10行

學習不易,需要堅持。 //列印楊輝三角前10行 #define N 10 #include <stdio.h> void Print() { int a[N][N] = {0} ; int i = 0 ; int j = 0 ; for(i=0; i<N;

5位運動員參加了10米臺跳水比賽+日本某地發生了一件謀殺案+在螢幕上列印三角

5位運動員參加了10米臺跳水比賽,有人讓他們預測比賽結果 A選手說:B第二,我第三; B選手說:我第二,E第四; C選手說:我第一,D第二; D選手說:C最後,我第三; E選手說:我第四,A第一; 比賽結束後,每位選手都說對了一半,請程式設計確定比賽的名次。 #include <s

鏈式表示的佇列——鏈式佇列2——三角問題

列印楊輝三角。楊輝三角是一個由數字排列成的三角形數表,一個8階的楊輝三角如下所示。                              

C語言列印三角程式碼及解析

楊輝三角是我們從初中就知道的,現在,讓我們用C語言將它在計算機上顯示出來。 在初中,我們就知道,楊輝三角的兩個腰邊的數都是1,其它位置的數都是上頂上兩個數之和。這就是我們用C語言寫楊輝三角的關鍵之一。在高中的時候我們又知道,楊輝三角的任意一行都是的二項式係數,n為行數減1。也就是說任何一個數等於這個是高中的

LeetCode刷題Easy篇列印三角(Pascal's Triangle)---動態規劃

題目 Example: Input: 5 Output: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 我的嘗試 我的程式碼因為leetcode缺少list介面的addAll方法,無法測試通過,我的思路是