hdu 1316 斐波那契数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1316

意思很简单,就是让你计算在两个数之间有多少个斐波那契数,很挫的时我一开始把string的比较给搞错了,一直出不来结果。。。orz,还是直接上代码了。。

View Code
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 const int N=1000;
 5 using namespace std;
 6 string f[N];
 7 int num[N];
 8 
 9 void solve(){
10     f[0]="1";
11     f[1]="2";
12     for(int i=2;i<N;i++){
13         memset(num,0,sizeof(num));
14         int len1=f[i-1].size();
15         int len2=f[i-2].size();
16         int j,k,l=0,temp=0;
17         for(j=len1-1,k=len2-1;j>=0&&k>=0;j--,k--){
18             temp+=f[i-1][j]+f[i-2][k]-96;
19             num[l++]=temp%10;
20             temp/=10;
21             num[l]=temp;
22         }
23         while(j>=0){
24             temp+=f[i-1][j--]-48;
25             num[l++]=temp%10;
26             temp/=10;
27             num[l]=temp;
28         }
29         while(k>=0){
30             temp+=f[i-2][k--]-48;
31             num[l++]=temp%10;
32             temp/=10;
33             num[l]=temp;
34         }
35         while(num[l]==0)l--;
36         while(l>=0){
37             f[i]+=num[l--]+48;
38         }
39     }
40 }
41 
42 int cmp(string &s1,string &s2,string &s){
43     int len1=s1.size();
44     int len2=s2.size();
45     int len=s.size();
46     if(len<len1||len>len2){
47         return 0;
48     }else if(len==len1&&len<len2){
49         if(s>=s1)return 1;
50         else return 0;
51     }else if(len==len1&&len==len2){
52         if(s>=s1&&s<=s2)
53             return 1;
54         else 
55             return 0;
56     }else if(len>len1&&len<len2){
57         return 1;
58     }else if(len>len1&&len==len2){
59         if(s<=s2)return 1;
60         else return 0;
61     }
62 }
63 
64 int main(){
65     solve();
66     string s1,s2;
67     while(cin>>s1>>s2){
68         if(s1=="0"&&s2=="0")break;
69         int count=0;
70         for(int i=0;i<N;i++){
71             if(cmp(s1,s2,f[i]))
72                 count++;
73         }
74         printf("%d\n",count);
75     }
76     return 0;
77 }
原文地址:https://www.cnblogs.com/wally/p/2937087.html