2)杨辉三角[1]队列实现

 1 #include<iostream>
 2 #include<iomanip>
 3 using namespace std;
 4
 5 enum error{overflow,underflow,success};
 6 const int maxlen=100;
 7
 8 class queue{
 9 public:
10     queue();
11     bool empty()const;
12     bool full()const;
13     int get_front(int &x)const;
14     error append(const int x);
15     error serve();
16 private:
17     int count;
18     int rear,front;
19     int data[maxlen];
20 };
21 queue::queue(){
22     count=0;
23     rear=front=0;
24 }
25
26 bool queue::empty()const{
27     if(count==0)return true;
28     return false;
29 }
30
31 bool queue::full()const{
32     if(count==maxlen)return true;
33     return false;
34 }
35
36 int queue::get_front(int &x)const{
37     if(empty())return underflow;
38     x=data[(front+1)%maxlen];
39     return success;
40 }
41 error queue::append(const int x){
42     if(full())return overflow;
43     rear=(rear+1)%maxlen;
44     data[rear]=x;
45     count++;
46     return success;
47 }
48
49 error queue::serve(){
50     if(empty())return underflow;
51     front=(front+1)%maxlen;
52     count--;
53     return success;
54 }
55
56 int main(){
57     queue q;
58     int n;
59     cin>>n;
60     int s1,s2;
61     for(int i=1;i<n;i++)cout<<"  ";
62     cout<<1<<endl;
63     q.append(1);
64     for(int i=2;i<=n;i++){
65         s1=0;
66         for(int k=1;k<=n-i;k++ )cout<<"  ";
67         for(int j=1;j<=i-1;j++){
68             q.get_front(s2);
69             q.serve();
70             cout<<s1+s2<<setw(4);
71             q.append(s1+s2);
72             s1=s2;
73         }
74         cout<<1<<endl;
75         q.append(1);
76     }
77     return 0;
78
79 }
时间: 2024-08-05 21:04:27

2)杨辉三角[1]队列实现的相关文章

数据结构上机4队列-杨辉三角2

#include <stdio.h> #include <malloc.h> #define OK 1 #define OVERFLOW -1 #define ERROR 0 typedef int Status, QElemType; //队列结构定义 typedef struct { QElemType *base; int front; int rear; } SqQueue; //初始化队列 Status InitQueue(SqQueue *Q, int Qsize) {

数据结构上机4队列-杨辉三角1

#include <stdio.h> #include <malloc.h> #define OK 1 #define OVERFLOW -1 #define ERROR 0 typedef int Status, QElemType; typedef struct { QElemType *base; int front; int rear; } SqQueue; Status InitQueue(SqQueue *Q, int Qsize) { Q->base = (QE

使用队列求解杨辉三角的第K层的所有元素

Java代码   package queue; import java.util.concurrent.ConcurrentLinkedDeque; /** * Created by Lanxiaowei * Craated on 2016/12/12 9:03 * 求解杨辉三角的第K层的所有元素 * 使用队列求解 */ public class YHTriangleWithQueue { public static void main(String[] args) throws Interru

杨辉三角——队列的应用

杨辉三角形是形如: 1 1   1 1   2   1 1   3   3   1 1   4   6   4   1 的三角形,其实质是二项式(a+b)的n次方展开后各项的系数排成的三角形,它的特点是左右两边全是1,从第二行起,中间的每一个数是上一行里相邻两个数之和. 使用<队列>的思想来实现杨辉三角的流程: 1>首先,需要初始化一个队列,即对头=队尾=0: 2>将第一行的元素1入队,接着操作第二行(一二行不需要求和操作,直接将元素入队即可): 3>从第三行开始,现在的对头

用队列打印杨辉三角

#include<iostream> #include<cstdio> #include<queue> #include<algorithm> using namespace std; queue<int> Q; int temp; void tringle(const int n) { Q.push(1); for(int i=2;i<=n;i++) { Q.push(1); for(int j=1;j<=i-2;j++) { te

数据结构总结系列(四)——循环队列之杨辉三角

今天我们来写一个循环队列的应用哦! 解决的是杨辉三角问题~~ 对于这样一个上下多层之间有密切联系的数据,如果只是用数组和循环来解决的话,显然会浪费大量的空间和时间, 所以我们用队列来解决这一问题: 之所以选用循环队列也是因为它对于空间的利用是非常有效的,方便我们的工作: 开始定义结构体: typedef struct //定义循环队列 { int data[MAXMIZE]; int Front; int Rear; }RollQueue; 这里的最大值(MAXMIZE)大家可以用宏定义来自己定

打印杨辉三角(STL版队列)

#include <iostream> #include <queue> using namespace std; void YangHuiTriangle(int n); int main() { cout<<"请输入杨辉三角的层数:"; int x; cin>>x; YangHuiTriangle(x); return 0; } void YangHuiTriangle(int n) { queue<int> q; q.p

LeetCode (13) Pascal&#39;s Triangle (杨辉三角 )

题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return 从第三行开始,每行除了最左边和最右边两个数为1,其他数字都是上一行中相邻两个数字之和.根据上述规则可以写出下面的代码: class Solution { public: vector<vector<int> > generateRow1() { vector<in

杨辉三角

1 package com.llh.demo; 2 3 /** 4 * 杨辉三角 5 * 6 * @author llh 7 * 8 */ 9 public class Test { 10 /* 11 * 杨辉三角 12 */ 13 public static void main(String[] args) { 14 int[] a = new int[11]; 15 int num = 1; 16 // 17 for (int i = 1; i <= 10; i++) { 18 for (i