Sign in to follow this  
Followers 0
hallaplay835

Math problem

7 posts in this topic

#1 ·  Posted (edited)

Hi everyone, I need your help - again.

After thinking for a very long time, I still cannot find a way to code for this math problem.

Suppose you had 50000 euros, and you went to the market to buy fruit.

You can buy 5 types of fruit: apples, oranges, lemons, bananas and pears. The prices are as follow:

- apples cost 36 euros

- oranges cost 400 euros

- lemons cost 121 euros

- bananas cost 1225 euros

- pears cost 16129 euros

Your aim is to spend your money buying first the most expensive fruit until you can no longer buy it, then buy the second most expensive fruit and continue buying in this manner until you cannot afford to buy even the cheapest fruit.

In this example, you would buy 3 pears, 1 banana, 1 lemon and 7 apples. This would leave you with 15 euros.

Now how would you implement this way of thinking in AutoIt?

Suppose you had all of the prices of the fruits and the available money in separate variables, and a function which would subtract the price of your purchase from your available money.

Any ideas?

Thanks in advance.

Edited by hallaplay835

_____________________________________________________[size="2"][font="Arial"]"Pain is temporary, glory is forever."[/font][/size]

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

like this ?

#include <Array.au3>

Global $_Euros = 50000
Global $avArray[5][2] = [ ['apples', 36], ["oranges", 400], ["lemons", 121], ["bananas", 1225], ["pears", 16129]]

_ArraySort ( $avArray, 1, 0, 0, 1 ) ; sort by descending order.

For $_I = 0 To UBound ( $avArray ) -1
    ConsoleWrite ( "!->-- " & $avArray[$_I][0] & " : " & Int ( $_Euros/$avArray[$_I][1] ) & @Crlf )
    $_Euros = mod ( $_Euros, $avArray[$_I][1] )
Next

ConsoleWrite ( "+->-- $_Euros : " & $_Euros & @Crlf )

You are wrong : In this example, you would buy 3 pears, 1 banana, 3 lemons. This would leave you with 25 euros. Posted Image

Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

My solution (similar to wakillon's):

#include <Array.au3>

Global $fruits[5][2] = [["apples", 36], ["oranges", 400], ["lemons", 121],  ["bananas", 1225], ["pears", 16129]]
$euro = 50000
_ArraySort($fruits, 1, 0, 0, 1)

For $i = 0 To UBound($fruits) - 1
    ConsoleWrite(Floor($euro / $fruits[$i][1]) & "x " & $fruits[$i][0] & @CRLF)
    $euro -= Floor($euro /  $fruits[$i][1]) * $fruits[$i][1]
Next
ConsoleWrite(@CRLF & $euro & " € left." & @CRLF)

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

And if thats all you're getting for 50000 euros, you might want to consider using another market.

Life is expensive in Europe since the crisis ! Posted Image

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

like this ?

#include <Array.au3>

Global $_Euros = 50000
Global $avArray[5][2] = [ ['apples', 36], ["oranges", 400], ["lemons", 121], ["bananas", 1225], ["pears", 16129]]

_ArraySort ( $avArray, 1, 0, 0, 1 ) ; sort by descending order.

For $_I = 0 To UBound ( $avArray ) -1
    ConsoleWrite ( "!->-- " & $avArray[$_I][0] & " : " & Int ( $_Euros/$avArray[$_I][1] ) & @Crlf )
    $_Euros = mod ( $_Euros, $avArray[$_I][1] )
Next

ConsoleWrite ( "+->-- $_Euros : " & $_Euros & @Crlf )

You are wrong : In this example, you would buy 3 pears, 1 banana, 3 lemons. This would leave you with 25 euros. Posted Image

Thanks wakillon, this is perfect. This is what I was looking for. And yes, I was wrong: my math is poor.

My solution (similar to wakillon's):

#include <Array.au3>

Global $fruits[5][2] = [["apples", 36], ["oranges", 400], ["lemons", 121],  ["bananas", 1225], ["pears", 16129]]
$euro = 50000
_ArraySort($fruits, 1, 0, 0, 1)

For $i = 0 To UBound($fruits) - 1
    ConsoleWrite(Floor($euro / $fruits[$i][1]) & "x " & $fruits[$i][0] & @CRLF)
    $euro -= Floor($euro /  $fruits[$i][1]) * $fruits[$i][1]
Next
ConsoleWrite(@CRLF & $euro & " € left." & @CRLF)

Br,

UEZ

Thanks to you also, WEZ. This is also perfect.

And if thats all you're getting for 50000 euros, you might want to consider using another market.

I know it was not a realistic example, but I used it because it seemed much easier to think about this way than thinking about what I am really trying to do.

_____________________________________________________[size="2"][font="Arial"]"Pain is temporary, glory is forever."[/font][/size]

Share this post


Link to post
Share on other sites

I see you have no question about script,otherwise see helpfile for used functions : _ArraySort, Int, Mod Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0