博客
关于我
热身题A——The 3n + 1 problem
阅读量:201 次
发布时间:2019-02-28

本文共 3019 字,大约阅读时间需要 10 分钟。

先不说题,我要吐槽!!!这题太坑了大哭,你给了1-1,000,000的范围,然后你竟然不用任何精妙的算法,直接暴力循环就能AC,然而我还以为要用什么精妙的算法,写了大半天。。。(虽然最后还没写出来,手动黑脸),原因是忘了比较i和j的大小顺序,最后气哄哄的跑去看题解,f**k,竟然真的是水题,我还是太菜狗了。

正题:

没有废话,直接上代码:

//problems://Problems in Computer Science are often classified as belonging //to a certain class of problems (e.g., NP, Unsolvable, Recursive).// In this problem you will be analyzing a property of an algorithm //whose classification is not known for all possible inputs. //Consider the following algorithm: //    1.      input n //    2.      print n //    3.      if n = 1 then STOP //    4.           if n is odd then n <- 3n + 1 //    5.           else n <- n / 2 //    6.      GOTO 2 //Given the input 22, the following sequence of numbers will be printed //22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 ////It is conjectured that the algorithm above will terminate //(when a 1 is printed) for any integral input value. Despite the //simplicity of the algorithm, it is unknown whether this conjecture is true. //It has been verified, however, for all integers n such that 0 < n < 1,000,000 //(and, in fact, for many more numbers than this.) ////Given an input n, it is possible to determine the number of numbers printed //(including the 1). For a given n this is called the cycle-length of n. //In the example above, the cycle length of 22 is 16. //For any two numbers i and j you are to determine the maximum cycle //length over all numbers between i and j. //Input://The input will consist of a series of pairs of integers i and j, //one pair of integers per line. All integers will be less than 1,000,000 and greater than 0. ////You should process all pairs of integers and for each pair determine //the maximum cycle length over all integers between and including i and j. ////You can assume that no opperation overflows a 32-bit integer. //Output://For each pair of input integers i and j you should output i, j, //and the maximum cycle length for integers between and including i and j. //These three numbers should be separated by at least one space with all //three numbers on one line and with one line of output for each line of input. //The integers i and j must appear in the output in the same order in which //they appeared in the input and should be followed by the maximum cycle length (on the same line). #include 
using namespace std;//求n得循环长度 int find(int n){ int cnt=0; while(n!=1){ cnt++; if(n%2) n=3*n+1; else n/=2; } return cnt+1;}int main(){ int i,j,max; while(cin>>i>>j){ //你没有看错,我的那个拙略的算法,就是将i,j都放进一个数组中,然后遍历数组求出最大长度 int vis[1000001]={0}; max=0; cout<
<<' '<
<<' '; //趁i,j位置没变,先偷偷输出他 //将i,j按大小排列 if(i>j){ int t=i; i=j; j=t; } //遍历i到j for(int k=i;k<=j;k++){ if(!vis[k]){ //若k的长度未知 int cnt=find(k); vis[k]=cnt; if(k%2 && k*3+1<=j) vis[k*3+1]=cnt-1;//当k长度确定时,且k为奇数时,3k+1的长度确定 else if(k*2<=j) vis[2*k]=cnt+1; //当k确定时,2k的长度确定 } if(vis[k]>max) max=vis[k]; //求最大长度 } cout<
<

总结:我是菜鸡!!!

你可能感兴趣的文章
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>