题目 根据给定的运算规定,算循环数字个数。输入一个范围,返回范围内所有数中循环数最大的
注意 输入的范围不一定就是第一个比第二个小,要注意判断
#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
*/