斐波那契数列(大数加法)
题意:
求斐波那契的前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"; } }
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25