一个算法,解决自动分配价格,望大家改进

来源:互联网 发布:知世故而不世故知乎 编辑:程序博客网 时间:2024/04/29 03:47

需求:

期货上面,要在1236的价格上买100手,要在1237的价格上面买150手(具体不定,可能分的很多)。另一方面:分配到具体的账户上面去买,如a账户要买50手,b账户要买60手,c账户要买140手(这两个购买手数相加是相等的),那么安装账户的顺序分配价格(从小到大),如a账户的50手的价格是1236,b账户的:50手1236价格,10手1237价格,c账户的全部是1237价格。

关键是在所有的变量都是变化的情况下,怎么自动为这些账户分配价格,按照账户的顺序,来从低到高分配价格。如果上面的不好理解我说过好理解的,商场里面有两种橘子:100斤橘子售价是80元,另外150斤橘子售价是85元。来了n个人,按照顺序买橘子,先来的肯定是买便宜的橘子,如a买了50斤,那么这50斤的价格全是80元,b要买130斤,那么其中50斤价格是80元,剩下的80斤价格是85元,c买了70斤,那么价格肯定是85元,求自动分配价格的算法

刚开始也是毫无头绪,但是经过指点

public class Test938 {public static void main(String[] args) {List<Orange> list = new ArrayList<Orange>();list.add(new Orange(80, 100));list.add(new Orange(90, 200));list.add(new Orange(85, 140));//将集合中元素按价格从低到高排列Collections.sort(list, new Comparator<Orange>() {@Overridepublic int compare(Orange o1, Orange o2) {return o1.getPrice() - o2.getPrice();}                       });sell(50,list);sell(130,list);sell(70,list);sell(300,list);}/**                   * @param count 要卖出的斤数                   * @param list  当前的存货                   */private static void sell(int count,List<Orange> list){System.out.println("该用户买橘子:"+count+"斤");List<Orange> sellList=new ArrayList<Orange>();List<Orange> delList=new ArrayList<Orange>();Iterator<Orange> it=list.iterator();Orange or=null;int temcount=0;while(it.hasNext()){or=it.next();temcount=or.getTotal();if(temcount>count){or.setTotal(temcount-count);sellList.add(new Orange(or.getPrice(),count));count=0;break;}else if(temcount==count){delList.add(or);sellList.add(new Orange(or.getPrice(),count));count=0;break;}else {delList.add(or);count-=temcount;sellList.add(new Orange(or.getPrice(),temcount));}}//更新存货for(Orange o:delList){list.remove(o);}for(Orange o:sellList){System.out.println("   价格:"+o.getPrice()+"<-->"+o.getTotal()+"斤");}if(count>0){System.out.println("   橘子缺货:"+count+"斤");}}}class Orange {private int price;private int total;public Orange(int price, int total) {this.price = price;this.total = total;}public int getPrice(){return price;}public int getTotal() {return total;}public void setTotal(int total){this.total = total;}}

改进:

 //把总的价格和总的position封装成map,然后把每个需要的position也传入,   //最后生成的就是封装好的一个list集合,因为这个账户可能有不同的价格和position。    private static List<Map<Double,Integer>> buy(int position,Map<Double,Integer> map)    {   //Map<Double,Integer> localMap=map;//把传过来的map转化为本地的map。        List<Map<Double,Integer>> end=new ArrayList<Map<Double, Integer>>();//返回的list集合,就是计算后的list集合,里面是map        List<Double> delKey=new ArrayList();//每个轮回需要删除的map的key        for (Map.Entry<Double, Integer> m : map.entrySet()) {                        int sumPosition=m.getValue();                        if(position<sumPosition)            {                   Map<Double,Integer> returnMap=new LinkedHashMap();                returnMap.put(m.getKey(), position);                end.add(returnMap);                m.setValue(sumPosition-position);                break;            }else if(position==sumPosition)            { Map<Double,Integer> returnMap=new LinkedHashMap();                returnMap.put(m.getKey(), position);                end.add(returnMap);                delKey.add(m.getKey());                              break;            }            else{                                 Map<Double,Integer> returnMap=new LinkedHashMap();                returnMap.put(m.getKey(), sumPosition);                end.add(returnMap);                delKey.add(m.getKey());                               position-=sumPosition;                  }            }            //清理货存            for(Double del:delKey)            {                            map.remove(del);            }//          for(Map<Double,Integer> endmap:end)//          {//              for(Map.Entry<Double,Integer> entry:endmap.entrySet())//              {//                  System.out.println("价格"+entry.getKey()+"...."+"仓位"+entry.getValue());//              }//          }              return end;    }

上面的算法并不算好,就是对于每个分配好的账户都封装到一个map里面(一个map只装一个键值对,这样觉得浪费),然后再把这些map封装到list的集合中。

大家谁有好的算法,欢迎指点。




原创粉丝点击