本文主要是介绍1079. Maximum,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1079. MaximumTime limit: 2.0 second
Memory limit: 64 MB
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
10
0
3
4
Problem Author: Emil Kelevedzhiev
Problem Source: Winter Mathematical Festival Varna '2001 Informatics Tournament
#include <iostream>
#include <vector>
using namespace std;
void Maximum(int n){
vector<int> nums;
nums.insert(nums.end(), 0);
nums.insert(nums.end(), 1);
if(n<=1){
cout<<nums[n]<<endl;
return;
}
int max = nums[1];
for(int i = 2 ; i <= n ; i++){
if(i%2==0){
nums.insert(nums.end(), nums[i/2]);
}
else{
nums.insert(nums.end(), (nums[(i-1)/2]+nums[((i-1)/2) + 1]));
}
if(nums[i]>max){
max = nums[i];
}
}
cout<<max<<endl;
}
int main(){
vector<int> inputnum;
int n;
cin>>n;
while (n!=0) {
inputnum.insert(inputnum.end(), n);
cin>>n;
}
for(int i = 0 ; i < inputnum.size() ; i++){
Maximum(inputnum[i]);
}
return 0;
}
这篇关于1079. Maximum的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!