好吧,我一直试图让我的头脑在这周围一段时间,但我觉得我太累了,无法绕过这个。
我的脚本每周都会通过一个表格,将60种新产品的状态从停用状态更改为已激活状态。我想确保这60种产品不是全部来自1类,而是来自所有可以激活产品的类别。
示例产品表
| productid | category | status |
| 1 | iPad 2 | deactivated |
| 2 | iPod Touch 4 | deactivated |
示例数组,您将看到类别名称以及可以激活的产品数量。
array(9) {
["iPad 2"] => int(2)
["iPod Touch 5"] => int(2)
["iPod Touch 4"] => int(6)
["iPhone 3G/3GS"] => int(94)
["iPad 1"] => int(104)
["iPad Mini"] => int(150)
["iPhone 4/4S"] => int(174)
["iPhone 5"] => int(205)
["iPad 3/4"] => int(236)
}
现在,我将如何激活所有这些类别的产品,分得很好?
生成的数组我可以大致手动计算。那将是,类别名称和将被激活的产品。共有60种产品,分类很好。
array(9) {
["iPad 2"] => int(2)
["iPod Touch 5"] => int(2)
["iPod Touch 4"] => int(6)
["iPhone 3G/3GS"] => int(8)
["iPad 1"] => int(8)
["iPad Mini"] => int(8)
["iPhone 4/4S"] => int(8)
["iPhone 5"] => int(9)
["iPad 3/4"] => int(9)
}
但我缺乏编程这种算法的能力。 我希望今天的人比我今天有点亮?
答案 0 :(得分:0)
在伪代码中:
EvenlySelect(item_list):
sorted_items = SortLowestToHighestCount(item_list)
remaining_num_items = CountTotalNumberOfItems(item_list)
return_list = Empty()
while (!Empty(sorted_items)):
desired_quantity = remaining_num_items / Size(sorted_items))
if desired_quantity == 0:
break
quantity_to_add = 0
if desired_quantity > sorted_items[0].quantity:
quantity_to_add = sorted_items[0].quantity
else:
quantity_to_add = desired_quantity
Append(Item(sorted_items[0].category, quantity_to_add), return_list)
remaining_num_items -= quantity_to_add
RemoveFirstElement(sorted_items)
return return_list
这个想法是:从数量最少的类别到最大的类别。在每个步骤中,确定从该类别中您需要的项目数量是否均匀(根据您之前步骤中已经选择的内容)。如果该类别有足够的项目,请采取那么多。否则请尽可能多地使用。
答案 1 :(得分:0)
import java.util.Arrays;
import java.util.Random;
public class Selection {
public static void main(String[] args) {
//input arrays
String[] categories = {"iPad 2","iPod Touch 5","iPod Touch 4","iPhone 3G/3GS","iPad 1","iPad Mini","iPhone 4/4S","iPhone 5","iPad 3/4"};
int[] quantities = {2,2,6,94,104,150,174,205,236};
//ensure that there's at least one item for each category
int[] distributions = {1,1,1,1,1,1,1,1,1};
int[] intervals = new int[9];
intervals[0] = 2;
for (int i=1;i<9;i++) {
intervals[i] = intervals[i-1]+quantities[i];
}
Random generator = new Random();
int range = intervals[intervals.length-1];
for (int j=0;j<51;j++) { //to fill the remaining 51 slots according roughly to their proportions
int randomInt = generator.nextInt(range); //generate an integer between 0 and 2+2+6+94+104+150+174+205+236 at random
if (randomInt < intervals[0]) {
distributions[0]++;
}
else if (randomInt >= intervals[0] && randomInt < intervals[1]) {
distributions[1]++;
}
else if (randomInt >= intervals[1] && randomInt < intervals[2]) {
distributions[2]++;
}
else if (randomInt >= intervals[2] && randomInt < intervals[3]) {
distributions[3]++;
}
else if (randomInt >= intervals[3] && randomInt < intervals[4]) {
distributions[4]++;
}
else if (randomInt >= intervals[4] && randomInt < intervals[5]) {
distributions[5]++;
}
else if (randomInt >= intervals[5] && randomInt < intervals[6]) {
distributions[6]++;
}
else if (randomInt >= intervals[6] && randomInt < intervals[7]) {
distributions[7]++;
}
else {
distributions[8]++;
}
}
System.out.println("array(9) {");
for (int k=0; k< 9; k++) {
System.out.println(" [\""+categories[k]+"\"] => int("+distributions[k]+")");
}
System.out.println("}");
}
}
我已经测试了上面的完整和有效的代码,并希望代码不言自明。
这是我刚刚获得的示例输出:
array(9) {
["iPad 2"] => int(1)
["iPod Touch 5"] => int(1)
["iPod Touch 4"] => int(2)
["iPhone 3G/3GS"] => int(6)
["iPad 1"] => int(7)
["iPad Mini"] => int(11)
["iPhone 4/4S"] => int(8)
["iPhone 5"] => int(13)
["iPad 3/4"] => int(11)
}
这个想法很简单:我们首先为每个类别保留一个项目,这需要9个广告位。考虑剩余的51个插槽,每个插槽根据其比例分配到其各自的类别。由于项目总量不够大而我们使用随机函数生成整数,我们无法保证结果完全符合每个类别的比例。尽管如此,我认为解决方案足以满足您的需求。