班级:信管1101 姓名:袁志祥 学号:201100832
package 杨辉三角;
import java.util.Scanner;
public class yhsj {
public static void main(String args[]) {
Scanner in = new Scanner(System.in); // 获取控制台输入对象
SeqQueue s = new SeqQueue();
s.enqueue(0);
s.enqueue(1);
System.out.print("请输入行号: ");
int n = in.nextInt();
int i = 0;
while (i <= n) {
int x = s.dequeue();
int y = s.frontqueue();
if (x == 0) {
i++;
s.enqueue(0);
}
s.enqueue(x + y);
if (x == 0) {
System.out.println();
for (int j = 0; j < 2 * (n - i); j++)
System.out.print(" ");
} else
System.out.print(x + " ");
}
}
}
interface QQueue {
boolean isEmpty();
void enqueue(T x);
T dequeue();
}
class SeqQueue implements QQueue {
private Object element[];
private int front, rear;
public SeqQueue(int length) {
if (length < 64)
length = 64;
this.element = new Object[Math.abs(length)];
this.front = this.rear = 0;
}
public SeqQueue() {
this(64); }
public boolean isEmpty() {
return this.front == this.rear;
}
public void enqueue(T x) {
if (x == null)
return;
if (this.front == (this.rear + 1) % this.element.length) {
Object[] temp = this.element;
this.element = new Object[temp.length * 2];
int i = this.front, j = 0;
while (i != this.rear) {
this.element[j] = temp[i];
i = (i + 1) % temp.length;
j++;
}
this.front = 0; this.rear = j; }
this.element[this.rear] = x;
this.rear = (this.rear + 1) % this.element.length;
}
public T frontqueue() {
if (isEmpty())
return null;
T temp = (T) this.element[this.front];
return temp;}
public T dequeue() {
if (isEmpty())
return null;
T temp = (T) this.element[this.front];
this.front = (this.front + 1) % this.element.length;
return temp; }
}
运行结果:
本文档为【java用队列实现杨辉三角123】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。