Advertisement
Gologo

Untitled

Oct 20th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. read = 0
  2. coin_values =[]
  3.  
  4. # Быстрая сортировка массива
  5. def partition(arr,low,high):
  6.     i = ( low-1 )
  7.     pivot = arr[high]
  8.     for j in range(low , high):
  9.         if arr[j] <= pivot:
  10.             i = i+1
  11.             arr[i],arr[j] = arr[j],arr[i]  
  12.     arr[i+1],arr[high] = arr[high],arr[i+1]
  13.     return ( i+1 )
  14.  
  15.    
  16. def quickSort(arr,low,high):
  17.     if low < high:
  18.         pi = partition(arr,low,high)
  19.         quickSort(arr, low, pi-1)
  20.         quickSort(arr, pi+1, high)
  21. #
  22.  
  23.  
  24. def money(coins_list,change,min_coins,coins_used):
  25.     for c in range(change+1):
  26.         coin_count = c
  27.         new_coin = coins_list[0]
  28.         for j in coins_list:
  29.             if j <= c:
  30.                 if min_coins[c-j] + 1 < coin_count:
  31.                     coin_count = min_coins[c-j]+1
  32.                     new_coin = j
  33.             min_coins[c] = coin_count
  34.             coins_used[c] = new_coin
  35.     return min_coins[change]
  36.  
  37. def print_coins(coins_used,change):
  38.     coin = change
  39.     while coin > 0:
  40.         this_coin = coins_used[coin]
  41.         print(this_coin)
  42.         coin = coin - this_coin
  43.  
  44.  
  45.  
  46. #Ввод данных
  47. while True:
  48.     print("Введите номинал монеты. Для выхода введите stop : ")
  49.     read = input();
  50.    
  51.     if read.isdigit():
  52.         coin_values.append(int(read))
  53.     elif read == "stop":
  54.         break
  55.     else:
  56.         print("Неверное значение! Введите число или stop.")
  57.  
  58. while True:
  59.     print("Введите сумму : ")
  60.     read = input()
  61.    
  62.     if read.isdigit():
  63.         summ = int(read)
  64.         break
  65.     else:
  66.         print("Неверное значение! Введите число.")
  67.  
  68. #
  69.  
  70.  
  71. quickSort(coin_values,0,len(coin_values)-1)
  72. coins_used = [0]*(summ+1)
  73. coin_count = [0]*(summ+1)
  74.  
  75. print(" монет  : ",money(coin_values,summ,coin_count,coins_used))
  76. print_coins(coins_used,summ)
  77. print()
  78. print(coins_used)
  79. print(coin_count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement