博客
关于我
热身题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<
<

总结:我是菜鸡!!!

你可能感兴趣的文章
MySQL5.6的zip包安装教程
查看>>
mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
查看>>
Webpack 基本环境搭建
查看>>
mysql5.7 安装版 表不能输入汉字解决方案
查看>>
MySQL5.7.18主从复制搭建(一主一从)
查看>>
MySQL5.7.19-win64安装启动
查看>>
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>