| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1612 人关注过本帖
标题:用队列方式输出杨辉三角!程序改错,请求帮忙!谢谢了
只看楼主 加入收藏
jj369258
Rank: 4
等 级:业余侠客
帖 子:116
专家分:226
注 册:2010-12-2
结帖率:69.57%
收藏
已结贴  问题点数:5 回复次数:4 
用队列方式输出杨辉三角!程序改错,请求帮忙!谢谢了
#include<stdlib.h>
#include<stdio.h>
#include<iostream.h>
#include<malloc.h>

typedef struct Qnode
{
    int data;
    struct Qnode *next;
}Qtype;

typedef struct LinkQueue
{
    Qtype *front;
    Qtype *rear;
}LinkQueue;

int InitQueue(LinkQueue *Q)
{
    Qtype *p;
    p=(Qtype*)malloc(sizeof(Qtype));
    if(!p)
        return 0;
    p->next=NULL;
    Q->front=Q->rear=p;
    return 1;
}

int EnQueue(LinkQueue *Q,int e)
{
    Qtype *Newnode;
    Newnode=(Qtype*)malloc(sizeof(Qtype));
    if(!Newnode)
        return 0;
    Newnode->data=e;
    Newnode->next=Q->rear->next;
    Q->rear->next=Newnode;
    Q->rear=Newnode;
    return 1;
}
int Empty(LinkQueue *Q)
{
    return(Q->front==Q->rear?1:0);
}

int DeQueue(LinkQueue *Q,int *e)
{
    Qtype *p;
    if(Empty(Q))
        return 0;
    p=Q->front->next;
    *e=p->data;
    Q->front->next=p->next;
    if(Q->front->next==NULL)
        Q->rear=Q->front;
    free(p);
    return 1;
}
int Gethead(LinkQueue *Q,int *e)
{
    if(Empty(Q))
        return 0;
    *e=Q->front->next->data;
    return 1;
}

int main()//yanghuisanjiao
{
    int i,j,n,temp,e;
    LinkQueue Q;
    InitQueue(&Q);
    EnQueue(&Q,1);//第一行元素入队
    cin>>n;
    for(i=2;i<=n;i++)
    {
        EnQueue(&Q,1);//第n行的第一个元素入队
        for(j=0;j<=i-2;j++)
        {
            DeQueue(&Q,&temp);
            cout<<temp;//打印第n-1行的元素
            Gethead(Q,&e);
            temp=temp+e;//利用第n-1行的元素产生第n行的元素
            EnQueue(&Q,temp);
        }
        DeQueue(&Q,&e);
        cout<<e;//打印第n-1行的最后一个元素
        EnQueue(&Q,1);//最后一个元素入队
    }
    return 0;
}


搜索更多相关主题的帖子: 杨辉三角 include return 
2011-11-04 11:10
无名可用
Rank: 4
等 级:业余侠客
帖 子:79
专家分:259
注 册:2010-7-27
收藏
得分:5 
程序代码:
int main()//yanghuisanjiao
{
    int i,j,n,temp,e;
    LinkQueue Q;
    InitQueue(&Q);
    cin>>n;
    cout << "1" << endl;
    ::EnQueue( &Q, 1 );
    ::EnQueue( &Q, 2 );
    ::EnQueue( &Q, 1 );
    for(i=3;i<=n+1;i++)
    {
        EnQueue(&Q,1);//第n行的第一个元素入队
        for(j=1;j<=i-1;j++)
        {
            DeQueue(&Q,&temp);
            cout<<temp<<" ";//打印第n-1行的元素
            Gethead(&Q,&e);
            temp=temp+e;//利用第n-1行的元素产生第n行的元素
            EnQueue(&Q,temp);
        }
        DeQueue(&Q,&e);
        cout<< e << " ";//打印第n-1行的最后一个元素
        EnQueue(&Q,1);//最后一个元素入队
        cout << endl;
    }
    system( "pause" );
    return 0;
}
初始化的时候将杨辉三角整个第二行一起入队,因为第二行无法根据第一行递推得来
2011-11-04 15:14
jj369258
Rank: 4
等 级:业余侠客
帖 子:116
专家分:226
注 册:2010-12-2
收藏
得分:0 
谢谢
2011-11-08 18:55
leizisdu
Rank: 2
等 级:论坛游民
帖 子:22
专家分:24
注 册:2011-10-17
收藏
得分:0 
回复 楼主 jj369258
楼主,您的思路是对的,我把您的程序复制到VC6.0的新工程里运行了。只是main函数中内层for循环的j初始应该从1开始,到i-2结束,共i-2次,表示第i行中间的i-2项:
for (j=1; j<=i-2; ++j);
另外,为了能打印出第n行(最后一行),可以再单独加个for循环

[ 本帖最后由 leizisdu 于 2011-11-12 21:43 编辑 ]
2011-11-12 21:40
wjx294801971
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-11-15
收藏
得分:0 
学习,谢谢
2011-11-18 21:03
快速回复:用队列方式输出杨辉三角!程序改错,请求帮忙!谢谢了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022381 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved