PichuChen 10 發表於 May 19, 2007 檢舉 Share 發表於 May 19, 2007 這題..本來是考試題...沒過就算了..可是莫名其妙的變成了我的"回家作業"於是,我就吐了一個早上的血...現在還沒吐完(大概還有10%吧)由於實在太吐血了...所以就拿上來讓大家一起吐4.有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個數列的前20項之和。等等在上來對答案http://www.wretch.cc/blog/lnylonl&article_id=13129745 鏈接文章 分享到其他網站
tale_lalala 10 發表於 May 19, 2007 檢舉 Share 發表於 May 19, 2007 #include<iostream>#include<cstdlib>using namespace std;int f(int n);int main(){ int count; double sum=0; for(count=1;count<=20;count++){ sum+=((double)f(count+2)/f(count+1)); } cout<<sum<<endl; system("pause");}int f(int n){ if(n<=0) return 0; else if(n<=2) return 1; else return f(n-1)+f(n-2); }寫的不好~見諒 鏈接文章 分享到其他網站
PichuChen 10 發表於 May 19, 2007 作者 檢舉 Share 發表於 May 19, 2007 我程式碼打好了....但是...跑的速度真是令人絕望..//for dev c#include <cstdlib>#include <iostream>#define debug 0using namespace std;inline void big_move(const char*,char*,int);void big_red(char*,char*,int);void big_xmu(char*,char*,int,int,int);void big_gcd(const char*,const char*,char*,int);void big_add(const char*,const char*,char*,int);void big_sub(const char*,const char*,char*,int);void big_mup(const char*,int,char*,int);void big_div(const char*,const char*,char*,int);void big_mod(const char*,const char*,char*,int);int big_cmp(const char*,const char*,int);void big_zero(char*,int);void big_show(const char*,int);void big_cut(char*,int);inline int pow(int,int);int f(int);#define lh 100int main(int argc, char *argv[]){ char a[lh],b[lh]; big_zero(a,lh); big_zero(b,lh); big_cut(a,f(3)); big_cut(b,f(2)); for(int i = 1;i<20;i++){ printf("\n%d",i); big_xmu(a,b,f(i+3),f(i+2),lh); big_red(a,b,lh); } system("cls"); big_show(a,lh); printf("/"); big_show(b,lh); system("PAUSE"); return EXIT_SUCCESS;}inline void big_move(const char *b,char *c,int n){ for(int i=1;i<=n;i++) *(c+n-i) = *(b+n-i);}void big_red(char*a,char*b,int n){ char *t=(char*)malloc(sizeof(char)*n); big_zero(t,n); big_gcd(a,b,t,n); big_div(a,t,a,n); big_div(b,t,b,n); free(t);}void big_xmu(char*a,char*b,int c,int d,int n){ char *t=(char*)malloc(sizeof(char)*n); big_mup(a,d,a,n); big_mup(b,c,t,n); big_add(a,t,a,n); big_mup(b,d,b,n); free(t);}void big_gcd(const char*a,const char*b,char*c,int n){ char*x=(char*)malloc(sizeof(char)*n); char*y=(char*)malloc(sizeof(char)*n); char*z=(char*)malloc(sizeof(char)*n); char*zero=(char*)malloc(sizeof(char)*n); big_zero(zero,n); big_move(a,x,n); big_move(b,y,n); big_mod(x,y,z,n); while(big_cmp(zero,z,n)!=0){ big_move(y,x,n); big_move(z,y,n); big_mod(x,y,z,n); } big_zero(c,n); big_move(y,c,n); free(x); free(y); free(z); free(zero);}void big_add(const char*a,const char*b,char*c,int n){ char *t=(char*)malloc(sizeof(char)*n); big_zero(t,n); for(int i=1;i<=n;i++){ #if (debug==1) if (i==1){ if(*(a+n-i)+*(b+n-i)+*(t+n-i) > 9){printf("溢位!!!");return;} else {*(t+n-i) += *(a+n-i)+*(b+n-i);} }else{ #endif if(*(a+n-i)+*(b+n-i)+*(t+n-i) > 9){*(t+n-i+1) += 1;*(t+n-i) += *(a+n-i)+*(b+n-i)-10;}//進位 else{*(t+n-i) += *(a+n-i)+*(b+n-i);} #if (debug==1) } #endif } big_zero(c,n); big_move(t,c,n); free(t);}void big_sub(const char*a,const char*b,char *c,int n){ char *t=(char*)malloc(sizeof(char)*n); big_zero(t,n); for(int i=0;i<n;i++){ #if (debug==1) if (i==n-1){ if(*(a+i)-*(b+i)+*(t+i) < 0){printf("溢位(不支援負數)!!!");return;} else {*(t+i) += *(a+i)-*(b+i);} }else{ #endif if(*(a+i)-*(b+i)+*(t+i) < 0){*(t+i+1) -= 1;*(t+i) += *(a+i)-*(b+i)+10;}//借位 else{*(t+i) += *(a+i)-*(b+i);} #if (debug==1) } #endif } big_zero(c,n); big_move(t,c,n); free(t);}void big_mup(const char*a,int b,char*c,int n){ char *t=(char*)malloc(sizeof(char)*n); big_zero(t,n); for(int i=0;i<b;i++){ big_add(t,a,t,n); #if (debug==1) big_show(a,n); #endif } if (b == 0) big_zero(t,n); big_zero(c,n); big_move(t,c,n); free(t);}void big_div(const char*a,const char*b,char*c,int n){ char *t=(char*)malloc(sizeof(char)*n); char *k=(char*)malloc(sizeof(char)*n); char *o=(char*)malloc(sizeof(char)*n); big_zero(k,n); big_zero(o,n); *o=1; big_move(a,t,n); while((big_cmp(t,b,n)) == -1){ big_sub(t,b,t,n); big_add(k,o,k,n); #if (debug==1) cout<<endl; big_show(k,n); #endif } if (big_cmp(t,b,n) == 0) big_add(k,o,k,n); big_zero(c,n); big_move(k,c,n); free(k); free(t);}void big_mod(const char*a,const char *b,char*c,int n){ char *t=(char*)malloc(sizeof(char)*n); big_move(a,t,n); while((big_cmp(t,b,n)) == -1){ big_sub(t,b,t,n); } if (big_cmp(t,b,n) == 0) big_zero(t,n); big_zero(c,n); big_move(t,c,n); free(t);}int big_cmp(const char *a,const char *b,int n){//-1-->a>b 0-->a=b 1-->a<b while(n>0){ if (*(a+n-1)>*(b+n-1)) return -1; if (*(a+n-1)<*(b+n-1)) return 1; n--; } return 0;}void big_zero(char*a,int n){ for(int i=1;i<=n;i++){ *(a+n-i)=0; }}void big_show(const char*a,int n){ int k=0; for(int i=1;i<=n;i++){ #if (debug==0) if(*(a+n-i) != 0 ) k=1; if (k==1) printf("%d",*(a+n-i)); #else printf("%d ",*(a+n-i)); #endif } #if (debug==0) if (k==0) printf("0"); #endif}void big_cut(char*a,int input){ int n=0,r=input; while(r!=0){ r /=10; n++; } #if (debug==1) int k=n; printf("n=%d\n",n); #endif for (n;n>=0;n--){ *(a+n-1) = input/pow(10,n-1); input -= (input/pow(10,n-1))*pow(10,n-1); } #if (debug==1) big_show(a,k); #endif}inline int pow(int x,int n){ if (n==0) return 1; for (n;n>1;n--) x *=x; return x;}int f(int n){ if (n==0)return 0; if (n==1)return 1; if (n==2)return 1; if (n==3)return 2; return f(n-1)+f(n-2);} 鏈接文章 分享到其他網站
SRX 10 發表於 May 19, 2007 檢舉 Share 發表於 May 19, 2007 我程式碼打好了....但是...跑的速度真是令人絕望..第一,這題不用大數,用long long即可。第二,你的fibonacii數列用函式慢慢遞迴當然會慢。 鏈接文章 分享到其他網站
智障。就是我。 10 發表於 June 5, 2007 檢舉 Share 發表於 June 5, 2007 同意應該只要設變數多一點用for加用if處理通分問題應該就可以了吧? 鏈接文章 分享到其他網站
Recommended Posts
請登入後來留意見
在登入之後,您才能留意見
立即登入