博客
关于我
热身题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 写入慢优化
查看>>
mysql 分组统计SQL语句
查看>>
Mysql 分页
查看>>
Mysql 分页语句 Limit原理
查看>>
MySql 创建函数 Error Code : 1418
查看>>
MySQL 创建新用户及授予权限的完整流程
查看>>
mysql 创建表,不能包含关键字values 以及 表id自增问题
查看>>
mysql 删除日志文件详解
查看>>
mysql 判断表字段是否存在,然后修改
查看>>
MySQL 到底能不能放到 Docker 里跑?
查看>>
mysql 前缀索引 命令_11 | Mysql怎么给字符串字段加索引?
查看>>
mysql 协议的退出命令包及解析
查看>>
mysql 取表中分组之后最新一条数据 分组最新数据 分组取最新数据 分组数据 获取每个分类的最新数据
查看>>
mysql 四种存储引擎
查看>>
MySQL 基础模块的面试题总结
查看>>
MySQL 备份 Xtrabackup
查看>>
mysql 多个表关联查询查询时间长的问题
查看>>