题意:
求斐波那契的前10000项目
分析:
模拟竖式加法, 用string作为数字的储存形式
#include<bits/stdc++.h> using namespace std; string add1(string s1, string s2) { if (s1 == "" && s2 == "") return "0"; if (s1 == "") return s2; if (s2 == "") return s1; string maxx = s1, minn = s2; if (s1.length() < s2.length()){ maxx = s2; minn = s1; } int a = maxx.length() - 1, b = minn.length() - 1; for (int i = b; i >= 0; --i){ maxx[a--] += minn[i] - ‘0‘; // a一直在减 , 额外还要减个‘0‘ } for (int i = maxx.length()-1; i > 0;--i){ if (maxx[i] > ‘9‘){ maxx[i] -= 10;//注意这个是减10 maxx[i - 1]++; } } if (maxx[0] > ‘9‘){ maxx[0] -= 10; maxx = ‘1‘ + maxx; } return maxx; } int main(){ string a[10007]; a[3] = "4" , a[4] = "7"; for(int i = 5; i <= 10000; i++){ a[i] = add1(a[i-1],a[i-2]); } int n; while(cin >> n){ cout << a[n] << "\n"; } }