队列写杨辉三角(拿两个队列写的。。。。
来源:互联网 发布:dota2 a卡优化 编辑:程序博客网 时间:2024/05/30 23:45
参考代码1:(用两个队列写的)
package queue;public class queue { public int msize; public int front; public int rear; public int [] queue; public queue(int size) { msize=size; queue=new int[msize]; front=0; rear=0; } public queue() { } public boolean isEmpty() { if(front==rear){ return true; } else { return false; } } public boolean isFull() { if((rear+1)%msize==front) { return true; } else { return false; } } public void enQueue(int elem) { if((rear+1)%msize!=front) { queue[rear]=elem; rear=(rear+1)%msize; } else { System.out.println("队列已满"); } } public void deQueue() { if(front!=rear) { // int elem=queue[front]; queue[front]=0; front=(front+1)%msize; } else { System.out.println("队列是空的"); // return 0; } } public void PrintQueue() { int i=front+1; while(i!=rear) { System.out.print(queue[i]+" "); i=(i+1)%msize; } System.out.println(); }}
package queue;public class yanghui { public static void main(String [] args) { int n=6; queue q1=new queue(3); queue q2=new queue(4); q1.enQueue(0);q1.enQueue(1);q1.enQueue(0); for(int i=0;i<n;i++) { q2.enQueue(0); while(q1.front!=q1.rear) { q2.enQueue(q1.queue[q1.front]+q1.queue[q1.front+1]); q1.deQueue(); } for(int j=0;j<n-i;j++)System.out.print(" "); q2.PrintQueue(); q1=new queue(i+4); q1=q2; q2=new queue(i+5); } }}
输出结果:
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
0 0
- 队列写杨辉三角(拿两个队列写的。。。。
- 【数据结构队列的应用】用队列打印杨辉三角
- 队列实现 杨辉三角的打印
- 杨辉三角的队列实现
- 队列的建立、杨辉三角
- 队列--杨辉三角
- 以前写的 C# 队列
- 我写的循环队列
- 利用两个队列模仿队列
- 栈与队列的实现__ 两个栈实现队列 vs 两个队列实现栈
- 栈与队列的实现__ 两个栈实现队列 vs 两个队列实现栈
- 杨辉三角(队列)
- 队列实现杨辉三角
- 杨辉三角队列方式
- 实现了两个普通的队列,一个优先权队列
- 栈和队列 由两个栈组成的队列
- 利用队列实现杨辉三角的打印
- 队列的简单应用-杨辉三角和约瑟夫环
- Path Sum I II III
- iOS 10 新特性之openURL
- Kotlin(二)-->基础语法
- Ubuntu下使用SSH和FileZilla远程登陆
- 华为,计算给定时间的下一秒,java
- 队列写杨辉三角(拿两个队列写的。。。。
- MySql5.7.14存储表情字符报错Incorrect string value
- oracle创建与使用视图
- Java Web 学习路线
- EditText的问题
- android developer tiny share-20161017
- 使用Smarty之变量信息的使用
- 如何安装windows和ubuntu双系统
- tomcat相关帖子