利用迴圈佇列實現楊輝三角的列印
#define MAXSIZE 100 #include <iostream> using namespace std; typedef int SElemType; typedef struct { SElemType *base; int front; int rear; }SqQue; void InitQue(SqQue &Q) { Q.base=new SElemType[MAXSIZE]; Q.front=Q.rear=0; } int CreatQue(SqQue &Q,int e) { if((Q.rear+1)%MAXSIZE==Q.front) return 0; Q.base[Q.rear]=e; Q.rear=(Q.rear+1)%MAXSIZE; return 1; } int OutQue(SqQue &Q,int &e) { if(Q.rear==Q.front) return 0; e=Q.base[Q.front]; Q.front=(Q.front+1)%MAXSIZE; return 1; } int main() { SqQue sq; int e=1,j=0,status=-1; printf("請輸入列印的行數\n"); scanf("%d",&j); InitQue(sq); for(int n=0;n<j;n++) { for(int m=0;m<j-1-n;m++) { printf(" "); } if(n==0) { cout<<"1"<<endl; CreatQue(sq,e); } else { int t1=0,t2=0; for(int r=0;r<n;r++) { OutQue(sq,e); t1=t2; t2=e; e=t1+t2; CreatQue(sq,e); cout<<e<<" "; } cout<<"1"<<endl; e=1; CreatQue(sq,e); } } return 1; }
這段程式碼直供給初學資料結構的小白們使用,有很多的不足與偷工減料的地方。沒有判斷是否隊空與隊滿,只是簡單地進行了操作。而且與別的博主寫的程式碼相比是在是LOW爆了,列印方法上也是很蠢得利用空格字元數來進行的列印,但是很好理解,希望大家都能寫出更優化的程式碼。
相關推薦
利用迴圈佇列實現楊輝三角的列印
#define MAXSIZE 100 #include <iostream> using namespace std; typedef int SElemType; typedef struct { SElemType *base; int front;
資料結構之用佇列實現楊輝三角
/************************************************************** > File Name: PascalTriangle.c > Author: chengfeiyan > Mail:
利用Python實現楊輝三角
利用Python實現楊輝三角 最近剛學python,邊學邊進行了一些小練習,以下是楊輝三角的實現思路和程式碼,希望大家給與支援,喜歡的給一個關注,我會繼續努力。(有問題發我郵箱[email protected],互相進步) 我們需要輸出的形式如下: 思路解析 看到這
C語言實現楊輝三角的列印
列印楊輝三角 這裡介紹兩種方法 第一種 第一眼看到楊輝三角很容易可以想到高中的數學知識–二項式定理(a+b)^n,當n從0開始,它的展開式每項的係數恰好對應楊輝三角的每一行 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5
資料結構學習之鏈佇列c++實現楊輝三角
#ifndef LINKQUEUE_H #define LINKQUEUE_H #include <iostream> #include <assert.h> using namespace std; template <class T>
python3 實現 楊輝三角
src 組合 楊輝三角 不同 tex 合數 三角形 itl enter 楊輝三角,是二項式系數在三角形中的一種幾何排列。在歐洲,這個表叫做帕斯卡三角形。帕斯卡(1623----1662)是在1654年發現這一規律的,比楊輝要遲393年,比賈憲遲600年 概述 前提:每行
JavaScript實現楊輝三角
數組實現 對齊 blog 個數字 入行 字符 空格 每一個 方法 楊輝三角的算法還是比較簡單易懂的,只是要想在控制臺更好的實現三角輸出就沒那麽簡單了。 根據目前所學,使用了二維數組實現楊輝三角。 每一行都是一個內層數組。 在輸出的時候再將每一個內層數組轉換成字符串來輸出,這
Java小題,通過JNI調用本地C++共享庫中的對應方法實現楊輝三角的繪制
question 文件夾 path ron variable iostream ring printf spl 1.在Eclipse中配置Javah,配置如下 位置是你javah.exe在你電腦磁盤上的路徑 位置:C:\Program Files\Java\jdk1.
python 實現楊輝三角(依舊遺留問題)
col 文件 分享 命令 image python span pan 文件名 1 #! usr/bin/env python3 2 #-*- coding :utf-8 -*- 3 print(‘楊輝三角的generator‘) 4 def triangles()
【Python】用生成器generator簡單實現楊輝三角
楊輝三角,又稱賈憲三角形,帕斯卡三角形,是二項式係數在三角形中的一種幾何排列。 def triangles(): L=[1] while(True): yield L L=[1]+[x+y for x,y in zip(L[:-1],L[1:])]+[1] n = 0 max=int(input
JAVA——實現楊輝三角的指定行數輸出
實現楊輝三角的指定行數輸出 1.要求 楊輝三角是一個由數字排列的三角形數表,此方法介紹如何實現控制檯輸出楊輝三角形。 2.楊輝三角 楊輝三角最本質的特徵是:除兩側元素均為1以外,其餘每個位置上的元素值為其正上方元素與左上角元素之和,用陣列來描述則為:
使用python生成器實現楊輝三角
今天學習了某位大神寫的實現楊輝三角的程式,貼出來和大家分享下: 楊輝三角定義如下: 1 / \ 1 1 / \ / \ 1 2 1 / \ / \ / \ 1 3 3 1
演算法:php實現楊輝三角
程式碼 // 階乘 function factorial($n){ return array_product(range(1,$n)); } // 排列數 function A($n,$m){ return factorial($n)/factor
鏈式表示的佇列——鏈式佇列2——楊輝三角問題
列印楊輝三角。楊輝三角是一個由數字排列成的三角形數表,一個8階的楊輝三角如下所示。
js中實現楊輝三角
實現效果:楊輝三角 即: 提示使用者輸入要實現的楊輝三角行數: 請輸入楊輝三角的行數: 8 程式碼實現後的效果如下: 1 1.1 1.2.1 1.3.3.1 1.4.6.4.1 1.5.10.10.5.1 1.6.15.20.15.6.1 1
二維陣列實現楊輝三角
參考連結:http://c.biancheng.net/view/495.html #include <stdio.h> #define N 20 int main(int argc, const char *argv[]) { int a[N][N] = {{0}
佇列輸出楊輝三角
#include "queue.h" int main() { int n = 8; QueuePtr Q = InitQueue(); //初始化 int s = 0, t; EnQueue(Q, 0);//入隊 EnQueue(Q,
程式基本演算法習題解析 編寫程式碼實現楊輝三角
先附上書上的程式碼(書上用c寫的,這裡轉換成了c++,但是思路沒變): #include "stdafx.h" #include<iostream> using namespace std; int main() { int a[11][20],i,j; for(i=
函式實現楊輝三角
def get_next_line(L): rl = [1] for i in range(len(L)-1): rl.append(L[i] + L[i + 1]) rl.append(1) return rl def get_yanhui_list(n):
HFUT資料結構作業感悟與分析-----C++運用佇列解決楊輝三角
//程式碼沒有很詳細的解釋,如果不明白,請自己根據程式碼一行一行分析 //分析三到四層,即可明白其中的原理 //再有問題的話,請在下面留言 #include<iostream> using namespace std; //我自己寫的Queue,是使用鏈佇列實