zoukankan      html  css  js  c++  java
  • NYOJ 271

    题意:3*n+1问题,输出区间内的最大处理次数

    #include<stdio.h>
    int a[10001];
    void fun()
    {
    	int temp,cnt;int i,k=0;
    	for(i=1;i<=10001;i++)
    		{
    			temp=i;cnt=0;//cnt初始化必须在for内
    			while(temp!=1)
    			{
    				if(temp&1)
    					temp=3*temp+1;
    				else
    					temp>>=1;
    				cnt++;
    				//printf("%d\n",cnt);
    			}
    			a[k++]=cnt;//实际上k没必要,k相当于(i-1)		
    		}
    }
    int main()
    {
    	int m,n,max;int i,j;
    	fun();
    	while(~scanf("%d%d",&m,&n))
    	{
    		max=0;
    		for(i=m;i<=n;i++)
    			if(max<a[i-1])
    				max=a[i-1];			
    		printf("%d %d %d\n",m,n,max+1);
    	}
    	return 0;
    }
    
    //超时
    
    #include<stdio.h>
    int main()
    {
    	int m,n;int i,j;int max,temp,cnt;
    	while(~scanf("%d%d",&m,&n))
    	{
    		max=0;
    		for(i=m;i<=n;i++)
    		{
    			temp=i;cnt=0;//cnt初始化必须在for内
    			while(temp!=1)
    			{
    				if(temp&1)
    					temp=3*temp+1;
    				else
    					temp>>=1;
    				cnt++;
    			//	printf("%d\n",cnt);
    			}
    			if(max<cnt)
    			{
    				max=cnt;
    			//	printf("%d\n\n",max);
    			}
    		}
    		printf("%d %d %d\n",m,n,max+1);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    JS高级
    函数作用域面试题
    11.14
    11.13
    Redux知识
    react-router-dom
    react 的三大属性
    vuex
    数组的扩展
    函数作用域和 class
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2533330.html
Copyright © 2011-2022 走看看