我构建了以下硬币更改(C#),它完美地运行:
class Program
{
static int amount = 0;
static void Main(string[] args)
{
EnterAmount();
int[] coins = new int[] { 500, 100, 50, 20, 10, 5, 2, 1 };
int Results = 0;
for (int i = 0; i < coins.Length; i++)
{
Results = amount / coins[i];
Console.WriteLine(Results + " x " + coins[i]);
amount -= Results * coins[i];
}
}
static void EnterAmount()
{
Console.Out.WriteLine("Enter an amount : ");
amount = int.Parse(Console.ReadLine());
}
}
我的问题是我不知道如何限制每枚硬币的硬币数量 . 例如,我想要最多4个硬币500欧元,6个硬币10欧元,5个硬币2欧元等等 . 硬币更换返回每个硬币使用的硬币数量真是太棒了 .
你能帮我吗?谢谢 .
2 回答
请注意,此代码只是回答了关于谁限制您的硬币数量的问题,但您的算法并不完整,因为您没有考虑很多极端情况 .
UPD 这个解决方案效率很低,但我想它可以解决你的问题 . 您可以将其作为参考并自行改进 .
我认为这将有所帮助: