首页 > 学院 > 开发设计 > 正文

3n+1 problem

2019-11-08 01:42:10
字体:
来源:转载
供稿:网友

简单粗暴

////  main.cpp//  PC_test////  Created by liuyuhan on 17/2/20.//  Copyright (c) 2017年 liuyuhan. All rights reserved.///* if i > j, exchange them for every number from i to j, compute the circle number, if it is bigger,record it*/#include <iostream>using namespace std;int main(int argc, const char * argv[]) {    int n, N; // the circle number n for every k, the max one N    int i, j, i2, j2; // the input every time    int t; // for i and j to exchange    int k; // the number from i to j        while(cin >> i >>j) {        N = 0;        i2 = i;        j2 = j;        if (i2 > j2) { t = i2; i2 = j; j2 = t; }        for (k = i2; k <= j2; k++)        {            int k2 = k;            n = 1;            while (k2 != 1)            {                if (k2 % 2 == 0) {                    k2 = k2 / 2;                    n ++;                }                else {                    k2 = k2 * 3 + 1;                    n ++;                }            }            if (n > N)                N = n;        }        cout << i << " " << j << " " << N << endl;    }    return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表