OpenJudge - 1207:The 3n + 1 problem

来源:互联网 发布:淘宝刷关注多少钱一个 编辑:程序博客网 时间:2024/05/29 15:39

题目链接:

http://bailian.openjudge.cn/practice/1207


Version 1.0 (2014-11-01)

Memory = 260 kb

Time = 0 ms

#include <stdio.h>int CLs[100000] = {0};int GetCL(int n){int CL = 1;while (n != 1) {/*if (CLs[n]) {return CL + CLs[n];}else {*/if (n >> 1 << 1 == n) {n = n >> 1;}else {n = 3 * n + 1;}CL++;//}}return CL;}int main(){int min, max;while (scanf("%d", &min) != EOF) {int CL = 0, tmp = 0;scanf("%d", &max);for (int i = min; i <= max; i++) {tmp = GetCL(i);if (tmp >= CL)CL = tmp;}for (int i = max; i <= min; i++) {tmp = GetCL(i);if (tmp >= CL)CL = tmp;}printf("%d %d %d\n", min, max, CL);}return 0;}


0 0
原创粉丝点击