Ural 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 integern (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
// Ural Problem 1079. Maximum
// Verdict: Accepted  
// Submission Date: 14:33:08 15 Jan 2014
// Run Time: 0.031s
//  
// 版权所有(C)acutus   (mail: acutus@126.com) 
// 博客地址:http://www.cnblogs.com/acutus/
// [解题方法]  
// 简单题模拟题
// 注意:最大项有可能是奇数项

#include<stdio.h>

long long a[100000], b[100000];

void solve()
{
    int i, j, max, n;
    a[0] = b[0] = 0;
    a[1] = b[1] = 1;
    max = 1;
    for(i = 2; i < 100000; i++) {
        if(i % 2) {
            j = (i - 1) >> 1;
            a[i] = a[j] + a[j + 1];
        } else {
            a[i] = a[i >> 1];
        }
        if(a[i] > max)
            max = a[i];
        b[i] = max;
    }
    while(scanf("%d", &n) && n) {
        printf("%lld\n", b[n]);
    }
}

int main()
{
    solve();
    return 0;
}

 

posted @ 2014-01-15 14:34  acutus  阅读(305)  评论(0编辑  收藏  举报
TOP