POJ 1207 求最大数链长度 暴力枚举数学题

本文详细介绍了如何优化暴力枚举算法处理整数对最大循环长度问题,特别关注了顺序反转情况下的输出顺序调整,并提供了通过标志位记录交换i和j的代码实现。该文通过实例演示了输入输出处理逻辑,最终实现了解决方案的验证与应用。

这个题目直接用的暴力枚举,但是还是WA了几次

原因是这句话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. 

注意i可能大于j,此时需要交换顺序,但是输出的时候还会是原始顺序

比如 输入1 10 输出 1 10 20 ;输入10 1 输出 10 1 20

因此交换i和j需要用标志位记录一下

Source Code

Problem: 1207 User: yangliuACMer
Memory: 244K Time: 16MS
Language: C++ Result: Accepted
//暴力枚举 //21:22 #include <iostream> using namespace std; int main(){ int i,j,k,count,max,newk,temp; bool flag; while(cin>>i>>j){ max = 1; flag = true; if(i > j){ temp = i; i = j; j = temp; flag = false; } for(k = i; k <= j; k++){ count = 1; newk = k;//注意计算循环节的时候k的值会发生变化 while(newk != 1){ count++; if(newk % 2) newk = 3 * newk + 1; else newk/=2; } if(count > max) max = count; } if(flag) cout<<i<<" "<<j<<" "<<max<<endl; else cout<<j<<" "<<i<<" "<<max<<endl; } return 0; } //21:42

转载于:https://www.cnblogs.com/yangliu/archive/2012/02/10/2421749.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值