How to count Fibonacci

    xiaoxiao2021-03-25  58

    转发请注明原文:http://blog.csdn.net/qq_34911465/article/details/61646367

    The next is my solution to solve this problem.

    #include<iostream> using namespace std; char a[1000],b[1000]; int ai[1000],bi[1000],ci[1002]; //This is get the length of string int getlen(char *a) { int i=0; for(;;i++)if(a[i]=='\0')break; return i; } void add() { int lena = getlen(a); int lenb = getlen(b); //change string a into Integer ai for(int i = 0 ; i < lena ; i++) { ai[lena-i-1]=a[i]-'0'; } //change string b into Integer bi for(int i = 0 ; i < lenb ; i++) { bi[lenb-i-1]=b[i]-'0'; } //which is longer? string a or string b int lenc = (lena>lenb)?lena:lenb; //initiallize the Integer ci for(int i = 0 ; i < lenc ; ci[i]=0,i++); //add ai to bi and save the result in ci for(int i = 0 ; i < lenc ; i++) { ci[i]=ai[i]+bi[i]+ci[i]; if(ci[i]>=10) { ci[i+1]+=1; ci[i]-=10; } } //if the highest of ci is bigger than zeor if(ci[lenc]>0)lenc++; //move string b to string a for(int i = 0 ; i < lenb ; i++) { a[i]=b[i]; } a[lenb]='\0'; //move Integer ci to string b for(int i = 0 ; i < lenc ; i++) { b[i] = ci[lenc-i-1]+'0'; } b[lenc]='\0'; } int main(int args,char* argv[]) { a[0]='1';a[1]='\0'; b[0]='1';b[1]='\0'; int n; cout<<"please input the number:"; while(cin>>n) { n-=2; while(n>0) { add(); n--; } cout<<"result is: "<<b<<endl; cout<<"please input the next number:"; } cout<<"stop input the number"<<endl; return 0; }

    Below is my test:

    转载请注明原文地址: https://ju.6miu.com/read-36533.html

    最新回复(0)