×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

有会这个算法的吗

本文发表在 rolia.net 枫下论坛We have a string of digits, find the minimum number of additions required for the string to equal some target number. Each addition is the equivalent of inserting a plus sign somewhere into the string of digits. After all plus signs are inserted, evaluate the sum as usual. For example, consider the string "12" . With zero additions, we can achieve the number 12. If we insert one plus sign into the string, we get "1+2", which evaluates to 3. So, in that case, given "12", a minimum of 1 addition is required to get the number 3. As another example, consider "303" and a target sum of 6. The best strategy is not "3+0+3", but "3+03". You can do this because leading zeros do not change the result.

Write a class SimpleSums that contains the method minMSums, which takes a String numbers and an int sum. The method should calculate and return the minimum number of additions required to create an expression from numbers that evaluates to sum. If this is impossible, return -1.
Examples
0)
"99999"

45


Returns: 4

In this case, the only way to achieve 45 is to add 9+9+9+9+9. This requires 4 additions.

1)
"1110"
3
Returns: 3

Be careful with zeros. 1+1+1+0=3 and requires 3 additions.
2)
"0123456789"
45
Returns: 8

3)
"99999"
100

Returns: -1
4)
"382834"
100

Returns: 2

There are three ways to get 100. They are 38+28+34, 3+8+2+83+4 and 3+82+8+3+4. The minimum required is 2.
5)
"9230560001"
71

Returns: 4更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / 有会这个算法的吗
    本文发表在 rolia.net 枫下论坛We have a string of digits, find the minimum number of additions required for the string to equal some target number. Each addition is the equivalent of inserting a plus sign somewhere into the string of digits. After all plus signs are inserted, evaluate the sum as usual. For example, consider the string "12" . With zero additions, we can achieve the number 12. If we insert one plus sign into the string, we get "1+2", which evaluates to 3. So, in that case, given "12", a minimum of 1 addition is required to get the number 3. As another example, consider "303" and a target sum of 6. The best strategy is not "3+0+3", but "3+03". You can do this because leading zeros do not change the result.

    Write a class SimpleSums that contains the method minMSums, which takes a String numbers and an int sum. The method should calculate and return the minimum number of additions required to create an expression from numbers that evaluates to sum. If this is impossible, return -1.
    Examples
    0)
    "99999"

    45


    Returns: 4

    In this case, the only way to achieve 45 is to add 9+9+9+9+9. This requires 4 additions.

    1)
    "1110"
    3
    Returns: 3

    Be careful with zeros. 1+1+1+0=3 and requires 3 additions.
    2)
    "0123456789"
    45
    Returns: 8

    3)
    "99999"
    100

    Returns: -1
    4)
    "382834"
    100

    Returns: 2

    There are three ways to get 100. They are 38+28+34, 3+8+2+83+4 and 3+82+8+3+4. The minimum required is 2.
    5)
    "9230560001"
    71

    Returns: 4更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • any performance concern?
    • Seems like dynamic programming
      • agree
    • 实际上又是一个排列组合的问题
    • 看着很象大学的作业啊.
      似乎不难啊,既然string digits的顺序不变,就没有排列的问题,单纯组合而已.甚至组合都不用,直接把从 0 到 字符长 - 1 个'+' 插入到不同位置,直到找到 target number. 我想得太简单了?
      • 自己实际的动手编编程序,就知道想得是不是太简单了
        • 恩.道理的确简单,实现随不容易,但至少有思路了.反正闲着.用JAVASCRIPT写写看.呵呵..
          • 用JAVASCRIPT写了个测试算法(其实也没什么算法).仅供参考而已.
            本文发表在 rolia.net 枫下论坛<HTML>
            <head>
            <script lang="javascript">
            var strIndex = "";
            var strInput;
            var targetNumber;
            var strTemp = "";
            var nInd = 0;

            function getNextNumber(N, a, b, m){
            for(var i=m; i<=N-(b-a+1); i++){
            strIndex += i;

            if(a==b){
            //Got the indexes
            document.getElementById("output").innerHTML += (strIndex + ": ");
            //insert '+' based on the indexes
            strTemp = strInput;
            for(var j=strIndex.length-1; j>=0; j--){
            nInd = parseInt(strIndex.charAt(j));
            strTemp = strTemp.substr(0, nInd) + '+' + strTemp.substring(nInd, strTemp.length);
            }
            document.getElementById("output").innerHTML += (strTemp + "<BR>");

            //
            if (eval(strTemp) == targetNumber) alert("Got it! " + strTemp + "=" + targetNumber + "\nResult: " + b);

            strIndex = strIndex.substr(0, strIndex.length-1);
            }else if(a<b){
            getNextNumber(N, a+1, b, i+1);
            }
            }

            strIndex = strIndex.substr(0, a-2);
            }

            function test(){
            document.getElementById("output").innerHTML ="";

            strInput = document.getElementById('txtString').value;
            targetNumber = parseInt(document.getElementById('txtNumber').value);

            for (var k=1; k<strInput.length; k++){
            getNextNumber(strInput.length, 1, k, 1);
            }
            }
            </script>
            </head>

            <body>
            <form>
            <table>
            <tr>
            <td>Digit String:<input type=text name=txtString id=txtString size=20></td>
            <td>Target Number:<input type=text name=txtNumber id=txtNumber size=20></td>
            </tr>
            <tr>
            <td>&nbsp;</td>
            <td><input type=button name=btnTest value=" Do It " onclick="test();"></td>
            </tr>
            <tr>
            <td colspan=10><div id=output style="width:200px; height:200px; overflow:auto"></div></td>
            </tr>
            </table>
            </form>
            </body>

            </HTML>更多精彩文章及讨论,请光临枫下论坛 rolia.net
            • 有个BUG: 056 会被认为是8进制, 仅供参考而已,所以凑和看吧..:)
    • 老师教导过,不CARE COMPLEXITY 叫办法,不叫算法。
    • 不考虑算法用递归没多少行
    • TopCoder has this kind of algorithm competitions. You can go there to practise.