uva 100 The 3n + 1 problem

题目  根据给定的运算规定,算循环数字个数。输入一个范围,返回范围内所有数中循环数最大的

注意  输入的范围不一定就是第一个比第二个小,要注意判断

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sstream>

#include<cstdio>

#include<iostream>

#include<math.h>

#include <map>

#include <vector>

#include <algorithm>

using namespace std;

int func(int n){

    int i = 1;

    while(1) {

        if(n == 1) break;

        if(n % 2 == 1) {

            i++;

            n = 3 * n + 1;

        }

        if(n % 2 == 0) {

            i++;

            n /= 2;

        }

    }

    return i;

}

int main(){

    int a,b;

    vector<int> num;

    while(cin>> a >> b) {

        int flag = 1;

        if(a > b){

            int temp = a;

            a = b;

            b = temp;

            flag=0;

        }

        for(int i = a; i <= b; i ++) {

            num.push_back(func(i)); //1️⃣func函数的嵌套

        }

    

    sort(num.begin(), num.end());//2️⃣sort函数两个迭代器组成,增序排列

    if(flag == 1)printf("%d %d %d\n",a,b,num[num.size()-1]);//3️⃣size()-1才是最后一个

        else printf("%d %d %d\n",b,a,num[num.size()-1]);//4️⃣注意输出还是原顺序a,b

    num.clear();

    }

    

    

    

    return 0;

}

/*

 1 10

 100 200

 201 210

 900 1000

 */



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值