using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace huanqian
{
public
class jisuan
{
public int i;
public int n ;
publicjisuan(int i, int n)
{
this.i = i;
this.n = n;
}
public int t=0;
public int GreedyChange( ref int[] c,double m , double[]s)
{
while (m != 0 && i >= 0)
{
if (ss[i + 1] && c[i + 1] != 0)
{
m -= s[i + 1];
n++;
i++;
c[i + 1]--;
Console.WriteLine("这步中m从{0}变到了{1},表示商店找了{2}元,客户应该找给商店{3}元!", s[i + 1] - m, m, s[i + 1], m);
}
elseif (m == s && c != 0)
{
m = s;
n++;
c--;
Console.WriteLine("这步中m从{0}变到了{1},表示客户找了{2}元,商店应该找给客户{3}元!", s - m, m, s, m);
}
//else if (m <= s[i-1] && c[i-1] != 0)
//{
//
m=s[i-1]-m;
//
n++;
//}
i--;
}
return n;
}
}
public class s
{
public static void Main()
{
int [] c=new int[]{2,4,2,2,1,0};
double [] s = new double[] {0.05,0.1,0.2,0.5,1,2};
int i = 4;
int n = 0;
jisuan s1 = new jisuan(i, n);
double m=0.55;
Console.WriteLine("开始时硬币个数n为:{0},客户应付款:{1}元", s1.n,m);
Console.WriteLine("开始计算.....:");
int j = s1.GreedyChange(ref c, m, s);
Console.WriteLine("使用到的硬币数目是:{0}",j);
Console.ReadKey();
}
}
}
这个小程序模拟换钱,交换过程是一步一步的。
比如:1.你买了0.55元的商品,你可以支付店家1块,店家找你0.45元;
2.店家给了你0.5元,你该找店家0.05元;
3.你有0.05元,你直接找给店家,整个交易完成,这整个过程中,使用到3块硬币!
|