1079. Maximum
Consider the sequence of numbers ai, i = 0, 1, 2, …, which satisfies the following requirements:
a0 = 0
a1 = 1
a2i = ai
a2i+1 = ai + ai+1
for every i = 1, 2, 3, … .
Write a program which for a given value of n finds the largest number among the numbers a0, a1, …, an.
Input
You are given several test cases (not more than 10). Each test case is a line containing an integer n (1 ≤ n ≤ 99 999). The last line of input contains 0.
Output
For every n in the input write the corresponding maximum value found.
Sample
input output
5 3
10 4
0
Consider the sequence of numbers ai, i = 0, 1, 2, …, which satisfies the following requirements:
a0 = 0
a1 = 1
a2i = ai
a2i+1 = ai + ai+1
for every i = 1, 2, 3, … .
Write a program which for a given value of n finds the largest number among the numbers a0, a1, …, an.
Input
You are given several test cases (not more than 10). Each test case is a line containing an integer n (1 ≤ n ≤ 99 999). The last line of input contains 0.
Output
For every n in the input write the corresponding maximum value found.
Sample
input output
5 3
10 4
0
Bài này mình giải như sau nhưng không hiểu sai đâu?? Mọi người chỉ giáo nhé!
Code:
#include <iostream> using namespace std; int main() { long n,i=0,i_f=2; long arr[10], f[99999]; f[0]=0; f[1]=1; do { cin>>n; while(n>=i_f) //f(n) chua dc tinh { if(i_f%2==0) f[i_f]= f[i_f/2]; else f[i_f]= f[i_f/2]+f[i_f/2+1]; i_f++; } if(n%2==0) arr[i++]=f[n-1]; else arr[i++]=f[n]; } while(n!=0); for(int j=0;j<i-1;j++) cout<<arr[j]<<endl; cin.get(); cin.get(); return 0; }
Comment