Ural1079-Maximum

来源:互联网 发布:java.util.date怎么用 编辑:程序博客网 时间:2024/06/07 03:39

递归预处理

#include <cstdio>#include <algorithm>using namespace std;const int maxn = 100000 + 5;int a[maxn];int f(int x) {    if (x == 0 || (x != 0 && a[x])) {        return a[x];    }    if (x % 2 == 0) {        return f(x/2);    } else {        return f((x-1)/2) + f((x-1)/2+1);    }}int main(int argc, char const *argv[]) {    a[0] = 0;    a[1] = 1;    for (int i = 2; i < 100000; i++) {        a[i] = f(i);    }    int n;    while (scanf("%d", &n) == 1 && n) {        printf("%d\n", *max_element(a, a + n + 1));    }    return 0;}
0 0
原创粉丝点击