uva 100 The 3n + 1 problem
来源:互联网 发布:长虹大数据公司 编辑:程序博客网 时间:2024/05/20 08:00
题目 根据给定的运算规定,算循环数字个数。输入一个范围,返回范围内所有数中循环数最大的
注意 输入的范围不一定就是第一个比第二个小,要注意判断
#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 namespacestd;
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才是最后一个
elseprintf("%d %d %d\n",b,a,num[num.size()-1]);//4️⃣注意输出还是原顺序a,b
num.clear();
}
return0;
}
/*
1 10
100 200
201 210
900 1000
*/
- UVA 100 The 3n + 1 problem
- UVa 100 - The 3n + 1 problem
- UVa 100 - The 3n + 1 problem
- UVa 100 - The 3n + 1 problem
- uva 100 The 3n+1 problem
- UVa 100 - The 3n + 1 problem
- UVa 100 The 3n + 1 problem
- uva 100 The 3n + 1 problem
- UVa 100 The 3n + 1 problem
- UVa 100 The 3n + 1 problem
- uva--100 - The 3n + 1 problem
- UVa 100 - The 3n + 1 problem
- UVa 100 - The 3n + 1 problem
- UVa 100 - The 3n + 1 problem
- UVa - 100 - The 3n + 1 problem
- UVa 100 - The 3n + 1 problem
- Uva 100 The 3n+1 problem
- UVa 100 - The 3n + 1 problem
- js原生dom操作
- 542. 01 Matrix
- 利用Access-Control-Allow-Origin响应头解决跨域请求
- spring boot mail 踩坑
- UITextField的那点事
- uva 100 The 3n + 1 problem
- 转接IC SJXX串口扩展芯片
- 转载: Python--详解Python中re.sub 作者:Mrzhoug
- 利用synchronized解决哲学家问题
- 1007(数组,时间超限)
- java实现记事本
- /etc/inittab
- hdu5375 格雷码性质 dp
- 基于How To Tango With Django 1.9的重新实践(12)——Bootstrapping Rango