Project Euler 14 Longest Collatz sequence
题意:对于任意一个数 N ,寻找在 100,0000 之内按照规则( N 为奇数 N = N * 3 + 1 ,N 为偶数 N = N / 2 ,直到 N = 1 时的步数 )步数的最大值
思路:记忆化搜索即可,利用之前搜索的值加速搜索,如果当前搜索值在之前已经处理过,那么直接利用当前搜索值 + 到当前数的步数即为该数的步数
/*************************************************************************
> File Name: euler014.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月24日 星期六 19时36分58秒
************************************************************************/
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#define MAX_N 1000000
#define MAX_KEEP_RANGE 50000000
int64_t skeep[MAX_KEEP_RANGE+5] = {0};
int64_t DFS(int64_t x){
if( x == 1 ) return 1;
if( x <= MAX_KEEP_RANGE && skeep[x] != 0 ) return skeep[x];
int64_t ans;
if( x & 1 ) ans = DFS( x*3 + 1 ) + 1;
else ans = DFS( x >> 1 ) + 1;
if( x <= MAX_KEEP_RANGE ) skeep[x] = ans;
return ans;
}
int32_t main(){
int64_t maxN = 0;
for(int64_t i = 1 ; i <= MAX_N ; i++){
skeep[i] = DFS(i);
maxN = maxN > skeep[i] ? maxN : skeep[i];
}
printf("%"PRId64"\n",maxN);
return 0;
}
Project Euler 14 Longest Collatz sequence的更多相关文章
- Project Euler Problem 14-Longest Collatz sequence
记忆化搜索来一发.没想到中间会爆int #include <bits/stdc++.h> using namespace std; const int MAXN = 1000000; in ...
- (Problem 14)Longest Collatz sequence
The following iterative sequence is defined for the set of positive integers: n n/2 (n is even) n 3n ...
- project euler 14 collatz
def collatz(num,i): i =i + 1 if num%2 == 0: return collatz(num//2,i) elif num == 1: return i else: r ...
- Python练习题 042:Project Euler 014:最长的考拉兹序列
本题来自 Project Euler 第14题:https://projecteuler.net/problem=14 ''' Project Euler: Problem 14: Longest C ...
- Python练习题 040:Project Euler 012:有超过500个因子的三角形数
本题来自 Project Euler 第12题:https://projecteuler.net/problem=12 # Project Euler: Problem 12: Highly divi ...
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...
- Python练习题 049:Project Euler 022:姓名分值
本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- Python练习题 030:Project Euler 002:偶数斐波那契数之和
本题来自 Project Euler 第2题:https://projecteuler.net/problem=2 # Each new term in the Fibonacci sequence ...
随机推荐
- _DataStructure_C_Impl:链串
//_DataStructure_C_Impl:链串 #include<stdio.h> #include<stdlib.h> #include<string.h> ...
- Project Euler:Problem 77 Prime summations
It is possible to write ten as the sum of primes in exactly five different ways: 7 + 3 5 + 5 5 + 3 + ...
- DecimalFormat格式化输出带小数的数字类型
刚開始 double d = 333333333.333333333; System.out.println(d); 输出结果为3.333333333333333E8 网上找到了DecimalForm ...
- element-UI中table表格的@row-click事件和@selection-change耦合了
<el-table ref="multipleTable" :data="tableData" tooltip-effect="dark&quo ...
- android 系统签名【转】
本文转载自:http://blog.csdn.net/csh86277516/article/details/73549824 Android——编译release版签名系统 AndroidManif ...
- oracle中关于删除表purge语句和闪回语句的基本使用
语法: drop table ... purge; 例子:drop table test purge; purge是直接删除表,不保留到回收站,10G开始默认drop表式改名移动到回收站; 闪回(fl ...
- AJAX复习笔记
AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术.通过在后台与服务器进行少量数据交换,AJAX 可况下更新以使网页实现异步更新. 工作原理: AJAX是基于现有的Internet ...
- NVL和NVL2有什么区别,NULLIF 的使用.
NULL指的是空值,或者非法值. NVL (expr1, expr2):expr1为NULL,返回expr2:不为NULL,返回expr1.注意两者的类型要一致 NVL2 (expr1, expr2, ...
- kindeditor文本编辑器乱码中乱码问题解决办法
这个问题我已经解决掉了,不是更改内容的编码格式,只要将lang/zh_CN.js 这个文件的编码转换成unicode即可 操作方法是 用记事本打开这个文件,另存为,然后更改文件的编码格式为unico ...
- NSLayoutConstraint的使用
*一切皆代码*- -- #继承关系框架|类|类:-:|:-:|:-:UIKit|NSLayoutConstraint|--|-|- #应用场景UI界面的搭建一般会占用项目开发相当一部分的时间.涉及到控 ...