1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. To chat with the GameOgre community, you need to have at least 100 posts. Once you have the 100 posts, post at Become A New Ogre
    Dismiss Notice

A program in pascal/java/c

Discussion in 'Online Game Development' started by SerbiaPhantom, Mar 27, 2015.

  1. SerbiaPhantom

    SerbiaPhantom Club Swinger

    Messages:
    65
    Likes Received:
    5
    Trophy Points:
    0
    Credit:
    18.02
    So my teacher told me if I do this question right,i would get an A :D .If anyone could write this program for me it would be amazing:
    A fly is jumping on an infinite string.For the first time it jumps 1cm in one direction.The second time she jumps 3cm in any direction(left or right),the third 5cm in any direction(left or right).Every time it jumps,the next time it will jump 2cm more in any direction.Is it possible for it to jump for x(input)times and on the x-th jump,it will land on the starting point(before the first jump).
    I would love you forever if you can write me this program.
     
  2. Croco12

    Croco12 The Crocodile The Pit
    1. TSM

    Messages:
    6,521
    Likes Received:
    670
    Trophy Points:
    113
    Credit:
    721.30
    I could do this, but in Basic... And... pm me with original text on Serbian, because I don't understand this well...
     
  3. I really doubt you need a solution at this point, but I'll provide a solution in Python anyway since it's a fun little exercise:

    Using recursion:
    Code:
    land_at_start = 0
    directions = []
    
    # explore all 2^x branches
    def explore_branches(moves, current_sum, path):
        global directions
        global land_at_start
       
        if not moves:
          if eval(path) == 0:
              directions.append(path)
              land_at_start = 1
          return
       
        # current sum = current sum + positive cm
        explore_branches(moves[1:], current_sum + moves[0], path + "+{}".format(moves[0]))
       
        # current sum = current sum - negative cm
        explore_branches(moves[1:], current_sum - moves[0], path + "-{}".format(moves[0]))
    
    def calculate_jump(x):
      moves = []
    
      # make an array of length x containing odd integers
      for i in range(1, x*2 + 1, 2):
        moves.append(i)
    
      # begin at sum = 0
      explore_branches(moves, 0, "")
     
      # print result
      if land_at_start == 1:
          print("Can land on start. Here are the possible moves:")
          print(*directions, sep="\n")
      else:
          print("Cannot land on start.")
    
    
    if __name__ == "__main__":
      # calculate the jump with x=4
      calculate_jump(4)
    
    And it looks like any even number x >= 6 can generate some path such that the sum = 0 (starting point).
     

Share This Page