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