zoukankan      html  css  js  c++  java
  • zzulioj 2597: 角谷猜想2

    题目链接:

    http://acm.zzuli.edu.cn/problem.php?id=2597

      这题n给的数字很大,显然需要我们打表,而且直接打表的话也很浪费时间,所以我们可以用记忆化搜索的方式,储存之前计算的值在之后计算用的时候用上以减少计算量

    #include<set>
    #include<map>
    #include<stack>
    #include<queue>
    #include<cmath>
    #include<cstdio>
    #include<cctype>
    #include<string>
    #include<vector>
    #include<climits>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #define max(a, b) (a > b ? a : b)
    #define min(a, b) (a < b ? a : b)
    #define mst(a) memset(a, 0, sizeof(a))
    #define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> P;
    const double eps = 1e-7;
    const int INF = 0x3f3f3f3f;
    const ll ll_INF = 0x3f3f3f3f3f3f3f;
    const int maxn = 4e6+10;
    ll res[maxn] = {0,1};
    //递归
    ll solve(ll x) {
        ll cnt;
        if (x < maxn && res[x]) //利用中间结果减少计算量
            return res[x];
        cnt = solve(x&1 ? x*3 + 1 : x >> 1) + 1;
        return cnt;
    }
    //循环版
    //ll solve(ll x) {
    //    ll cnt = 1;
    //    while(x != 1) {
    //        x&1 ? x = x*3 + 1 : x >>= 1;
    //        ++cnt;
    //        if (x <= maxn && res[x])
    //            return cnt+res[x]-1;
    //    }
    //    return cnt;
    //}
    int main(void) {
        for(int i = 2; i<maxn; ++i)
            res[i] = solve(i);
        P _max = {res[1], 1};
        res[1] = 1;
        for(int i = 2; i<maxn; ++i) { //求解1~n内步长最大的数
            if (res[i] < _max.first)
                res[i] = _max.second;
            else if (res[i] > _max.first) {
                _max.first = res[i];
                _max.second = i;
                res[i] = i;
            }
            //printf("%lld ", res[i]);
        }
        int num;
        while(~scanf("%d", &num))
            printf("%lld\n", res[num]);
        return 0;
    }
  • 相关阅读:
    Ubuntu16.04 JAVA配置!
    vs快捷键
    2015上半年软件设计师考点,难点5
    2015上半年软件设计师考点,难点4
    2015上半年软件设计师考点,难点3
    2015上半年软件设计师考点,难点2
    软件的知识产权保护
    标准化知识
    嵌入式系统
    2015上半年软件设计师考点,难点
  • 原文地址:https://www.cnblogs.com/shuitiangong/p/12070540.html
Copyright © 2011-2022 走看看