• UVA 100 The 3n + 1 problem


    今天太困了,陪她下象棋。。真,,,,,

    /****************************************************************/

    The 3n + 1 problem
    Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

    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 classification is not known for all possible inputs.

    Consider the following algorithm:


        1.      input n

        2.      print n

        3.      if n = 1 then STOP

        4.           if n is odd then n <- 3n + 1

        5.           else n <- n / 2

        6.      GOTO 2


    Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

    It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)

    Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.

    For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.
     

    Input

    The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.

    You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.

    You can assume that no opperation overflows a 32-bit integer.
     

    Output

    For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).
     

    Sample Input

    1 10 100 200 201 210 900 1000
     

    Sample Output

    1 10 20 100 200 125 201 210 89 900 1000 174



    这题真的可以说是超级的水,我一开始想暴力过不了,简单点,要不就是优化的暴力,谁知道就是纯暴力也可以A,之前不A的原因是因为这一句“The integers i and j must appear in the output in the same order in which they appeared in the input”,就是说输入的i可能会大于j。最后还是按照原来的顺序输出。

    方法就是int x=i,y=j,if(i>j)swap(i,j);  ....最后printf("%d %d %d ",x,y,xxx);就OK了

    不过通过这题我也复习了一下map的简单用法,首先头文件包含include<map> 然后map<数据类型1, 数据类型2>变量名 如map<int,int>my_Map,或者map<string.map>myMap

    前者就是数组,第一个int是数组的下标,后一个int是数组里的数,后者就是用字符串当数组的下标,这种可能会用到。比如上次的hdu today那题。

    总结就是第一个是下标,第二个是数值。 map刚申请好的时候没赋值,里面都是0,可以用my_Map.clear把里面的数字都清零。这点也是有些题目里比数组方便的地方。比如这题数组的话不一定申请的那么大的内存。用map就很好。不会超内存,用到那个才申请空间。map跟hash应该能很好的配合。不过我还没有实战过。


    纯暴力代码(蠢的不能再蠢,但是可以过)

    #include<math.h>
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    int n,i,j,cnt,ma;
    
    int main()
    {
    	while(~scanf("%d%d",&i,&j))
    	{
    		int x=i,y=j;
    		if(i>j)swap(i,j);
    		ma=cnt=1;
    		for(n=i;n<=j;n++)
    		{
    			int m=n;
    			cnt=1;
    			while(m>1)
    			{
    				if(m&1)
    				{
    					m=m*3+1;
    					cnt++;
    				}
    				else
    				{
    					m=m/2;
    					cnt++;
    				}
    			}
    			if(cnt>ma)
    			{
    				ma=cnt;
    			}
    		}
    		printf("%d %d %d
    ",x,y,ma);
    	}
    
    
    
    
    
        return 0;
    }
    

    用map优化,算是hash吧,就是计算过的不重复计算

    #include<queue>
    #include<map>
    #include<math.h>
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    map<int,int>my_Map;
    
    int n,i,j,ma,cnt;
    
    int main()
    {
    	my_Map[1]=1;
    	my_Map[2]=2;
    	my_Map[4]=3;
    	while(~scanf("%d%d",&i,&j))
    	{
    		int x=i,y=j;
    		if(i>j)swap(i,j);
    		ma=cnt=0;
    		for(int n=i;n<=j;n++)
    		{
    			int m=n;
    			cnt=0;
    			while(m>1)
    			{
    				if(my_Map[m])
    				{
    					cnt=cnt+my_Map[m];
    					break;
    				}
    				else
    				{
    					if(m&1)
    					{
    						m=m*3+1;
    						cnt++;
    					}
    					else
    					{
    						m=m/2;
    						cnt++;
    					}
    				}
    			}
    			my_Map[n]=cnt;
    			ma=max(cnt,ma);
    		}
    		printf("%d %d %d
    ",x,y,ma);
    	}
        return 0;
    }
    
    
    







    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    LintCode "Binary Tree Serialization"
    LeetCode "Find the Duplicate Number"
    LintCode "Route Between Two Nodes in Graph"
    LintCode "Search a 2D Matrix II"
    LintCode "Submatrix Sum"
    LintCode "Sort Letters by Case"
    LeetCode "Peeking Iterator"
    LintCode "Sort Colors II"
    LeetCode "Move Zeroes"
    LintCode "Update Bits"
  • 原文地址:https://www.cnblogs.com/wmxl/p/4662830.html
Copyright © 2020-2023  润新知