程序代码:
#include <stdio.h>
#define N 10
int yh(int i,int j) {
if (i==0 || j==0 || j == i)
return 1;
return yh(i-1,j-1)+yh(i-1,j);
}
int main() {
int i,j;
for (i=0;i<N;i++) {
for (j=0;j<=i;j++)
if (j == 0)
printf("%*d",(N-i)*2,yh(i,j));
else
printf("%4d",yh(i,j));
putchar('\n');
}
getchar();
}
不用数组?那样很慢耶……
[[italic] 本帖最后由 StarWing83 于 2007-12-20 17:59 编辑 [/italic]]
----------------解决方案--------------------------------------------------------
这是我写的代码
/* Note:Your choice is C IDE */
#include "stdio.h"
main()
{int a[10][10],i,j,k;
for(i=0;i<10;i++)
for(j=0;j<=i;j++)
{
if(i==j||j==0)
a[i][j]=1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
for(i=0;i<10;i++)
{ for(k=0;k<=10-i;k++)
printf(" ");
for(j=0;j<=i;j++)
printf("%6d",a[i][j]);
printf("\n");
}
}
呵呵,也是初学时写的,请大家多多指教,也是用二维数组写的代码.
----------------解决方案--------------------------------------------------------
呵呵,你也在看"数据结构"吗?刚好以前初学时也写了个,队列实现,又臭又长,勿笑.
/*////////////////Queue.h///////////////*/
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
typedef int ElemType;
typedef struct Queue
{
ElemType data[SIZE];
int front,rear;
}SeQueue;
void InitQueue(SeQueue **q)
{
*q=(SeQueue *) malloc (sizeof(SeQueue));
(*q)->front=0;
(*q)->rear=0;
}
int QueueEmpty(SeQueue *q)
{
return(q->front==q->rear);
}
int QueueFull(SeQueue *q)
{
return((q->rear+1)%SIZE==q->front);
}
void EnQueue(SeQueue *q,ElemType e)
{
if(QueueFull(q))
printf("Queue is Full!\n ");
else
{
q->data[q->rear]=e;
q->rear=(q->rear+1)%SIZE;
}
}
void DeQueue(SeQueue *q,ElemType &e)
{
if(QueueEmpty(q))
printf("Queue is Empty!\n");
else
{
e=q->data[q->front];
q->front=(q->front+1)%SIZE;
}
}
void GetTop(SeQueue *q,ElemType &e)
{
if(QueueEmpty(q))
printf("Queue is Empty!\n");
else
{
e=q->data[q->front];
}
}
void GetLen(SeQueue *q,int &l)
{
if(QueueEmpty(q))
printf("Queue is Empty!\n");
else
{
int tmp=q->front;
for(;tmp!=q->rear;tmp++)
{
l++;
}
}
}
void display(SeQueue *q)
{
if(QueueEmpty(q))
printf("Queue is Empty!\n");
else
{
int tmp=q->front;
printf("\n------------------\n");
for(;tmp!=q->rear-1;tmp++)
{
printf("%d ",q->data[tmp]);
}
printf("\n------------------\n");
}
}
/*////////////////yanghui.c///////////////*/
#include "Queue.h"
int main()
{
int s,e;
SeQueue *my;
InitQueue(&my);
EnQueue(my,0);
EnQueue(my,1);
EnQueue(my,0);
int n,i;
for(n=0;n<10;n++)
{
for(i=1;i<=10-n;i++)
printf("%2c",' ');
do{
DeQueue(my,s);
GetTop(my,e);
if(!e) printf("\n");
else printf("%3d ",e);
EnQueue(my,s+e);
}while(e!=0);
EnQueue(my,0);
}
display(my);
return 0;
}
[[italic] 本帖最后由 iaai315313 于 2007-12-21 14:04 编辑 [/italic]]
----------------解决方案--------------------------------------------------------
初学时写的,很不"规范",改了几遍,还是很多;
希望没有误导LZ
(数据结构C版-严蔚敏)[url]http://blog.jd128.cn/sjjg/text/chapter03/section4/index2.htm[/url]
[local]2[/local]
[[italic] 本帖最后由 iaai315313 于 2007-12-21 14:11 编辑 [/italic]]
----------------解决方案--------------------------------------------------------
提示: 作者被禁止或删除 内容自动屏蔽