CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2005
    Posts
    28

    Trick Question apple/orange/watermelon

    Don't know whether this is the right place to ask. Somebody ask me this and i can't figure it out.

    anyone?

    1 apple= 10 cent
    1 orange = 30 cent
    1 watermelon =$7

    How do i buy 100 items for $100

  2. #2
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Trick Question apple/orange/watermelon

    Hi,

    You can write that as a linear diophantine equation, then google for solutions.

    Alternatively, ask whether you can buy the melon in slices.

    Alan

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Trick Question apple/orange/watermelon

    Personnaly, I could not eat 12 watermelons.

  4. #4
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Trick Question apple/orange/watermelon

    At those prices though, if you don't get those 12 watermelons you'll end up with a lot of apples & oranges, could be even more difficult to get through. Even 52 apples is a lot for one person, I think a fruit salad is the best plan.

  5. #5
    Join Date
    Mar 2005
    Posts
    28

    Re: Trick Question apple/orange/watermelon

    Kind of frustrating when this question is lingering on mind.

    In terms of programming or in terms of maths,i can't really figure it out.

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Trick Question apple/orange/watermelon

    Solve it like any other "word problem" in algebra ...

    Code:
    let x = number of apples
    let y = number of oranges
    let z = number of watermellons
    
    Equations:
    
    (eq 1) :      x +      y +      z = 100
    (eq 2) : 0.10*x + 0.30*y + 7.00*z = 100
    
    Other restrictions:
    
    (r 1) : 0 <= x <= 100
    (r 2) : 0 <= y <= 100
    (r 3) : 0 <= z <= 100
    (r 4) : x , y, and z must be integers
    
    
    For ease : multiply both sides of (eq 2) by 10
    
    (eq 1) :      x +      y +    z =  100
    (eq 2) :      x +    3*y + 70*z = 1000
    
    2 equations ... 3 unknowns
    
    solve for x in (eq 1) and substitute into (eq 2)
    
    (eq 1) : x = 100 - y - z
    (eq 2) : (100-y-z) + 3*y + 70*z = 1000
    
    simplify (eq 2)
    
    (eq 2) : 2*y + 69*z + 100 = 1000
    
    solve for y in equation 2
    
    (eq 2) : y = (900 - 69*z) / 2
    
    brute force :
    
    set z =  0 ... yields y = 450 (too big)
    set z =  1 ... yields y = 415.5 (too big and not an integer)
    set z =  2 ... yields y = 381 (too big)
    set z =  4 ... yields y = 312 (too big)
    set z =  6 ... yields y = 243 (too big)
    set z =  8 ... yields y = 174 (too big)
    set z = 10 ... yields y = 105 (too big)
    set z = 12 ... yields y =  36
    
    check :
    
    z = 12 , y = 36 ---> x = 52  (from eq 1)
    
    eq 2 : 52 + 3*36 + 12*70 must equal 1000

  7. #7
    Join Date
    Mar 2005
    Posts
    28

    Smile Re: Trick Question apple/orange/watermelon

    -U r a genius-

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured