POJ1207解题报告
来源:互联网 发布:肿眼泡做双眼皮知乎 编辑:程序博客网 时间:2024/05/17 00:51
题意
这个题的意思是 :有一个数,按那个规则,最后能够转换到1,算出这个序列的长度然后输入两个数,在这两个数构成的闭区间中,每个数都有其序列长度,求这个序列中最长的一个。
思路
先写出一个函数,求一个数的序列长度,然后对区间做循环,求长度最大值
难点
似乎也没什么难点,只是有很多细节需要考虑一下,详见代码 我改了三次才AC,就是因为输出那块出了些问题。。。
代码
#include <iostream>using namespace std;int number(int num){ //计算一个数序列长度,返回值就是序列长度 int count=1; while(1){ if(num==1){return count;} if(num%2!=0){num=3*num+1;count++; } if(num%2==0) {num=num/2;count++;} }}int main(){ int low,high,i; while(cin>>low>>high){ int m =low,n=high; //最后输出格式需要 if(low>high){ //因为他没说输入两个数的大小关系,所以得判断一下把小的数放在前面 int temp = low; low = high; high = temp; } int max = number(low); //刚才出错就出在这儿了,low写成了i for(i=low;i<=high;i++){ if(number(i)>max) max = number(i); } cout<<m<<" "<<n<<" "<<max<<endl; //这一句我也曾出过错 } return 0;}
0 0
- POJ1207解题报告
- POJ1207解题报告
- POJ1207解题报告 Java
- POJ1207解题报告----极水的题目
- POJ1207
- poj1207
- poj1207
- POJ1207
- POJ1207
- poj1207
- poj1207
- poj1207
- poj1207
- poj1207
- poj1207
- 解题报告
- 解题报告
- 解题报告
- JNI 共享内存
- opencv运行时exe 无法写进去的解决方法
- Quartz2D
- Android内存泄露案例分析
- linux设备驱动归纳总结(八):3.设备管理的分层与面向对象思想
- POJ1207解题报告
- git +repo使用技巧
- VS2013 编译程序时报无法加载PDB文件错误解决方案
- Group By 语句结合聚合函数
- 旋转动画的简单使用
- Programmer-Competency-Matrix
- RTLable
- jQuery UI 插件Datepicker Widget
- 【转】mysql 主从设置