題目來源: https://www.google.com.tw/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&cad=rja&uact=8&ved=0CCsQFjACahUKEwjIovKBteXIAhXKG5QKHQRBDyE&url=https%3A%2F%2Fuva.onlinejudge.org%2Findex.php%3Foption%3Dcom_onlinejudge%26Itemid%3D8%26page%3Dshow_problem%26problem%3D436&usg=AFQjCNHrnRFwsT4S7PfjJC0SMNVOhCniLg&sig2=4cde6Qx6bZLXPk83HzGT-A
題意:大數的費式數列,用C寫
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<cstdio> | |
#include<cstring> | |
#include<cstdlib> | |
#include<string> | |
#include<sstream> | |
#include<fstream> | |
using namespace std; | |
typedef long long int L_int; | |
L_int p1[100],p2[100],temp[100]; | |
string s[5001] = {""}; | |
char h[100]; | |
int main(){ | |
int tag1,n=5000; | |
s[0] = "0"; | |
s[1] = "1"; | |
s[2] = "1"; | |
ofstream fout("005.txt"); | |
memset(p1,0,sizeof(p1)); | |
memset(p2,0,sizeof(p2)); | |
memset(temp,0,sizeof(temp)); | |
tag1 = 1; | |
p1[0] = 1; | |
p2[0] = 1; | |
for(int i=0;i<n-2;i++){ | |
memcpy(temp,p1,sizeof(p1)); | |
for(int j=0;j<tag1;j++){ | |
p1[j]=p1[j]+p2[j]; | |
p1[j+1] += p1[j]/1000000000000; | |
p1[j]%=1000000000000; | |
} | |
memcpy(p2,temp,sizeof(temp)); | |
if(p1[tag1]) tag1++; | |
for(int m=tag1-1;m>=0;m--){ | |
sprintf(h,"%lld",p1[m]); | |
if(strlen(h)<12&&m!=tag1-1) | |
for(int ii = 0;ii<12-strlen(h);ii++) | |
s[i+3]+="0"; | |
s[i+3]+=h; | |
} | |
} | |
//for(n = 0;n<=5000;n++){ | |
while(scanf("%d",&n)==1){ | |
printf("The Fibonacci number for %d is ",n); | |
cout<<s[n]; | |
cout<<"\n"; | |
} | |
} |
#想睡了~~~期待真愛營 =)
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。