现我有规则如下:
如果买商品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个
如何求得我可能得到的赠品及数量?
各位高手请给出解?谢谢
如果买商品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个
如何求得我可能得到的赠品及数量?
各位高手请给出解?谢谢
解决方案 »
- 如何获取PPT总页数?
- WPF中如何或得焦点控件
- New ReportDocument时跳出程序的安装窗口?
- winform checkedlistbox的问题?
- TcpClient.ReceiveBufferSize 属性默认是 8192 ,如果返回的流数据大于这个值,是不是我就不能收到完整的数据,必须将ReceiveBufferSize数
- 如何用c#编程实现运行一个exe文件后,让其进程邦定一个cpu
- c#打开数据库中ole类型字段的问题
- 新手求助:往数据库里插入数据时,不成功,请教,急!谢谢~~~~
- 用C Sharp怎样实现软件启动画面?
- 怎样得到和设置一个已经打开的Excel文件中的单元格的值
- 代码实现CHM文件制作
- 求这样,有图(类式QQ分类的菜单)控件源码,或者编写方法
1*A+2*B=3是不是得D和E?
就是说有10个A10个B,1A+1B后,在组合就是9个A和9个B了吧
并且B的数量=2时,可以赠E 一个但是,如果你选择了赠品D,你要减去D所占的份额数量,
在看B的剩余数量还可否够,选择赠品E如果1*A+2*B=3
你选择了D,B的剩余数量是1,你就没有选择E的权利
{
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;
}
{
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
{
static void Main(string[] args)
{
Program p = new Program();
int a=2;
int b=3;
int c=4;
int[] def = p.ok(a, b, c);
foreach (int i in def)
{
Console.WriteLine(i);
}
Console.WriteLine("请选择D赠品数量"); //在这里没有作任何的越界判断和异常处理,因为我在做事,没太多时间。。
int d = int.Parse(Console.ReadLine());
if (d - def[0] == 0)
{
def[1] = 0;
}
else
{
d -= a / 2;
if (d > 0)
{
def[1] -= d;
}
}
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;
}
}晕。没看清题。改了改。。
你的算法中在哪能体现出以下规则:
如果你选择了赠品D,你要减去D所占的份额数量,
在看B的剩余数量还可否够,选择赠品E
{
static void Main(string[] args)
{
Program p = new Program();
int a=2;
int b=3;
int c=4;
int[] def = p.ok(a, b, c);
foreach (int i in def)
{
Console.WriteLine(i); //你可以选择的赠品 DEF
}
Console.WriteLine("请选择D赠品数量"); //在这里没有作任何的越界判断和异常处理,因为我在做事,没太多时间。。
int d = int.Parse(Console.ReadLine()); //在这里选择你所要的D赠品数量
if (d - def[0] == 0)
{
def[1] = 0;
}
else
{
d -= a / 2;
if (d > 0)
{
def[1] -= d;
}
}
foreach (int i in def)
{
Console.WriteLine(i); //你最后可以得到的赠品 DEF
}
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 a=2;
int b=3;
int c=4;
int[] def = p.ok(a, b, c);
foreach (int i in def)
{
Console.WriteLine(i); //你可以选择的赠品 DEF
}
Console.WriteLine("请选择D赠品数量"); //在这里没有作任何的越界判断和异常处理,因为我在做事,没太多时间。。
int d = int.Parse(Console.ReadLine()); //在这里选择你所要的D赠品数量
if (d - def[0] == 0)
{
def[1] = 0;
}
else
{
d -= a / 2;
if (d > 0)
{
def[1] -= d;
}
}
foreach (int i in def)
{
Console.WriteLine(i); //你最后可以得到的赠品 DEF
}
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;
}
}改好了,你看看是不是这样。干活去了。。
{
int[] def = new int[3];
def[0] = (a + b) / 2;
def[1] = b / 2;
def[2] = c / 2;
return def;
}
你这里是返回的赠品数量呢,还是可得的赠品种类?
{
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;
}
{
Dictionary<string, int> ret = new Dictionary<string, int>();
foreach (string largess in largessSerials)
{
ret[largess] = LargessCalc(favor, (Largess)Enum.Parse(typeof(Largess), largess));
} return ret;
}
{
int[] def = new int[3];
def[0] = (a + b) / 2;
def[1] = b / 2;
def[2] = c / 2;
return def;
}
你这里是返回的赠品数量呢,还是可得的赠品种类?------------------------------------------------------------------------------------
def数组代表三种赠品种类撒,而def[0]就代表d,以此类推撒。不好意思,昨天玩游戏去了=。=!!
B%2为E数量
C%2为F数量
列出总组合
如果用户选择D商品则先减去对应的A数量(如果黑店可以先减B不过为意义用户怒了你站点也就黄了)当A不够再减B操作结束后如上再计算剩下的
可能是我的能力问题,不知道我说的对否?
我发现你的程序有两个问题
1,我的问题中的商品只是个其中的例子,如果我有q,w,r,t....商品和规则要无限的算下去么?
2,你的程序我运行后,为什么只能有一种选择.
我的最终需求
需要一个更灵活和通用的算法!而不是仅仅针对这个例子! 谢谢
可能是我的能力问题,不知道我说的对否?
我发现你的程序有两个问题
1,我的问题中的商品只是个其中的例子,如果我有q,w,r,t....商品和规则要无限的算下去么?
2,你的程序我运行后,为什么只能有一种选择.
我的最终需求
需要一个更灵活和通用的算法!而不是仅仅针对这个例子! 谢谢============LZ没试我写的那个方法? 应该通用了吧
大兄弟实在抱歉,你用到了泛型,
可我的是vs2003,所以没试
(惭愧还在用老掉牙的东西!哎!)