Python CS50 贪婪算法的 C 翻译
Python translation from C for CS50 greedy algorithm
所以,我们都知道 CS50 是一个很大的 class 并且有很多东西需要学习。这是我现在遇到的 Python 的另一个问题。语法需要一点时间才能正确处理所有缩进更改,但逻辑似乎非常相似。在您输入 .99 甚至 1.20 之前,代码一直有效。但是,它很大,但是,我无法在 cloud9 ide 中使用 Python 进行调试......?同上。我这周才开始 Python,所以我确定这是一个面向语言的问题,我只需要解决这个问题。希望您能够帮助我。谢谢
#Greedy algorithm converted to python
import sys
import os
c = float(input("How much change is owed? "))
i = 0
while (c<0 or c==0):
print("Please input a positive amount...")
while (c>.24):
i += 1
c=(c-.25)
while (c>.1 or c==.1):
i += 1
c=(c-.1)
while (c>.05 or c==.05):
i += 1
c=(c-.05)
while (c>.01 or c==.01):
i += 1
c=(c-.01)
print("%i coin(s) needed to make the change." % i)
您可能 运行 遇到 float
的舍入问题。将其转换为美分并使用整数后,它有效:
#Greedy algorithm converted to python
import sys
import os
c = float(input("How much change is owed? "))
i = 0
while (c<0 or c==0):
print("Please input a positive amount...")
c = round(c * 100)
while (c>=25):
i += 1
c=(c-25)
while (c>=10):
i += 1
c=(c-10)
while (c>=5):
i += 1
c=(c-5)
while (c>=1):
i += 1
c=(c-1)
print("%i coin(s) needed to make the change." % i)
我的 C# 实现似乎已关闭,但请在下面查看它的 python 实现。
使用cs50的头文件.
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int greedyAlgorithm(float change);
//main
int main(void)
{
float change;
do
{
printf("What is the change owed\n");
change = get_float("$: ");
}while(change<0);
greedyAlgorithm(change);
}
//greedyAlgorithm implementation
int greedyAlgorithm(float change)
{
int coins[4] = {25, 10, 5, 1};
int amount, totalCoins, tmpNumber, remainder, quarterCounter, dimeCounter, nickelCounter, pennyCounter;
amount = round(change * 100);
tmpNumber = 0;
remainder = 0;
totalCoins = 0;
quarterCounter =0;
dimeCounter = 0;
nickelCounter = 0;
pennyCounter = 0;
if (amount >= coins[0])//quarter
{
remainder = amount % coins[0];
tmpNumber = amount - remainder;
totalCoins = totalCoins + (tmpNumber / coins[0]);
quarterCounter = tmpNumber / coins[0];
amount = remainder;
printf("Quarters: %i\n", quarterCounter);
}
if (amount >= coins[1])//dime
{
remainder = amount % coins[1];
tmpNumber = amount - remainder;
totalCoins = totalCoins + (tmpNumber / coins[1]);
dimeCounter = tmpNumber / coins[1];
amount = remainder;
printf("Dimes: %i\n", dimeCounter);
}
if (amount >= coins[2])//nickle
{
remainder = amount % coins[2];
tmpNumber = amount - remainder;
totalCoins = totalCoins + (tmpNumber / coins[2]);
nickelCounter = tmpNumber / coins[2];
amount = remainder;
printf("Nickels: %i\n", nickelCounter);
}
if (amount >= coins[3])//penny
{
remainder = amount % coins[3];
tmpNumber = amount - remainder;
totalCoins = totalCoins + (tmpNumber / coins[3]);
pennyCounter = tmpNumber / coins[3];
amount = remainder;
printf("Pennies: %i\n", pennyCounter);
}
printf("Total Coins: %i\n", totalCoins);
return 0;
}```
**Python equivalent**
#get user input
import math
def greedyAlgorithm(change):
coins = [25, 10, 5, 1] #coins denominations
tmpNumber = 0
remainder = 0
totalCoins = 0
quarterCounter =0
dimeCounter = 0
nickelCounter = 0
pennyCounter = 0
amount = round(int(change*100))
if amount >= coins[0]: #quarter
remainder = amount % coins[0]
tmpNumber = amount - remainder
totalCoins = totalCoins + (tmpNumber / coins[0])
quarterCounter = tmpNumber / coins[0]
amount = remainder
print(f"Quarters: {int(quarterCounter)}")
if amount >= coins[1]: #dime
remainder = amount % coins[1]
tmpNumber = amount - remainder
totalCoins = totalCoins + (tmpNumber / coins[1])
dimeCounter = tmpNumber / coins[1]
amount = remainder
print(f"Dimes: {int(dimeCounter)}")
if amount >= coins[2]: #nickle
remainder = amount % coins[2]
tmpNumber = amount - remainder
totalCoins = totalCoins + (tmpNumber / coins[2])
nickelCounter = tmpNumber / coins[2]
amount = remainder
print(f"Nickels: {int(nickelCounter)}")
if amount >= coins[3]:#penny
remainder = amount % coins[3]
tmpNumber = amount - remainder
totalCoins = totalCoins + (tmpNumber / coins[3])
pennyCounter = tmpNumber / coins[3]
amount = remainder
print(f"Pennies: {int(pennyCounter)}")
print(f"Total Coins: {int(totalCoins)}")
print("What is the change owed")
print("$: ", end="")
change = float(input())
greedyAlgorithm(change)
所以,我们都知道 CS50 是一个很大的 class 并且有很多东西需要学习。这是我现在遇到的 Python 的另一个问题。语法需要一点时间才能正确处理所有缩进更改,但逻辑似乎非常相似。在您输入 .99 甚至 1.20 之前,代码一直有效。但是,它很大,但是,我无法在 cloud9 ide 中使用 Python 进行调试......?同上。我这周才开始 Python,所以我确定这是一个面向语言的问题,我只需要解决这个问题。希望您能够帮助我。谢谢
#Greedy algorithm converted to python
import sys
import os
c = float(input("How much change is owed? "))
i = 0
while (c<0 or c==0):
print("Please input a positive amount...")
while (c>.24):
i += 1
c=(c-.25)
while (c>.1 or c==.1):
i += 1
c=(c-.1)
while (c>.05 or c==.05):
i += 1
c=(c-.05)
while (c>.01 or c==.01):
i += 1
c=(c-.01)
print("%i coin(s) needed to make the change." % i)
您可能 运行 遇到 float
的舍入问题。将其转换为美分并使用整数后,它有效:
#Greedy algorithm converted to python
import sys
import os
c = float(input("How much change is owed? "))
i = 0
while (c<0 or c==0):
print("Please input a positive amount...")
c = round(c * 100)
while (c>=25):
i += 1
c=(c-25)
while (c>=10):
i += 1
c=(c-10)
while (c>=5):
i += 1
c=(c-5)
while (c>=1):
i += 1
c=(c-1)
print("%i coin(s) needed to make the change." % i)
我的 C# 实现似乎已关闭,但请在下面查看它的 python 实现。 使用cs50的头文件.
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int greedyAlgorithm(float change);
//main
int main(void)
{
float change;
do
{
printf("What is the change owed\n");
change = get_float("$: ");
}while(change<0);
greedyAlgorithm(change);
}
//greedyAlgorithm implementation
int greedyAlgorithm(float change)
{
int coins[4] = {25, 10, 5, 1};
int amount, totalCoins, tmpNumber, remainder, quarterCounter, dimeCounter, nickelCounter, pennyCounter;
amount = round(change * 100);
tmpNumber = 0;
remainder = 0;
totalCoins = 0;
quarterCounter =0;
dimeCounter = 0;
nickelCounter = 0;
pennyCounter = 0;
if (amount >= coins[0])//quarter
{
remainder = amount % coins[0];
tmpNumber = amount - remainder;
totalCoins = totalCoins + (tmpNumber / coins[0]);
quarterCounter = tmpNumber / coins[0];
amount = remainder;
printf("Quarters: %i\n", quarterCounter);
}
if (amount >= coins[1])//dime
{
remainder = amount % coins[1];
tmpNumber = amount - remainder;
totalCoins = totalCoins + (tmpNumber / coins[1]);
dimeCounter = tmpNumber / coins[1];
amount = remainder;
printf("Dimes: %i\n", dimeCounter);
}
if (amount >= coins[2])//nickle
{
remainder = amount % coins[2];
tmpNumber = amount - remainder;
totalCoins = totalCoins + (tmpNumber / coins[2]);
nickelCounter = tmpNumber / coins[2];
amount = remainder;
printf("Nickels: %i\n", nickelCounter);
}
if (amount >= coins[3])//penny
{
remainder = amount % coins[3];
tmpNumber = amount - remainder;
totalCoins = totalCoins + (tmpNumber / coins[3]);
pennyCounter = tmpNumber / coins[3];
amount = remainder;
printf("Pennies: %i\n", pennyCounter);
}
printf("Total Coins: %i\n", totalCoins);
return 0;
}```
**Python equivalent**
#get user input
import math
def greedyAlgorithm(change):
coins = [25, 10, 5, 1] #coins denominations
tmpNumber = 0
remainder = 0
totalCoins = 0
quarterCounter =0
dimeCounter = 0
nickelCounter = 0
pennyCounter = 0
amount = round(int(change*100))
if amount >= coins[0]: #quarter
remainder = amount % coins[0]
tmpNumber = amount - remainder
totalCoins = totalCoins + (tmpNumber / coins[0])
quarterCounter = tmpNumber / coins[0]
amount = remainder
print(f"Quarters: {int(quarterCounter)}")
if amount >= coins[1]: #dime
remainder = amount % coins[1]
tmpNumber = amount - remainder
totalCoins = totalCoins + (tmpNumber / coins[1])
dimeCounter = tmpNumber / coins[1]
amount = remainder
print(f"Dimes: {int(dimeCounter)}")
if amount >= coins[2]: #nickle
remainder = amount % coins[2]
tmpNumber = amount - remainder
totalCoins = totalCoins + (tmpNumber / coins[2])
nickelCounter = tmpNumber / coins[2]
amount = remainder
print(f"Nickels: {int(nickelCounter)}")
if amount >= coins[3]:#penny
remainder = amount % coins[3]
tmpNumber = amount - remainder
totalCoins = totalCoins + (tmpNumber / coins[3])
pennyCounter = tmpNumber / coins[3]
amount = remainder
print(f"Pennies: {int(pennyCounter)}")
print(f"Total Coins: {int(totalCoins)}")
print("What is the change owed")
print("$: ", end="")
change = float(input())
greedyAlgorithm(change)