zoukankan      html  css  js  c++  java
  • 红包算法设计

    最近在群里抢红包,抢的甚是开心,似乎这已经过时了,但发钱总是好的嘛,哈哈,熟悉了红包的规则,就自己编了个小程序,模拟下

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">

    window.onload=function()
    { var aResult=[];
    var dollar=document.getElementById("m");
    var count=document.getElementById("r");
    var star=document.getElementById("start");
    var oUl=document.getElementById("ull");
    star.onclick=ss;

    //查找所有的随机数
    function redbag(money,count)
    {
    var num=0;
    if(count==1)
    {
    aResult.push(money);


    return;
    }
    else
    {
    num=parseInt(Math.random()*money)+1;
    money=money-num;
    aResult.push(num);
    redbag(money,count-1);

    }


    }
    // 检验随机数是否都大于零
    function check()
    {
    var b=true;
    for(var i=0;i<aResult.length;i++)
    {
    if(aResult[i]>0)
    {

    continue;
    }
    else
    {
    b=false;
    break;
    }

    }
    return b;

    }
    //结合

    function ss()
    {
    aResult.length=0;
    redbag(dollar.value,count.value);
    check();
    while(!check())
    { aResult.length=0;
    redbag(dollar.value,count.value);
    check();

    }
    li=document.createElement("li");

    li.innerHTML=aResult;
    oUl.appendChild(li);


    }
    }

    </script>
    </head>

    <body>
    金额:<input type="text" id="m"/>
    红包数:<input type="text" id="r"/>
    <input type="button" value="发放" id="start"/>
    <ul id="ull">

    </ul>
    </body>
    </html>

    写的这个程序,复杂度高点,望大神给点指点哈

  • 相关阅读:
    Zepto跨域请求
    C语言中浮点数的比较
    IOS开源项目Telegram初试
    Java NIO初试
    ios8.0之后注册通知中心
    spring AOP的配置
    IOS学习日记-2014年10月25日
    struct框架中实体类中属性的类型错误问题
    Oracle存储过程使用DML语句问题
    跳出类似framset 嵌入式框架
  • 原文地址:https://www.cnblogs.com/wenyan/p/4424351.html
Copyright © 2011-2022 走看看