博客
关于我
热身题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备份工具xtrabackup
查看>>
mysql备份恢复出错_尝试备份/恢复mysql数据库时出错
查看>>
mysql复制内容到一张新表
查看>>
mysql复制表结构和数据
查看>>
mysql复杂查询,优质题目
查看>>
MySQL外键约束
查看>>
MySQL多表关联on和where速度对比实测谁更快
查看>>
MySQL多表左右连接查询
查看>>
mysql大批量删除(修改)The total number of locks exceeds the lock table size 错误的解决办法
查看>>
mysql如何做到存在就更新不存就插入_MySQL 索引及优化实战(二)
查看>>
mysql如何删除数据表,被关联的数据表如何删除呢
查看>>
MySQL如何实现ACID ?
查看>>
mysql如何记录数据库响应时间
查看>>
MySQL子查询
查看>>
Mysql字段、索引操作
查看>>
mysql字段的细节(查询自定义的字段[意义-行列转置];UNION ALL;case-when)
查看>>
mysql字段类型不一致导致的索引失效
查看>>
mysql字段类型介绍
查看>>
mysql字段解析逗号分割_MySQL逗号分割字段的行列转换技巧
查看>>