列印紐曼-康威序列 n 個項
紐曼-康威序列用於生成以下整數序列。
1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 10 11 12
用於生成 n 個數字的紐曼-康威序列的公式為 −
P(n) = P(P(n - 1)) + P(n - P(n - 1)) Where, p(1) =p(2) =1
演算法
START Step 1 -> Input variable n(e.g. 20) Step 2 -> start variables as i, p[n+1], p[1]=1, p[2]=1 Step 3 -> Loop For i=3 and i<=n and i++ Set p[i] = p[p[i - 1]] + p[i - p[i - 1]] Print p[i] Step 4 -> End Loop For STOP
示例
#include<stdio.h>
int main() {
int n = 20,i;
int p[n + 1];
p[1] = 1;
p[2] = 1;
printf("Newman-Conway Sequence is :");
printf("%d %d ",p[1],p[2]);
for (i = 3; i <= n; i++) {
p[i] = p[p[i - 1]] + p[i - p[i - 1]];
printf("%d ",p[i]);
}
return 0;
}輸出
如果執行以上程式,則將生成以下輸出。
Newman-Conway Sequence is :1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 10 11 12
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP