將二項式 ( a + b )i展開,其系數構成如圖1所示的楊輝三角形,也即Pascal's trangle。想不到吧,楊輝三角形還有這種意義呢,數學裡面的知識之間的關系真是千絲萬縷啊。
1 1 i=1
1 2 1 2
1 3 3 1 3
1 4 6 4 1 4
1 5 10 10 5 1 5
1 6 15 20 15 6 1 6
圖1
現在要求將展開式系數打印出來。 規定:本題必須采用“隊列”這種數據結構來解決。
Input有多個測試用例,每個測試用例占一行。
每行是一個整數 i, 1 ≤ i ≤ 30 。表示該二項式的冪。
Output對每個測試用例輸出一行,從左到右輸出該二項展開式的各項的系數,各數6
2
1 6 15 20 15 6 1
1 2 1
John
就不貼完整代碼了,寫一下思路,畢竟一次AC。
剛開始看到這題,第一印象就是樹的層序遍歷,就開始ctrl+r mspaint 開始模擬過程
總的來說還是挺簡單的。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
//#include <dos.h>
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct Node *next;
}Node;
typedef struct Queue
{
Node * front;
Node * rear;
int length;
}Queue;
Queue * queue_new();
bool IsEmpty(Queue *q);
bool EnQueue(Queue *q, ElemType e);
bool DeQueue(Queue *q, ElemType *e);
void BlackBox(int n);
int main()
{
int n;
while (scanf("%d", &n) != EOF)
BlackBox(n);
//system("pause");
return 0;
}
/********************************************************
* 算法:隊列中開始有兩個'1',循環i=1 to n-1次{ *
* 循環j=1 to i次{ *
* 1.出隊, *
* 2.得到的元素與隊頭元素相加再將結果入隊 } *
* 3.入隊'1'} *
********************************************************/