有N个人 N>=5 都要入场去三家店购物
入场时抽签决定顺序,要怎么样设计抽签法 让每个人三家店购物的顺序之和相同?
例:
A店 B店 C店 和
甲 4 3 2 9
乙 1 4 4 9
丙 3 5 1 9
丁 2 2 5 9
戊 5 1 3 9
以下提供别人写好的解法,但是我完全不懂为什么...
先随机决定A的顺序(customers[i].A)
var count = customers.length;
var half = (count % 2) ? count / 2 ; (count-1) / 2;
foreach(customers as customer){
if(count % 2 == 0){
if(customer.A <= half){
customer.B = count - 2 * (customer.B-1);
customer.C = customer.A + half;
}else{
customer.B = count - 1 - 2*(customer.A - half -1);
customer.C = customer.A - half;
}
}else{
if(customer.A <= half){
customer.B = count -1 - 2 * (customer.A -1);
customer.C = customer.A + half +1;
}else{
customer.B = count - 2 * (customer.A - (count + 1) / 2)
customer.C = customer.A - half;
}
}
}
希望有板友能稍作解释
谢谢大家...