博客
关于我
热身题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 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>
Mysql 学习总结(89)—— Mysql 库表容量统计
查看>>
mysql 实现主从复制/主从同步
查看>>
mysql 审核_审核MySQL数据库上的登录
查看>>
mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
查看>>
mysql 导入导出大文件
查看>>
MySQL 导出数据
查看>>
mysql 将null转代为0
查看>>
mysql 常用
查看>>
MySQL 常用列类型
查看>>
mysql 常用命令
查看>>
Mysql 常见ALTER TABLE操作
查看>>