参考程序1:
#include<iostream>
using namespace std;
int first,second;
int m,n;
int sum = 0;
int fn(int n) {
if(n==1) {
return first;
} else if (n==2) {
return second;
} else {
return fn(n-1)+fn(n-2);
}
}
int main() {
cin>>first>>second;
cin>>m>>n;
sum = first+second;
for(int i = 3; i<=n; i++) {
sum+=fn(i);
if(fn(i)>=m) {
break;
}
}
cout<<sum;
}
参考程序2:
#include<iostream>
using namespace std;
int main() {
int a, b, m, n, ans, c;
cin>>a>>b>>m>>n;
ans = a+b;
for(int i=3; i<=n; i++) {
c = a+b;
ans += c;
a = b;
b = c;
if(c>=m) break;
}
cout<<ans;
}
参考程序3:
#include<iostream>
using namespace std;
int main() {
int a, b, m, n, num, num1;
cin>>a>>b>>m>>n;
num = a+b;
num1 = num+b;
int z = a+b+num+num1;
for(int i=5; i<=n; i++) {
b = num;
num = num1;
num1 = b+num;
z += num1;
if(num1>=m) break;
}
cout<<z;
}