读书人

求算法!(急),该如何处理

发布时间: 2012-03-12 12:45:33 作者: rapoo

求算法!(急)
现我有规则如下:
如果买商品A+B任意组合数量等于2,则赠D一个
(可能1*A+1*B=2,2*A+0*B=2,0*A+2*B=2)
如果买商品B 数量等于2, 则赠E一个
如果买商品C 数量等于2, 则赠F一个

现我买A商品2个,B商品2个,C商品2个
如何求得我可能得到的赠品及数量?


各位高手请给出解?谢谢

[解决办法]
UP先~!
[解决办法]
没说明白
1*A+2*B=3是不是得D和E?
[解决办法]
组合一次后,A和B都要减去相应进行组合的数量吧.真拗口.
就是说有10个A10个B,1A+1B后,在组合就是9个A和9个B了吧
[解决办法]
路过,帮顶
[解决办法]
有几种可能 可以给选择让用户挑
[解决办法]
static void Main(string[] args)
{
Program p = new Program();
int[] def = p.ok(2, 3, 5);
foreach (int i in def)
{
Console.WriteLine(i);
}
Console.ReadKey();
}

private int[] ok(int a, int b, int c)
{
int[] def = new int[3];
def[0] = (a + b) / 2;
def[1] = b / 2;
def[2] = c / 2;
return def;
}
[解决办法]
static void Main(string[] args)
{
Program p = new Program();
int[] def = p.ok(2, 3, 5);
foreach (int i in def)
{
Console.WriteLine(i);
}
Console.ReadKey();
}

private int[] ok(int a, int b, int c)
{
int[] def = new int[3];
def[0] = (a + b) / 2;
def[1] = b / 2;
def[2] = c / 2;
return def;
}

是等于或大于是吧。。如果只是等于的话。那么就太简单了。。。直接减个2看是不是等于0。。。
[解决办法]
有以下可能:
1、3个D
2、2个D和1个E
3、2个D和1个F
4、1个D和1个E和1个F


[解决办法]
让用户自己选. 根据在去减
[解决办法]
public enum FavorFirstChoice
{
A,
B,
Unknown
}

public enum Largess
{
D,
E,
F
}
private Dictionary <string, int> _goods;

_goods = new Dictionary <string, int> ();
_goods.Add( "A ", 2);
_goods.Add( "B ", 2);
_goods.Add( "C ", 2);

Dictionary <string, int> test = LargessArithmetic(new string[] { "D ", "E ", "F " }, FavorFirstChoice.A);

private Dictionary <string, int> LargessArithmetic(string[] largessSerials, FavorFirstChoice favor)
{
Dictionary <string, int> ret = new Dictionary <string, int> ();
foreach (string largess in largessSerials)
{
MethodInfo vMethodInfo = this.GetType().GetMethod( "LargessCalc ", BindingFlags.Instance | BindingFlags.NonPublic);


int tmp = 0;
if (vMethodInfo != null)
{
tmp = (int)vMethodInfo.Invoke(this, new object[] { favor, (Largess)Enum.Parse(typeof(Largess), largess) });
}
ret[largess] = tmp;
}

return ret;
}

private int LargessCalc(FavorFirstChoice favor, Largess largess)
{
string methodName = largess.ToString() + "Largess ";
MethodInfo vMethodInfo = this.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
if (vMethodInfo != null)
{
return (int)vMethodInfo.Invoke(this, new object[] { favor });
}
else return 0;
}

private int DLargess(FavorFirstChoice favor)
{
int ret = 0;
int tmpA = 0;
int tmpB = 0;
if (_goods.ContainsKey( "A ")) tmpA = _goods[ "A "];
if (_goods.ContainsKey( "B ")) tmpB = _goods[ "B "];

if (favor == FavorFirstChoice.A)
{
while (tmpA > 1)
{
ret++;
tmpA -= 2;
}
tmpB += tmpA;
while (tmpB > 1)
{
ret++;
tmpB -= 2;
}
}
else if (favor == FavorFirstChoice.B)
{
while (tmpB > 1)
{
ret++;
tmpA -= 2;
}
tmpA += tmpB;
while (tmpA > 1)
{
ret++;
tmpA -= 2;
}
}

_goods[ "A "] = tmpA;
_goods[ "B "] = tmpB;

return ret;
}

private int ELargess(FavorFirstChoice favor)
{
int ret = 0;
int tmp = 0;
if (_goods.ContainsKey( "B "))
{
tmp = _goods[ "B "];
while (tmp > 1)
{
ret++;
tmp -= 2;
}
}

_goods[ "B "] = tmp;
return ret;
}

private int FLargess(FavorFirstChoice favor)
{
int ret = 0;
int tmp = 0;
if (_goods.ContainsKey( "C "))
{
tmp = _goods[ "C "];
while (tmp > 1)
{
ret++;
tmp -= 2;
}
}

_goods[ "C "] = tmp;
return ret;
}

读书人网 >C#

热点推荐