Ghostwriting C program, vending machine C program ghostwriting, ghostwriting C language program assignments

Problem Description

Assuming that there are [1,x1,x2,x3,...,xn] multiple currencies in a certain country, the vending machines in this country should follow a principle when making change - "the total number of change is the least". So, how do you write a program to help a vending machine automatically give
change ?

problem analysis

The most direct way to solve this problem is the exhaustive method. Suppose you need to change Y yuan, then through all the currencies less than Y, list all the solutions for change, and then compare which has the least total number of sheets. This
kind and the time complexity is extremely large.
An efficient solution to similar problems is to use the idea of ​​dynamic programming.

Minimum amount of currency needed to solve for change

Take currencies with denominations 1, 5, 10, 25 as an example:

 

The key to solving this problem is to use similar equations to keep reducing the size of the problem. When the question is narrowed down to "how many coins are needed to find 0 yuan", the answer to the question is obviously 0.
All we need to do extra is to create a list to hold requirements smaller than the change needs to be calculated.

# need_change is the amount to be changed,
# currency_list is the denomination list of the currency of the country,
# num_list is the minimum number of currencies to be changed, the length of num_list is at least (need_change+1)
def giveChange(need_change, currency_list, num_list):
for change in range(need_change+1): #Calculate the minimum number of currencies needed from 0
for currency in currency_list: # Traverse each currency
if (change-currency >= 0) and (num_list[change-currency]+1 <num_list[change]): #Calculate the minimum currency demand
num_list[change] = num_list[change-currency] + 1
return

def main():
need_change = 63
currency_list = [1,5,10,21,25]
num_list = list(range(need_change+1)) #Initialize num_list from 0 to need_change, a total of (need_change+1) number
giveChange( need_change, currency_list, num_list)
print("%d needs %d currency for change"%(need_change, num_list[need_change]))
if __name__ == "__main__": the result of
main()
is:

63 It takes 3 currencies to make change
It is not enough to output the correct amount of currency, we also need to output which denominations of currency.

Solving the problem of automatic change

As usual, the record of the shortest path. In order to output the change of the currency in which denominations need to be found, we need to record the currency used for the minimization of each step based on the previous step.
To do this, we need to add a list to record this value.

# need_change is the amount that needs to be changed,
# currency_list is the denomination list of the currency of the country,
# num_list is the minimum number of currencies that need to be changed, and the length of num_list is at least (need_change+1)
# used_list is the minimum number of currencies that need to be changed , the length is the same as num_list
def giveChange(need_change, currency_list, num_list, used_list):
for change in range(need_change+1): #Start from 0 to calculate the minimum number of currencies needed
for currency in currency_list: #Travel through each currency
if ( change-currency >= 0) and (num_list[change-currency]+1<=num_list[change]): #Calculate the minimum number of currency demand
num_list[change] = num_list[change-currency] + 1
used_list[change] = currency #Record the currency consumed
return

# 返回需要的货币
def showChange(need_change, used_list):
give_list = []
while need_change > 0:
give_list.append(used_list[need_change])
need_change -= used_list[need_change]
give_list.sort() #排序
return give_list

def main():
need_change = 64 #The amount of change needed
currency_list = [1,5,10,21,25] #The country's currency denomination list
num_list = list(range(need_change+1)) #Initialize num_list as 0 to need_change, total (need_change+1) number
used_list = list(range(need_change+1)) #Initialize used_list to 0 to need_change, total (need_change+1) number
giveChange(need_change, currency_list, num_list, used_list)
print ("%d needs %d currency for change"%(need_change, num_list[need_change]))
give_list = showChange(need_change, used_list)
print(give_list)

if __name__ == "__main__":
main()
evaluates to:

64 4 coins are needed for change
[1, 21, 21, 21]

http://www.daixie0.com/contents/13/1231.html

 

The core members of the team mainly include Silicon Valley engineers, BAT front-line engineers, top 5 master and doctoral students in China, and are proficient in German and English! Our main business scope is to do programming assignments, course design and so on.

 

Our field of direction: window programming, numerical algorithm, AI, artificial intelligence, financial statistics, econometric analysis, big data, network programming, WEB programming, communication programming, game programming, multimedia linux, plug-in programming program, API, image processing, embedded/MCU database programming, console process and thread, network security, assembly language hardware Programming software design engineering standards and regulations. The ghostwriting and ghostwriting programming languages ​​or tools include but are not limited to the following:

C/C++/C# ghostwriting

Java ghostwriting

IT ghostwriting

Python ghostwriting

Tutored programming assignments

Matlab ghostwriting

Haskell ghostwriting

Processing ghostwriting

Building a Linux environment

Rust ghostwriting

Data Structure Assginment

MIPS ghostwriting

Machine Learning homework ghostwriting

Oracle/SQL/PostgreSQL/Pig database ghostwriting/doing/coaching

web development, website development, website work

ASP.NET website development

Finance Insurance Statistics Statistics, Regression, Iteration

Prolog ghostwriting

Computer Computational method

 

Because professional, so trustworthy. If necessary, please add QQ: 99515681 or email: [email protected]   WeChat: codinghelp

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324577191&siteId=291194637