用队列实现逐行处理(杨辉三角)

这个方法既节约了空间又节约了时间:

#include"iostream"
#include"queue"
#include"string.h"
#include"stdio.h"
#include"cmath"
using namespace std;
int  main()
{
    int i,j,s,t;
    queue<int>q;
    q.push(1);
    for(i=1;i<=10;i++)
    {
        s=0;q.push(0);
        for(j=0;j<i+1;j++)
        {
            t=q.front();
            q.pop();
            if(t)
            cout<<t<<‘ ‘;
            q.push(s+t);//输出上一行的同时将下一行进队列
            s=t;
        }
        cout<<endl;
    }
    return 0;
}

时间: 2024-08-25 13:17:27

用队列实现逐行处理(杨辉三角)的相关文章

数据结构上机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

Python 中使用 for、while 循环打印杨辉三角练习(列表索引练习)。

Python中使用for while循环打印杨辉三角练习(列表索引练习). 杨辉三角是一个由数字排列成的三角形数表,一般形式如下: 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组成的,而其余的数则是等于它肩上的两个数之和. 方法一: __author__ = 'Brad' n = int(input('请输入你想打印杨辉三角

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