• 一个简单的算法_应该是最笨的写法了 简单


    <?php
    
    header("Content-type: text/html; charset=utf-8");
    $count = 19;//5 9另外处理 
    
    
    $tempArray = array(2,3,4);
    $_template = array();
    
    if($count >= 2){
    	if(in_array($count, $tempArray)){
    	    array_push($_template, $count);
    	}else{
    		if($count == 5){
    		    array_push($_template, 2);
    		    array_push($_template, 3);
    		}else if($count == 9){
    		    array_push($_template, 4);
    		    array_push($_template, 2);
    		    array_push($_template, 3);
    		}else{
    			Calculate($_template, $count, $tempArray);
    		}	
    	}
    }else{ 
         array_push($_template, $count);
    }
    
    function Calculate(&$_template, $count, $tempArray)
    {
    	 shuffle($tempArray);
         while($count>=2)
         {
              $templateid = array_rand($tempArray,1);
              $templateValue = $tempArray[$templateid];
              #echo "模板值为:{$templateValue}<BR><BR>";
    
              if($count >= 2){
              	 if($count >= $templateValue){
    		          $count = $count - $templateValue;
    		          #echo "count:{$count}<BR><BR>"; 
    		          array_push($_template, $templateValue);
    		          //echo $templateid."<BR><BR>";          	 	
              	 }else{
                      array_push($_template, $count);
                      $count = 0;
              	 }
              }
         }
    
         if($count == 1){
         	$isZero = false;
            #echo "最后还有一个哦{$count}<BR><BR>";
            if(count($_template) >= 1){
                 foreach($_template as $key=>$val){
                     if($val <= 3 ){
                     	 #echo "最后一个处理完成";
                     	 $_template[$key] = $val+1;
                     	 $isZero = true;
                     	 break;
                     }
                 }
            }
            if($isZero == false){
            	#echo "<h1>只能添加一个模板了</h1>";
            	array_push($_template, $count);
            }
         }
    }
    
    echo "<BR><BR><BR>最后取得的模板数组为:<BR><BR>";
    var_dump($_template);
    
    
    ?>
    

      

  • 相关阅读:
    设计模式-外观模式
    发生死锁怎么办
    设计模式-工厂模式
    设计模式-模板方法
    设计模式-命令模式(Command)
    设计模式-单例模式
    设计模式-装饰器模式
    CQRS之旅——旅程8(后记:经验教训)
    CQRS之旅——旅程7(增加弹性和优化性能)
    CQRS之旅——旅程6(我们系统的版本管理)
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2822503.html
Copyright © 2020-2023  润新知