描述: 有一批电商订单用数组表示如下,order_list=【【i,j,k】……】。其中,i,j为商品编号,
k为商品编号对应的数量,n为商品类别总数。现设计一个算法,计算总订单量。
输入: order_list=【【0,2,5】,【3,4,6】,【2,4,7】】,n=6
输出: final_list=【5,5,12,13,13,0,0】,解释:0号商品5件,1号商品5件,2号商品12件……
直接思路: 双层遍历,外层遍历order_list中的一维数组,内层遍历每个一维数组第1列至第2列的商品对应的数量。建立n+1行*2列的数组arr,arr【n+1】【0】存商品序号,arr【n+1】【1】存对应的k,即数量。
算法之一:
/**
* 有电商订单为i,j,k。i,j为商品序号,k为数量,n为商品种类,求商品订单量。
* @param order_list
* @param order_list[][],n
* @return arr[];
* @input order_list=[[0,2,5],[3,4,6],[2,4,7]];n=6
* @output arr=[5,5,12,13,13,0];
*/
public static int[] calOrderList(int order_list[][],int n) {
if(order_list.length==0||n==0)
return null;
int temp[][] = new int [n+1][2]; //定义n+1行*2列数组 ps:商品共n类,0号计算在内
for(int i=0;i<order_list.length;i++) { //外循环,定位第i个一维数组,即order_list[i]
int j;
for(j=order_list[i][0];j<=order_list[i][1];j++) {
//内循环从一维数组的第1列遍历到第2列,
//如:【0,2,5】遍历结果为,0号商品5件,1号商品5件,2号商品5件
temp[j][0]=j; //建立的n+1行*2列数组,第1列用商品序号填充,即为j
temp[j][1] += order_list[i][2]; //对应商品序号的k,即数量
}
}
int arr[] = new int[n+1];
for(int i =0;i<n+1;i++) {
arr[i]=temp[i][1];
}
return arr;
}
public static void main(String[] args) {
int[][] order_list= {{0,2,5},{3,4,6},{2,4,7}};
int n = 6;
int arr[]=new int[n];
int start = (int) System.nanoTime();
arr=calOrderList(order_list, n);
System.out.println("final:");
for(int i=0;i<n+1;i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
int end = (int)System.nanoTime();
System.out.println("time:"+(end-start)+"ns");
}
后记:若有错误,或可改进之处,欢迎读者提出自己的看法、思路、见解。