您的位置:首页 > 移动开发 > IOS开发

ural 1079. Maximum

2015-05-05 20:24 253 查看


1079. Maximum

Time 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

inputoutput
5
10
0

3
4

Problem Author: Emil Kelevedzhiev
Problem Source: Winter Mathematical Festival Varna '2001 Informatics Tournament

Tags: (

show tags for all problems
)

给你一个n,找出一到n里面a[i]最大值,,当然普通暴力必然超时,另外开一个数组存所有的最大值便好了 。
复杂度是O(n)。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 100000
int a[MAXN];
int sum[MAXN];
int main()
{
int i;
int ans=3;
a[0]=0;
a[1]=1;
a[2]=1;
a[3]=2;
a[4]=1;
a[5]=3;
sum[0]=0;
sum[1]=1;
sum[2]=1;
sum[3]=2;
sum[4]=2;
sum[5]=3;
for(i=6; i<100000; i++)
{
if(i&1)
a[i]=a[i/2]+a[i/2+1];
else
a[i]=a[i/2];
ans=max(ans,a[i]);
sum[i]=ans;
}
int n;
while(cin>>n,n)
cout<<sum
<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息