Description
試利用循環隊列編寫k階斐波那契數列中前n+1項 (f(0),f(1),…,f(n))的程序,要求滿足: f(n)<=max而f(n+1)>max,其中max爲某個約定的常數。(注意:本題所用循環隊列的容量僅爲k,則在程序執行結束時,留在循環隊列中的元素應是所求k階斐波那契序列中的最後k項 f(n-k+1),…,f(n))。
Input
輸入常數max(0<max<10000),階數k(1<k<100),用空格隔開。
Output
輸出k階斐波那契數列中的最後k項f(n-k+1),…,f(n)。
- Sample Input
14 2
- Sample Output
8 13
#include<stdio.h>
int main(){
int arr[1000] = {0}, sum[1000] = {0};
int max, k, i, tmp;
scanf("%d%d",&max, &k);
arr[k] = 1;
sum[k] = 1;
for(i = k+1; ; i++){
arr[i] = sum[i-1];
sum[i] = sum[i-1] - arr[i-k] + arr[i];
if(arr[i] > max && arr[i-1] <=max){
tmp = i;
break;
}
}
int flag = 1;
for(i = tmp-k; i < tmp ;i++){
if(flag){
printf("%d", arr[i]);
flag = 0;
}
else{
printf(" %d", arr[i]);
}
}
printf("\n");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
typedef struct data{
int num;
struct data *next;
}data;
typedef struct queue{
data *head;
data *rear;
data *k_before;
}queue;
void init(queue *list,int k){
int i;
data *p = (data*) malloc (sizeof(data));
p->num = 0;
p->next = NULL;
list->head = p;
list->rear = p;
list->k_before = p;
for(i = 0; i < k-2; i++){
data *p = (data*) malloc (sizeof(data));
p->num = 0;
p->next = NULL;
list->rear->next = p;
list->rear = p;
}
for(i = 0; i < 2; i++){
data *p = (data*) malloc (sizeof(data));
p->num = 1;
p->next = NULL;
list->rear->next = p;
list->rear = p;
}
}
void pop(queue *list){
data *p;
p = list->k_before;
while(p->next != list->rear){
printf("%d ", p->num);
p = p->next;
}
printf("%d\n", p->num);
}
void push(queue *list, int z){
data *q;
q = (data*) malloc (sizeof(data));
/*p = list->head;
list->head = p->next;
free(p);*/
q->num = z;
q->next = NULL;
list->rear->next = q;
list->rear = q;
list->k_before = list->k_before->next;
}
int main(){
int max,k,x,y,z;
scanf("%d%d", &max, &k);
queue *list = (queue*) malloc (sizeof(queue));
init(list, k);
while(1){
x = list->rear->num;
y = list->k_before->num;
z = 2*x - y;
if(x > max){
pop(list);
break;
}
else{
push(list, z);
}
}
return 0;
}
複習:
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int num;
struct node *next;
}node;
typedef struct queue{
struct node *head;
struct node *rear;
struct node *k_before;
}queue;
void init(queue *list, int k){
node *p, *q;
int i;
p = (node*)malloc(sizeof(node));
p->num = 0;
p->next = NULL;
list->head = p;
list->rear = p;
list->k_before = p;
for(i = 0; i < k-2; i++){
q = (node*)malloc(sizeof(node));
q->num = 0;
q->next = NULL;
p->next = q;
q = p;
}
for(i = 0; i <2; i++){
q = (node*)malloc(sizeof(node));
q->num = 1;
q->next = NULL;
p->next = q;
p = q;
}
list->rear = p;
}
void push(queue *list, int x){
node *p;
p = (node*)malloc(sizeof(node));
p->num = x;
p->next = NULL;
list->rear->next = p;
list->rear = p;
list->k_before = list->k_before->next;
}
void pop(queue *list){
node *p;
p = list->k_before;
while(p->next != list->rear){
printf("%d ", p->num);
p = p->next;
}
printf("%d\n", p->num);
}
int main(){
int max, k, x, y, z;
scanf("%d%d", &max, &k);
queue *list = (queue*)malloc(sizeof(queue));
init(list, k);
while(1){
x = list->rear->num;
y = list->k_before->num;
z = 2*x - y;
if(x > max){
pop(list);
break;
}
else{
push(list, z);
}
}
return 0;
}