HDU 1250 Hat's Fibonacci(高精度)

Problem Description
A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take a number as input, and print that Fibonacci number.
 
Input
Each line will contain an integers. Process to end of file.
 
Output
For each case, output the result in a line.
 


题目大意:看题。
思路:高精度

 

代码(671MS):

  1 //模板测试
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <string>
  6 #include <algorithm>
  7 using namespace std;
  8 
  9 const int MAXN = 2010;
 10 
 11 struct bign {
 12     int len, s[MAXN];
 13 
 14     bign () {
 15         memset(s, 0, sizeof(s));
 16         len = 1;
 17     }
 18     bign (int num) { *this = num; }
 19     bign (const char *num) { *this = num; }
 20 
 21     bign operator = (const int num) {//数字
 22         char s[MAXN];
 23         sprintf(s, "%d", num);
 24         *this = s;
 25         return *this;
 26     }
 27     bign operator = (const char *num) {//字符串
 28         for(int i = 0; num[i] == '0'; num++) ;  //去前导0
 29         if(*num == 0) --num;
 30         len = strlen(num);
 31         for(int i = 0; i < len; ++i) s[i] = num[len-i-1] - '0';
 32         return *this;
 33     }
 34 
 35     bign operator + (const bign &b) const {
 36         bign c;
 37         c.len = 0;
 38         for(int i = 0, g = 0; g || i < max(len, b.len); ++i) {
 39             int x = g;
 40             if(i < len) x += s[i];
 41             if(i < b.len) x += b.s[i];
 42             c.s[c.len++] = x % 10;
 43             g = x / 10;
 44         }
 45         return c;
 46     }
 47 
 48     bign operator += (const bign &b) {
 49         *this = *this + b;
 50         return *this;
 51     }
 52 
 53     void clean() {
 54         while(len > 1 && !s[len-1]) len--;
 55     }
 56 
 57     bign operator * (const bign &b) {
 58         bign c;
 59         c.len = len + b.len;
 60         for(int i = 0; i < len; ++i) {
 61             for(int j = 0; j < b.len; ++j) {
 62                 c.s[i+j] += s[i] * b.s[j];
 63             }
 64         }
 65         for(int i = 0; i < c.len; ++i) {
 66             c.s[i+1] += c.s[i]/10;
 67             c.s[i] %= 10;
 68         }
 69         c.clean();
 70         return c;
 71     }
 72     bign operator *= (const bign &b) {
 73         *this = *this * b;
 74         return *this;
 75     }
 76 
 77     bign operator - (const bign &b) {
 78         bign c;
 79         c.len = 0;
 80         for(int i = 0, g = 0; i < len; ++i) {
 81             int x = s[i] - g;
 82             if(i < b.len) x -= b.s[i];
 83             if(x >= 0) g = 0;
 84             else {
 85                 g = 1;
 86                 x += 10;
 87             }
 88             c.s[c.len++] = x;
 89         }
 90         c.clean();
 91         return c;
 92     }
 93     bign operator -= (const bign &b) {
 94         *this = *this - b;
 95         return *this;
 96     }
 97 
 98     bign operator / (const bign &b) {
 99         bign c, f = 0;
100         for(int i = len - 1; i >= 0; i--) {
101             f *= 10;
102             f.s[0] = s[i];
103             while(f >= b) {
104                 f -= b;
105                 c.s[i]++;
106             }
107         }
108         c.len = len;
109         c.clean();
110         return c;
111     }
112     bign operator /= (const bign &b) {
113         *this  = *this / b;
114         return *this;
115     }
116 
117     bign operator % (const bign &b) {
118         bign r = *this / b;
119         r = *this - r*b;
120         return r;
121     }
122     bign operator %= (const bign &b) {
123         *this = *this % b;
124         return *this;
125     }
126 
127     bool operator < (const bign &b) {
128         if(len != b.len) return len < b.len;
129         for(int i = len-1; i >= 0; i--) {
130             if(s[i] != b.s[i]) return s[i] < b.s[i];
131         }
132         return false;
133     }
134 
135     bool operator > (const bign &b) {
136         if(len != b.len) return len > b.len;
137         for(int i = len-1; i >= 0; i--) {
138             if(s[i] != b.s[i]) return s[i] > b.s[i];
139         }
140         return false;
141     }
142 
143     bool operator == (const bign &b) {
144         return !(*this > b) && !(*this < b);
145     }
146 
147     bool operator != (const bign &b) {
148         return !(*this == b);
149     }
150 
151     bool operator <= (const bign &b) {
152         return *this < b || *this == b;
153     }
154 
155     bool operator >= (const bign &b) {
156         return *this > b || *this == b;
157     }
158 
159     string str() const {
160         string res = "";
161         for(int i = 0; i < len; ++i) res = char(s[i]+'0') + res;
162         return res;
163     }
164 };
165 
166 istream& operator >> (istream &in, bign &x) {
167     string s;
168     in >> s;
169     x = s.c_str();
170     return in;
171 }
172 
173 ostream& operator << (ostream &out, const bign &x) {
174     out << x.str();
175     return out;
176 }
177 
178 bign f[5];
179 
180 void solve(int n) {
181     f[1] = f[2] = f[3] = f[4] = 1;
182     if(n < 5) cout<<f[n]<<endl;
183     else {
184         int x = 1;
185         for(int i = 5; i <= n; ++i) {
186             f[0] = f[1] + f[2] + f[3] + f[4];
187             f[x] = f[0];
188             if(++x == 5) x = 1;
189         }
190         cout<<f[0]<<endl;
191     }
192 }
193 
194 int main() {
195     int n;
196     while(scanf("%d", &n)!=EOF) {
197         solve(n);
198     }
199     return 0;
200 }
View Code

 

 

posted @ 2013-08-20 15:14  Oyking  阅读(194)  评论(0编辑  收藏  举报