Need help for improvement.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AnonyNewbie
    New Member
    • Apr 2012
    • 6

    Need help for improvement.

    I need some help to improve my code.
    My code is duplicating the output, and the output format is not what I want.

    Here is the full Pseudo code.
    Code:
    int binary = 0;
    int sum;
    
    while(binary<256)
    sum = 0;
    for(go through all 8 digits)
        if the i-th digit is 0
            sum += i
        if the i-th digit is 1
            sum -= i
    end for
    
    if sum == 0
        output
    
    binary++
    end while
    Since sum, binary, and check is initialize as 0.

    I have written this code using the Pseudocode given above. But seems like my code will duplicate the output and one more problem, the format.

    I want the output be like this format :
    Code:
    Enter a number : 3
    
    -1 -2 +3 = 0
    1 +2 -3 = 0
    But my currently output is :
    Code:
    Enter a number : 3
    
    -1 -2 3 = 0
    1 2 -3 = 0
    Code:
        CODE IS REMOVED.
        PROBLEM OF FORMAT AND DUPLICATION OF OUTPUT ARE SOLVED
    The duplication is because of the binary < 256. As you draw the tree diagram out, I find out that 256 is equal to 2^8, which is also 256 possible solutions.
    So, if I enter "3" inside the program, the program will definitely loop from 1 to 3 only, but the program will continue to loop for all 256 solutions, which is from 1 to 9. So, for sure there will be same answer printed out.
    So, I change the looping to 2^n so that it will stop where I wanted.
    Thanks for the helps from all.
    Last edited by AnonyNewbie; Apr 13 '12, 12:46 PM. Reason: Problem Solved.
  • limweizhong
    New Member
    • Dec 2006
    • 62

    #2
    It's not clear what you mean by "duplicates ", but as for the output format, a positive number converted to a string will not have the + sign by default. If you wanted a +, you can add it yourself, like "+" + i + " ";

    Comment

    • AnonyNewbie
      New Member
      • Apr 2012
      • 6

      #3
      Originally posted by limweizhong
      It's not clear what you mean by "duplicates ", but as for the output format, a positive number converted to a string will not have the + sign by default. If you wanted a +, you can add it yourself, like "+" + i + " ";

      The actual answer is 10 times the answer in the example, meaning that the print out 10 times, 20 lines, and not 2 lines.
      And the "+" sign I did it before, it just add before 1 and stop adding to the others positive number.

      Comment

      • limweizhong
        New Member
        • Dec 2006
        • 62

        #4
        There are no repeats in the binary representation of numbers 0 to 255, so based on your "pseudo-code", there should be no repeats. It's probably not your output code then. Anyway if you did type out exactly string += "+" + i + " "; in line 9 of your output code it should have worked. Please try without assuming.

        Comment

        • AnonyNewbie
          New Member
          • Apr 2012
          • 6

          #5
          About the format, previously I put the "+" in wrong place, sorry for my fault.
          I have added my full code, can you help me check it?
          Thanks.

          Comment

          • limweizhong
            New Member
            • Dec 2006
            • 62

            #6
            You're not "[going] through all 8 digits" as you stated in the pseudo-code. If you go through only 3 digits, it will be only the last 3 digits of the binary numbers from 0 to 255 and thus this will contain many repeats, because the last 3 digits of 8n + k will be the same as k for all n. Moreover, if you input 9, your answer will be wrong. You must calculate the number to put in place of 256, i.e. 2 ^ input

            Comment

            • limweizhong
              New Member
              • Dec 2006
              • 62

              #7
              Also, this is a simple integral knapsack problem that also can be solved by dynamic programming.

              Comment

              • AnonyNewbie
                New Member
                • Apr 2012
                • 6

                #8
                Thanks for your helps.
                The pseudocode is given by someone replied my post at the answer.yahoo.co m, so i used some time to study it.
                I'm a very very new Java programmer, previously I just studied C. So, I'm a bit blur when see different things.
                I'm apologize for my disturb.

                Comment

                • AnonyNewbie
                  New Member
                  • Apr 2012
                  • 6

                  #9
                  By the way, user only allow to input <=9 as mentioned in the question I found online. advanced.pdf

                  Comment

                  Working...