C隊列 輸出楊輝三角

來源:互聯網
上載者:User

也是中軟筆試的演算法題,當時並不知道叫楊輝三角,唉。N年不用了,還得再拾起,為了那個夢。

#include <stdio.h>
void main()
{
    int a[50][50];
    int i,j,n;
    printf("Please input Number:");
    scanf("%d",&n);
    for (i=0;i<n;i++)
    {
        for (j=0;j<=i;j++)
        {
            if (j==0 ||j==i)
                a[i][j]=1;
            else
                a[i][j]=a[i-1][j-1]+a[i-1][j];
            printf("%5d",a[i][j]);
        }
        printf("\n");
    }
    getch();
}

/*TC 2.0測試*/
#define size 100
#define true 1
   #define false 0
    typedef int elemtype;
    typedef struct queue
     {
       elemtype element[size];
      int front;
       int rear;}queue;/*jie gou ti*/

    void initqueue(queue*q)
    {
       q->front=q->rear=0;}/*chu shi hua*/

    int enqueue(queue*q,int x)
    {
       if((q->rear+1)%size==q->front)
        return(false);
         q->element[q->rear]=x;
          q->rear=(q->rear+1)%size;
          return(true);    } /*chong xin she zhi wei zhi zhen*/

          int dequeue(queue*q,int*x)
          {
          if(q->front==q->rear)
          return(false);
          *x=q->element[q->front];
          q->front=(q->front+1)%size;
          return(true);}

          int queueempty(queue*q)
          {
          if(q->front==q->rear)
           return(false); }

        int getqueue(queue*q,int *e)
          {
          if(q->front==q->rear)
          return(false);
          else{*e=q->element[q->front];}
          }

    void main()
    {int n,i,k,e;
    int s;
    queue q;
    printf("please input N");
    scanf("%d",&n);
    for(i=1;i<=n;i++)

    printf(" ");printf("1");

    printf("\n");
    initqueue(&q);
    enqueue(&q,0);
    enqueue(&q,1);
    enqueue(&q,1);
    k=1;
    while(k<n)
    {
    for(i=1;i<=n-k;i++)
    printf(" ");
    enqueue(&q,0);
    do{
    dequeue(&q,&s);
    getqueue(&q,&e);
    if(e!=0)printf("%d ",e);
    else printf("\n");
    enqueue(&q,s+e);}
    while(e!=0);
    k++;}
    dequeue(&q,&e);
    while(!queueempty(&q))
    {dequeue(&q,&e);
    printf("%d ",e);
    clrscr();
    }
}

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.