江淮瑞鹰越野车怎么样:求c语言输出杨辉三角形的完整程序

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/28 03:13:16
求c语言输出杨辉三角形的完整程 最好对程序有说明的
hbqinlei 大哥 你那个程序我运行了 不是杨辉三角

你看看这个,这个是我编译运行通过的

#include <stdio.h>
int main()
{
int arr[2][11], n, i, j;

n=10;
for (i=0; i<=10; i++)
arr[0][i] = arr[1][i] = 0;
arr[0][1] = 1;
for (i=1; i<=n; i++)
{
for (j=1; j<=i; j++)
arr[i%2][j] = arr[(i-1)%2][j-1]+arr[(i-1)%2][j];
for (j=1; j<=i; j++)
printf("%-4d", arr[i%2][j]);
printf("\n");
}
printf("\n");
return 0;
}

运行结果如下:
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 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

这是用c++的
#include <stdio.h>
#include <assert.h>
#include <iostream.h>
class Queue;
class QueueNode
{
friend class Queue;
private:
int data;
QueueNode *link;
QueueNode (int d = 0, QueueNode *l = NULL):data(d), link(l){}
};
class Queue
{
public:
Queue():rear(NULL), front(NULL){}
~Queue();
void Enqueue(const int &item);
int DeQueue();
int GetFront();
void MakeEmpty();
int IsEmpty()const {return front == NULL;}
private:
QueueNode *front, *rear;
};
Queue::~Queue()
{
MakeEmpty();
}
void Queue::MakeEmpty()
{
QueueNode *p;
while(front)
{
p = front;
front = front->link;
delete p;
}
}
void Queue::Enqueue(const int &item)
{
if(!front)
front = rear = new QueueNode (item, NULL);
else
rear = rear->link = new QueueNode (item, NULL);
}
int Queue::DeQueue()
{
assert(!IsEmpty());
QueueNode *p = front;
int retvalue = p->data;
front = front->link;
delete p;
return retvalue;
}
int Queue::GetFront()
{
assert(!IsEmpty());
return front->data;
}
void YANGHUI(int n)
{
Queue q;
q.MakeEmpty();
q.Enqueue(1);
q.Enqueue(1);
int s = 0;
for(int i = 1; i <= n; i++)
{
q.Enqueue(0);
for(int j = 1; j <= i+2; j++)
{
int t = q.DeQueue();
q.Enqueue(s+t);
s = t;
if(j != i+2)
cout<<s<<" ";
}
cout<<endl;
}
}

int main(int argc, char* argv[])
{
YANGHUI(4);
return 0;
}

我知道