宇宙規律#黃金分割#斐波那契數列【崔哥侃】 ... 費氏數列第零到n項和嘅證明 ... 第五十六章技術分析|教你如何描述艾略特波浪理論與費伯納希數列(李宜豐CFA & FRM 教室# ... ... <看更多>
費氏數列第100項 在 所以還是總歸一句 - Facebook 的推薦與評價
來遞迴個費氏數列的第100項吧. 3 yrs Report. Hsu Karinsu, profile picture. Hsu Karinsu replied · 9 replies. Hermes White, profile picture. Hermes White. ... <看更多>
費氏數列第100項 在 [問題] 費氏數列大數化- 看板C_and_CPP 的推薦與評價
問題是一開始不能輸入大值
如100之類的
但是只要先輸入小值
像是10(試過只能打到48)之類的
然後印出來
之後要打到1000都OK
輸出是輸出第N項費氏數列
我有去網上找資料
我的程式做出來的答案是對的
而測試程式碼的部分就不修掉了
希望高手們能幫我看看問題是出在哪
以下附上小弟程式碼
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct big_number{
char data[500];
};
int number,length_I,length_II;
char *replace_value,*replace_answer;
char answer[500],data[500];
/*data是倒轉時的預備字串*/
struct big_number fibonacci[1000];
char* add(struct big_number* value_a,int length_a,struct big_number*
value_b,int length_b);
char* get_strrev_number(struct big_number value,int length,char* out);
void fibonacci_number(int number);
void read_in(struct big_number* target,char *value,int length);
int get_length(struct big_number data,int length);
int main(){
char one[5]={'1'},enter;
sprintf(fibonacci[0].data,"%s",one);
sprintf(fibonacci[1].data,"%s",one);
//fibonacci_number(1000);
while(~scanf("%d%c",&number,&enter)){
/*第一次無法打大數字.... 有待解決*/
fibonacci_number(number);
printf("fibonacci(%d):%s\n",number,fibonacci[number-1].data);
}
return 0;
}
int add(struct big_number value_a,int length_a,struct big_number value_b,int
length_b){
memset(answer,0,sizeof(answer));
char *value_I,*value_II;
value_I=(char*)malloc(sizeof(value_I));
value_II=(char*)malloc(sizeof(value_II));
/*沒開空間給指標的話 無法使用*/
int i,max_length=(length_a>=length_b)?length_a:length_b,count=0;
//printf("value_a:%s length_a:%d\nvalue_b:%s
length_b:%d\n",value_a.data,length_a,value_b.data,length_b);
//printf("max_length:%d\n",max_length);
value_I=get_strrev_number(value_a,length_a,value_I);
value_II=get_strrev_number(value_b,length_b,value_II);
//printf("value_I:%s\nvalue_II:%s\n",value_I,value_II);
for(i=0;i<=max_length;++i){
//printf("add max_number:%d\n",max_length);
answer[i]+=(answer[i]==0)?48:0;
value_I[i]=((value_I[i]==0)?48:value_I[i]);
value_II[i]=((value_II[i]==0)?48:value_II[i]);
answer[i]+=value_I[i]+value_II[i]-96;
//printf("before ADD%s---%d\n",answer,answer[i]);
if(answer[i]>=58){
//printf("進位前%c\n",answer[i+1]);
answer[i+1]+=(answer[i+1]>48?1:49);
answer[i]-=10;
//printf("%c進位%c\n",answer[i],answer[i+1]);
count++;
}
//printf("%c",answer[i]);
}
//printf("\nadd over\n");
free(value_I);free(value_II);
for(i=0;answer[i]!='\0';++i){
}
/*算answer的長度*/
max_length=i-1;
return max_length;
/*answer陣列為全域陣列 不用傳出*/
}
char* get_strrev_number(struct big_number value,int length,char *out){
/*將倒轉值輸出*/
memset(data,0,sizeof(data));
int i,number=length;
//printf("in function get_strrev_number:\nlength:%d value:",length);
for(i=0;i<=length+1;++i){
/*\0也要傳過來啊 所以到length+1*/
data[i]=value.data[number];
//printf("%c",value.data[number]);
number--;
}
//printf("\n");
/*for(i=0;i<=length/2;i++){
if(data[i]>=48&&data[i]<=57){
data[i]=data[number];
number--;
}
}*/
//printf("function finish:%s\n",data);
sprintf(out,data);
//printf("function out:%s\n",out);
//printf("-----get_strrev_number over------\n");
return out;
}
int get_length(struct big_number data,int length){
int i;
//printf("in get_length:%s\n",data.data);
for(i=0;i<=999;++i){
if(data.data[i]<=47||data.data[i]>=58){
break;
}
}
length=i-1;
//printf("length:%d\n------get_length over-------\n",length);
return length;
}
void fibonacci_number(int number){
//printf("now number:%d in[%d]\n",number,number-1);
if(fibonacci[number-1].data[0]!=0||number==1||number==2){
//printf("check:fibonacci[%d]:%s ",number-1,fibonacci[number-1].data);
//printf("stop:%d\n",stop);
//printf("out_data:%s\n",fibonacci[number-1].data);
//printf("_____________________\n");
return ;
}
else{
int length_replace;
if(fibonacci[number-2].data[0]==0||fibonacci[number-1].data[0]==0){
//printf("RE number=%d=>%d find %d
%d\n",number,number-1,number-2,number-3);
fibonacci_number(number-2);
//printf("1.number:%d fuction(%d)fibonacci[%d]:%s\n",
number,number-2,number-3,fibonacci[number-3].data);
fibonacci_number(number-1);
//printf("2.number:%dfuction(%d)fibonacci[%d]:%s\n",
number,number-1,number-2,fibonacci[number-2].data);
}
//printf("ADD %d=[%d]%s [%d]%s\n",
number,number-2,fibonacci[number-2].data,number-3,
fibonacci[number-3].data);
length_I=get_length(fibonacci[number-2],length_I);
length_II=get_length(fibonacci[number-3],length_II);
length_replace=add(fibonacci[number-3],length_II
,fibonacci[number-2],length_I);
read_in(&fibonacci[number-1],answer,length_replace);
//printf("length_replace:%d\n",length_replace);
//printf("----------------else over------------\n");
}
}
void read_in(struct big_number* target,char *value,int length){
/*target要傳記憶體位置*/
/*在這裡把倒過來的answer轉回來*/
int i,j=0;
//printf("doing\n");
for(i=length;i>=0;--i){
target->data[j]=value[i];
//printf("%c",value[i]);
j++;
}
//printf("\ntarget\n");
for(i=0;i<length+1;++i){
//printf("%c",target->data[i]);
}
//printf("\n");
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.143.154.146
... <看更多>