• 1001 害死人不偿命的(3n+1)猜想 (15分)


    卡拉兹(Callatz)猜想:

    对任何一个正整数 n,如果它是偶数,那么把它砍掉一半;如果它是奇数,那么把 ( 砍掉一半。这样一直反复砍下去,最后一定在某一步得到 n=1。卡拉兹在 1950 年的世界数学家大会上公布了这个猜想,传说当时耶鲁大学师生齐动员,拼命想证明这个貌似很傻很天真的命题,结果闹得学生们无心学业,一心只证 (,以至于有人说这是一个阴谋,卡拉兹是在蓄意延缓美国数学界教学与科研的进展……

    我们今天的题目不是证明卡拉兹猜想,而是对给定的任一不超过 1000 的正整数 n,简单地数一下,需要多少步(砍几下)才能得到 n=1?

    输入格式:

    每个测试输入包含 1 个测试用例,即给出正整数 n 的值。

    输出格式:

    输出从 n 计算到 1 需要的步数。

    输入样例:

    3
    
     

    输出样例:

    5


    #include<iostream>
    #include<algorithm>
    #include<cstdlib>
    #include<cstdio>
    #include<queue>
    #include<stack>
    #include<cstring>
    #include<string.h>
    #include<cmath>
    using namespace std;
    const int maxn=1010;
        
    int step;
    void compute(int n){
        if(n==1){
            return ; 
        }
        else if(n%2==0){
            step++;
            compute(n/2);
        }else if(n%2!=0){
            n=n*3+1;
            step++;
            compute(n/2);
        }
    }
    int main(){
        int n;
        cin>>n;
        compute(n);
        cout<<step<<endl;
        return 0;
    }
  • 相关阅读:
    【转】Netty系列之Netty是什么
    【转】程序员技术练级攻略
    【转】Spring MVC 教程,快速入门,深入分析
    【转】Jqgrid学习之数据
    【转】Jqgrid学习之ColModel API
    【转】jqGrid学习之参数
    【转】jqGrid学习之安装
    go语言项目汇总
    33 Introducing the Go Race Detector
    32 Profiling Go Programs 分析go语言项目
  • 原文地址:https://www.cnblogs.com/dreamzj/p/13843616.html
Copyright © 2020-2023  润新知