标题:怎么在 Adobe Dreamweaver 写水仙花数代码啊?
只看楼主
事与愿违
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-3-6
结帖率:0
已结贴  问题点数:20 回复次数:1 
怎么在 Adobe Dreamweaver 写水仙花数代码啊?
求好心人帮忙
搜索更多相关主题的帖子: 水仙花 Adobe 
2012-03-06 18:06
yms123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:209
帖 子:12488
专家分:19042
注 册:2004-7-17
得分:20 
程序代码:
<html>
<head>
<title>计算水仙花数</title>
<script type="text/javascript">
     /*******************输出水仙花数的算法*******************************/
function Narcissus(s)    
{
    var i,j,k,n,m,l,reNum;
    reNum="";
    if (s==3)                  /*输出100-999的水仙花数*/
    {
        for(n=100;n<999;n++)
        {
            i = n / 100;
            j = n / 10 - i * 10;
            k = n % 10;
            if (n==Recursion(i,s) + Recursion(j,s) + Recursion(k,s))
                reNum+=n+"<br/>";
        }
    }
    else if (s == 4)               /*输出1000-9999的水仙花数*/
    {
        for (n = 1000; n <= 9999; n++)
        {
            i = n / 1000;
            j = n / 100 - i * 10;
            k = n / 10 - i * 100 - j * 10;
            l = n % 10;
            if (n == Recursion(i, s) + Recursion(j, s) + Recursion(k, s) + Recursion(l, s))
                reNum+=n+"<br/>";
        }
    }
    else if(s==5)                     /*输出10000-99999的水仙花数*/
    {
        for (n = 10000; n <= 99999; n++)
        {
            i = n / 10000;
            j = n / 1000 - i * 10;
            k = n / 100 - i * 100 - j * 10;
            l = n / 10 - i * 1000 - j * 100 - k * 10;
            m = n % 10;
            if (n == Recursion(i, s) + Recursion(j, s) + Recursion(k, s) + Recursion(l, s) + Recursion(m, s))
                reNum+=n+"<br/>";
        }
    }
    else if (s ==34)                 /*输出100-9999的水仙花数*/
    {
        for (n = 100; n <= 9999; n++)
        {
            i = n / 1000;
            j = n / 100 - i * 10;
            k = n / 10 - i * 100 - j * 10;
            l = n % 10;
            if (i != 0 && n==Recursion(i, 4) + Recursion(j, 4) + Recursion(k, 4) + Recursion(l, 4))
                reNum+=n+"<br/>";
            if (n== Recursion(j, 3) + Recursion(k, 3) + Recursion(l, 3))
                reNum+=n+"<br/>";
        }
    }
    else if(s == 45)                 /*输出1000-9999的水仙花水*/
    {
        for (n = 1000; n <= 99999; n++)
        {
            i = n / 10000;
            j = n / 1000 - i * 10;
            k = n / 100 - i * 100 - j * 10;
            l = n / 10 - i * 1000 - j * 100 - k * 10;
            m = n % 10;
            if (i != 0 && n == Recursion(i, 5) + Recursion(j, 5) + Recursion(k, 5) + Recursion(l, 5) + Recursion(m, 5))
                reNum+=n+"<br/>";
            if (n == Recursion(j, 4) + Recursion(k, 4) + Recursion(l, 4) + Recursion(m, 4))
                reNum+=n+"<br/>";
                    
        }
    }
    else                         /* 输出100-99999的水仙花数*/
    {
        for (n = 100; n <= 99999; n++)
        {
            i = n / 10000;
            j = n / 1000 - i * 10;
            k = n / 100 - i * 100 - j * 10;
            l = n / 10 - i * 1000 - j * 100 - k * 10;
            m = n % 10;
            if (i != 0 && n == Recursion(i, 5) + Recursion(j, 5) + Recursion(k, 5) + Recursion(l, 5) + Recursion(m, 5))
                reNum+=n+"<br/>";
            else if (j != 0 && n == Recursion(j, 4) + Recursion(k, 4) + Recursion(l, 4) + Recursion(m, 4))
                reNum+=n+"<br/>";
            else if (n== Recursion(k, 3) + Recursion(l, 3) + Recursion(m, 3))
                reNum+=n+"<br/>";
        }
    }
    document.getElementById("Result").innerHTML=reNum;
}
/*********************用递归求数n的k次方*******************************/
function Recursion(n,k)
{
    var m;
    if (k == 1) 
            return n;
    else
            m=n*Recursion(n,k-1);
    return m;
}        
</script>
<script type="text/javascript">
function SelMedian_Change(Val)
{
    Narcissus(parseInt(Val));
}
</script>
</head>
<body>
  <select name="SelMedian" onchange="SelMedian_Change(this.options[this.selectedIndex].value);" >
    <option value="3">3----找出三位数所有的水仙花数!</option>
    <option value="4">4----找出四位数所有的水仙花数!</option>
    <option value="5">5----找出五位数所有的水仙花数!</option>
    <option value="34">34---你选择找出三、四位数的水仙花数!</option>
    <option value="45">45---你选择找出四、五位数的水仙花数!</option>
    <option value="35">35---你选择找出三、四、五位数的水仙花数!</option>
  </select>请选择位数<br/>
  输出结果
  <div id="Result">
  </div>
</body>
</html>
2012-03-08 17:40



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-362379-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.046653 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved