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
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
input | output |
---|---|
5 10 0 |
3 4 |
Problem Author: Emil Kelevedzhiev
Problem Source: Winter Mathematical Festival Varna '2001 Informatics Tournament
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; }
作者:acutus
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.