HDU 1032 [The 3n + 1 problem] 暴力模拟

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032

题目大意:给出i,j,要求输出i j之间“3N+1”的循环次数的最大值。

关键思想:暴力,模拟。可以优化,因为某些大数在进行操作时,会变为已经求过的小数,之后的循环次数已求过。

代码如下:

//between i,j  模拟,暴力
#include <iostream>
#include <algorithm>
using namespace std;
int i,j;
int main(){
	long long MAX,temp,cnt;
	while(cin>>i>>j){
		MAX=0;
		int s=min(i,j),l=max(i,j);//输入i可以大于j
		for(int a=s;a<=l;a++){
			cnt=1;
			temp=a;
			while(temp>1){
				if(temp%2==1){
					temp=3*temp+1;
					temp/=2;
					cnt+=2;
				}else{
					temp/=2;
					cnt++;
				}
			}
			MAX=max(cnt,MAX);
		}
		cout<<i<<" "<<j<<" "<<MAX<<endl;
	}
	return 0;
}

  

时间: 2024-08-06 11:41:02

HDU 1032 [The 3n + 1 problem] 暴力模拟的相关文章

hdu 1032 The 3n + 1 problem (打表)

The 3n + 1 problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26353    Accepted Submission(s): 9784 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032 Problem Description Problems in Comp

HDU 1032.The 3n + 1 problem【注意细节】【估计数据不强】【8月21】

The 3n + 1 problem Problem Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classificat

HDU 1032 The 3n + 1 problem【递归】

/* 中文题目 3n+1问题 中文翻译-大意 当一个数n是奇数的时候变成3*n+1,为偶数的时候变成n/2 解题思路:区间内每个数逐个求解 难点详解:如何逐个计算每个数的次数,选用while循环,还有就是将此时的 i 值赋给data,用于while循环的条件.最后再将这一个数运算次数累加在一起 关键点:理解题意 解题人:lingnichong 解题时间:2014-06-03 09:40:39 解题体会:没有告诉你那个大那个小,要先比较一下数,是个陷阱 */ The 3n + 1 problem

HDU 1032 The 3n + 1 problem (这个题必须写博客)

The 3n + 1 problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22148    Accepted Submission(s): 8276 Problem Description Problems in Computer Science are often classified as belonging to a c

HDU 1032.The 3n + 1 problem【注意细节】【预计数据不强】【8月21】

The 3n + 1 problem Problem Description Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classificat

题解报告:hdu 1032 The 3n + 1 problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032 解题思路:这道题的意思就是输入一个区间i,j找出这里面的最长周期:周期是这样子:当这个数(不为1)是奇数时就变为3*n+1,为偶数时就变为n/2,并且用sum来计数周期,直到n为1就跳出.(水题!!!注意杭电oj出题的一些坑) AC代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 int a,b,s

hdu 5641 King&#39;s Phone(暴力模拟题)

Problem Description In a military parade, the King sees lots of new things, including an Andriod Phone. He becomes interested in the pattern lock screen. The pattern interface is a 3×3 square lattice, the three points in the first line are labeled as

HDU 4858 项目管理(邻接表 暴力模拟)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4858 我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的! 两个节点间可能有多条边,不过一条边的两端必然是不同的节点.每个节点都有一个能量值. 现在我们要编写一个项目管理软件,这个软件呢有两个操作:1.给某个项目的能量值加上一个特定值.2.询问跟一个项目相邻的项目的能量值之和.(如果有多条边就算多次,比如a和b有2条边,那么询问a的时候b的权值算2次). 解题报告:这个

(HDU/UVA)1032/100--The 3n + 1 problem(3n+1问题)

描述计算机科学中的问题通常被归类为属于某一类问题(例如,NP,不可解,递归).在这个问题中,您将分析算法的属性,该算法的分类对于所有可能的输入都是未知的. 考虑下面的算法: 1.输入n 2.输出n 3.如果n = 1,则停止 4.如果n是奇数,则n=3n + 1 5.否则n=n / 2 6.返回第2步 给定输入22,将输出以下序列的数字22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 据推测,使用上述算法任何输入值将终止(当输出1时).尽管算法简单,但是不知道