The 3n + 1 problem UVA - 100
[问题描述]
考虑如下的序列生成算法:从整数 n 开始,如果 n 是偶数,把它除以 2;如果 n 是奇数,把它乘 3 加1。用新得到的值重复上述步骤,直到 n = 1 时停止。例如,n = 22 时该算法生成的序列是:
22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
人们猜想(没有得到证明)对于任意整数 n,该算法总能终止于 n = 1。这个猜想对于至少 1 000 000内的整数都是正确的。
对于给定的 n,该序列的元素(包括 1)个数被称为 n 的循环节长度。在上述例子中,22 的循环节长度为 16。输入两个数 i 和 j,你的任务是计算 i 到 j(包含 i 和 j)之间的整数中,循环节长度的最大值。
[输入]
输入每行包含两个整数 i 和 j。所有整数大于 0,小于 1 000 000。
[输出]
对于每对整数 i 和 j,按原来的顺序输出 i 和 j,然后输出二者之间的整数中的最大循环节长度。这三个整数应该用单个空格隔开,且在同一行输出。对于读入的每一组数据,在输出中应位于单独的一行。
[样例输入]
1 10
100 200
201 210
900 1000
[样例输出]
1 10 20
100 200 125
201 210 89
900 1000 174
代码
暴力枚举
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{int i,j,ans[1000];while(cin>>i>>j){memset(ans,0,sizeof(ans));for(int a=i;a<=j;a++){int x=a;while(x!=1){if(x%2==0){x/=2;ans[a]++;}else{x=3*x+1;ans[a]++;}}}int temp=*max_element(ans+i,ans+j);cout<<i<<' '<<j<<' '<<temp+1<<endl;}return 0;
}
Runtime error
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{int i,j;while(scanf("%d %d",&i,&j)!=EOF){cout<<i<<' '<<j<<' ';int min,max,ans=0,temp=0;if(i>j){min=j;max=i;}else{min=i;max=j;}for(int a=min;a<=max;a++){int x=a;while(x!=1){if(x%2==0) x/=2;else x=x*3+1;ans++;}ans++;if(a==min)temp=ans;else{if(ans>temp) temp=ans;}ans=0;}cout<<temp<<endl;}
}
Accepted
The 3n + 1 problem UVA - 100相关推荐
- 110101_The 3n+1 problem
http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=110101&format=html ...
- PC/UVa 题号: 110101/100 3n+1 Problem
不解释了,暴力,不过需要注意的是 i有可能大于j #include <stdio.h> #include <string.h> #define maxnum 1000001 l ...
- UVA 100 HDU 1032 The 3n+1问题 (扑街题)
链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...
- Poj1207 The 3n + 1 problem(水题(数据)+陷阱)
一.Description Problems in Computer Science are often classified as belonging to a certain class of p ...
- the blocks problem(uva 101 or poj 1208)
题目描述见:uva 101 or poj 1208 关键在于彻底理解题目中搬积木的几个命令的含义,见具体分析 如果还不能理解题意,那么找一个正确通过的代码,编译并输入测试数据,查看其每一个命令的执行情 ...
- The Geodetic Set Problem UVA - 1198
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 数学 ( 解高次同余方程 )——Discrete Logarithm Problem ( UVA 7457 )
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- POJ 1207 The 3n + 1 problem
题目链接:http://poj.org/problem?id=1207 题目大意:给你一个数x,规定一个函数F(x),如果x为1则F(x)==1,否则如果x是偶数,F(x)==F(x/2),x为奇数F ...
- uva 524(Prime Ring Problem UVA - 524 )
dfs练习题,我素数打表的时候j=i了,一直没发现实际上是j=i*i,以后可记住了.还有最后一行不能有空格...昏迷了半天 我的代码(紫书上的算法) #include <bits/stdc++. ...
最新文章
- 2020人工神经网络第一次作业-参考答案第六部分
- Floyd-Warshall算法
- carsim8.02和matlab2016b的联合仿真,找不到carsim s-function的解决办法
- MyBaits 支持哪些数据源类型?
- c#中tcp协议服务器同时接收客户端的数据
- [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第51篇]什么是基于ID的加密的安全模型,描述一个IBE方案
- 数据结构 10分钟让你掌握经典排序(一)
- VNPY2.0火币期货交易接口配置使用
- 光复用技术中三种重要技术_【技术文章】X射线无损检测仪在锂电池行业中的重要应用...
- linux 找出僵尸进程,linux 查看僵尸进程
- 证券secuerity英语
- tomcat到底是干什么用的?白话理解
- 创建Web站点的欢迎页面
- mybatis where 标签使用
- WiFi大师终极版4.1.8独立源码
- LCD12864液晶显示
- 西方经济学微观部分中的英文字母缩写
- Android性能优化实践
- RPA技术在提升业务合规操作方面的作用及相关风险探讨
- zencart忘记后台密码的解决办法