c语言贪心算法找零钱,数据结构--贪心算法解决找零钱问题 这里用的是人民币()...

// 实验小结 吴新强于2013年3月20日0:19:08 桂电 2507实验室

// 贪心算法解决找零钱问题  这里用的是人民币()

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

namespace Greedy_Algorithm

{

class Program

{

static void Main()

{

double origAmount = 0.36;  //给了100百,需要找63块的算法(假设不存在几分的)

double toChange = origAmount * 100;

double remainAmount = 0.0;

int[] coins = new int[6];

MakeChange(origAmount, remainAmount, coins);

Console.WriteLine("提示:请输入小于100元的值,找零大于100是没意义的!");

Console.WriteLine();

Console.WriteLine("最优找零方法如下:" + toChange);

Console.WriteLine();

//origAmount = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("找零的人民币是: ");

Console.WriteLine();

ShowChange(coins);

}

static void MakeChange(double origAmount, double remainAmount, int[] coins)

{

if ((origAmount % 0.5) < origAmount)

{

coins[8] = (int)(origAmount / 0.5);

remainAmount = origAmount % 0.5;

origAmount = remainAmount;

}

if ((origAmount % 0.2) < origAmount)

{

coins[7] = (int)(origAmount / 0.2);

remainAmount = origAmount % 0.2;

origAmount = remainAmount;

}

if ((origAmount % 0.1) < origAmount)

{

coins[6] = (int)(origAmount / 0.1);

remainAmount = origAmount % 0.1;

origAmount = remainAmount;

}

if ((origAmount % 0.05) < origAmount)

{

coins[5] = (int)(origAmount / 0.05);

remainAmount = origAmount % 0.05;

origAmount = remainAmount;

}

if ((origAmount % 0.02) < origAmount)

{

coins[4] = (int)(origAmount / 0.02);

remainAmount = origAmount % 0.02;

origAmount = remainAmount;

}

if ((origAmount % 0.01) < origAmount)

{

coins[3] = (int)(origAmount / 0.01);

remainAmount = origAmount % 0.01;

}

if ((origAmount % 0.005) < origAmount)

{

coins[2] = (int)(origAmount / 0.005);

remainAmount = origAmount % 0.005;

}

if ((origAmount % 0.002) < origAmount)

{

coins[1] = (int)(origAmount / 0.002);

remainAmount = origAmount % 0.002;

}

if ((origAmount % 0.001) < origAmount)

{

coins[0] = (int)(origAmount / 0.001);

remainAmount = origAmount % 0.001;

}

}

static void ShowChange(int[] arr)

{

if (arr[8] > 0)

Console.WriteLine("50元的人民币: " + arr[8] + "张 ");

if (arr[7] > 0)

Console.WriteLine("20元的人民币: " + arr[7] + "张");

if (arr[6] > 0)

Console.WriteLine("10元的人民币: " + arr[6] + "张");

if (arr[5] > 0)

Console.WriteLine("5元的人民币: " + arr[5] + "张");

if (arr[4] > 0)

Console.WriteLine("2元的人民币: " + arr[4] + "张");

if (arr[3] > 0)

Console.WriteLine("1元的人民币: " + arr[3] + "张");

if (arr[2] > 0)

Console.WriteLine("5角的人民币: " + arr[2] + "张");

if (arr[1] > 0)

Console.WriteLine("2角的人民币: " + arr[1] + "张");

if (arr[0] > 0)

Console.WriteLine("1角的人民币: " + arr[0] + "张");

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值