**
航电oj:The 3n + 1 problem
**
#题目描述
#给你一个区间 要你求在这个区间内能运转这个规则的次数
#这个规则是 奇数就变为三倍加1 偶数就除2 是1了就结束
#知识点
英语? 语文?
#代码
这个题就是有坑 算法并不难
第一版 没有注意到坑 但我觉得这个比第二版好 可以看看 (这个是挂的!!!)
#include<cstdio>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<stdio.h>
using namespace std;
int ans[1000020];
int n,m;
void math(int x)
{
if(x == 1)
{
ans[x] = 1;
return ;
}
if(x %2 ==0)//偶数
{
math(x/2);
ans[x] = ans[x/2] +1;
}
else
{
math(3*x+1);
ans[x] = ans[3*x+1] +1;
}
}
int main()
{
memset(ans,0,sizeof(ans));
while(scanf("%d%d",&n,&m)!=EOF)
{
double ark = 0;
for(int i=n; i<=m; i++)
{
math(i);
ark = max((int)ark,ans[i]);
}
cout <<n <<" "<<m<<" " << (int)ark <<endl;
}
return 0;
}
第二版 过了 换了种方式来算
#include<cstdio>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<stdio.h>
using namespace std;
int n,m;
int main()//英语不好 就卡在坑上了 可恶啊w(゚Д゚)w
{
while(scanf("%d%d",&n,&m)!=EOF)//大小不确定
{
int a,b;
if(n > m)
{
a = m;
b = n;
}else
{
a = n;
b = m;
}
int ark = 0;
for(int i=a; i<=b; i++)
{
int temp = i;
int ans = 0;
while(temp != 1)
{
if(temp %2 == 0)
{
temp = temp /2;
ans ++;
}else
{
temp = 3*temp+1;
ans ++;
}
}
ark = max(ark,ans);
}
cout <<n <<" "<<m<<" " << ark+1 <<endl;//必需要是原来的n m 不能是a b
}
return 0;
}
#总结
英语好真是太重要了 ┭┮﹏┭┮ 多看看题总是好的