Write C program, vending machine C program generation, Generation C language Program work

Source: Internet
Author: User

Problem description

Assuming that there are multiple currencies [1,x1,x2,x3,..., xn] in a country, the country's vending machine will follow a principle of finding zero-"The total number of change is the least". So, how to write programs that help the vending machine automatically
Where's the change?

Problem analysis

The most direct way to solve this problem is to be exhaustive. Suppose you need change Y, then all the options for change are listed through all the currencies less than Y, and then the least total number is compared. This kind of thinking needs to be calculated
There is a great deal of repetition and time complexity.
An effective solution to a similar problem is to use the idea of dynamic programming to deal with it.

The minimum number of currencies needed to solve the change

For example, a currency with a 1,5,10,25 value:

The key to solving this problem is to use similar equations to shrink the scale of the problem. When the problem narrowed to "how many currencies are needed to find $0", the answer to the question is clearly 0.
What we need to do extra is to create a list of requirements that are smaller than the change needs to be calculated.

# Need_change for the amount that needs change,
# currency_list is a list of the denominations of the country's currency,
# num_list is the minimum amount of money needed for change, num_list length is at least (need_change+1)
def givechange (Need_change, Currency_list, num_list):
For change in range (need_change+1): #从0开始计算最少需要的货币数
For currency in Currency_list: #遍历每一种货币
if (change-currency >= 0) and (Num_list[change-currency]+1<num_list[change]): #计算最少货币需求数
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)) #初始化num_list为0到need_change, Total (need_change+1)
Givechange (Need_change, Currency_list, Num_list)
Print ("%d requires%d currency for change"% (Need_change, Num_list[need_change]))
if __name__ = = "__main__":
Main ()
The result of the operation is:

63 need 3 Currency for change
Just outputting the correct number of currencies is not enough, and we also need to export the specific denominations of the currency.

Solve the problem of automatic change

As is common, the shortest path is the same as the record. In order to output the change of the currency in which the face value is to be found, we need to record the minimum used currency for each step based on the previous step.
To do this, we need to add a list to record this value.

# Need_change for the amount that needs change,
# currency_list is a list of the denominations of the country's currency,
# num_list is the minimum amount of money needed for change, num_list length is at least (need_change+1)
# used_list is the minimum number of currencies that need change, the same length as Num_list
def givechange (Need_change, Currency_list, Num_list, used_list):
For change in range (need_change+1): #从0开始计算最少需要的货币数
For currency in Currency_list: #遍历每一种货币
if (change-currency >= 0) and (Num_list[change-currency]+1<=num_list[change]): #计算最少货币需求数
Num_list[change] = num_list[change-currency] + 1
Used_list[change] = Currency #记录消耗的货币
Return

# Return the required currency
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 = #需要找零的钱数
Currency_list = [1,5,10,21,25] # List of currency denominations of the country
Num_list = List (range (need_change+1)) #初始化num_list为0到need_change, Total (need_change+1)
Used_list = List (range (need_change+1)) #初始化used_list为0到need_change, Total (need_change+1)
Givechange (Need_change, Currency_list, Num_list, Used_list)
Print ("%d requires%d currency for change"% (Need_change, Num_list[need_change]))
Give_list = Showchange (Need_change, Used_list)
Print (give_list)

if __name__ = = "__main__":
Main ()
The result of the calculation is:

64 need 4 Currency for change
[1, 21, 21, 21]

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

The core staff of the team mainly include Silicon Valley engineers, bat front-line engineers, domestic TOP5 master, PhD students, proficient in German English! Our main business scope is to do programming big homework, curriculum design and so on.

Our Direction field: Window Programming numerical algorithm AI Artificial Intelligence financial statistical Metrology analysis Big Data network programming Web programming Communication Programming game Programming Multimedia Linux plug-in programming API image processing embedded/Microcontroller database programming console process and thread Network security assembly language Hardware programming software Design Engineering Standard Rules. The generation of programming languages or tools including, but not limited to, the following ranges:

C/c++/c# Write

Java Write generation

It generation

Python writes

Tutoring Programming Jobs

The MATLAB Generation writes

Haskell writes

Processing Write

Linux Environment Setup

Rust Generation Write

Data Structure assginment Data structure generation

MIPS Generation Writing

Machine Learning Job Writing

Oracle/sql/postgresql/pig database Generation/Generation/Coaching

Web development, Web development, Web site jobs

Asp. NET Web site development

Finance insurace Statistics Statistics, regression, iteration

Prolog write

Computer Computational Method Generation

Because of professional, so trustworthy. If necessary, please add qq:99515681 or e-mail:[email protected] : Codinghelp

Write C program, vending machine C program generation, Generation C language Program work

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.