原题链接:The 3n + 1 problem
参考代码:
#include <iostream>
using namespace std;
int main()
{
int i,j,tot;
while(cin>>i>>j){
cout<<i<<" "<<j<<" ";
int maxt=0;
if(i>j){int temp=i;i=j;j=temp;}
for(int k=i;k<=j;k++){
tot=1;
int n=k;
while(n!=1){
if(n%2==0)n=n/2;
else n=3*n+1;
tot++;
}
if(maxt<tot) maxt=tot;
}
cout<<maxt<<endl;
}
return 0;
}0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
#include<iostream> using namespace std; length(int x,int y) { int i=0; int j; int sum; int str=0; for(j=x;j<y;j++) { sum=j; while(sum!=1) { if(sum%2==0) sum=sum/2; else sum=3*sum+1; i++; } if(str<i) str=i+1; i=0; } return str; } int main() { int a,b; while(cin>>a>>b) { cout<<a<<" "<<b<<" "<<length(a,b)<<endl; } return 0; } 为啥编译错误啊。。。#include<iostream> using namespace std; int three(int a,int b); int main() { int a,b; while(cin>>a) { cin>>b; int t; if(a>b) { t=a; a=b; b=t; } three(a,b); } return 0; } int three(int a,int b) { int i,n=0,max=0; for( i=a;i<=b;i++) { n=1; int j=i; while(j!=1) { if(j%2==0) j=j/2; else j=j*3+1; n++; } if(n>max) max=n; } cout<<a<<" "<<b<<" "<<max<<endl; } 我的为啥只过%30