原题链接:The 3n + 1 problem
解题思路:
还是原来的套路,,先得出所有结果,,
然后输入,,筛选,,输出。。
注意事项:
x 推荐使用long long int,,
否则i==113383,i==134379,i==138367时会爆int,,虽然测试的数据都是小于等于1000的
参考代码:
#include <stdio.h>
int a[1000001];
int main() {
int m, n, max;
long long int x;
int count;
int temp, i;
for (i = 1; i <= 1000000; i++) {
x = i;
count = 1;
while (x != 1) {
if (x % 2 == 0)
x /= 2;
else
x = x * 3 + 1;
count++;
}
a[i] = count;
}
while (scanf("%d%d", &m, &n) != EOF) {
printf("%d %d ", m, n);
if (m > n) {
temp = m;
m = n;
n = temp;
}
max = 0;
for (i = m; i <= n; i++) {
max = a[i] > max ? a[i] : max;
}
printf("%d\n", max);
}
return 0;
}0.0分
3 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
#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; } //我的为毛不过?我看别人的int能通过啊