您的位置:首页 > 编程语言

2017 8.25 阿里巴巴校招 在线笔试题

2017-08-26 14:37 357 查看
2017 8.25 阿里巴巴校招 在线笔试题

选择(40min)



编程题(80min)

1.天猫 物流,海关问题

关键字:盒子大小、每盒总价值小于2000

求 最少盒子数

public class NewTest {

/** 请完成下面这个process函数,实现题目要求的功能 **/
/** 当然,你也可以不按照这个模板来作答,完全按照自己的想法来 ^-^  **/
private static int process()

{
int amount=0,maxlength=0,maxhight=0;maxwidth=0,j=0;
int sumlength= boxTemplate.length;
int sumheight= boxTemplate.height;
int sumwidth = boxTemplate.width;
for(int i=0;i<itemNum;i++){
for(int j=i;j<itemNum;j++){
if(item[i].length<=boxTemplate.length&&maxlength<=boxTemplate.length){
if(item[i].height<=boxTemplate.height&&maxhight<=boxTemplate.height){
if(item[i].width<=boxTemplate.widthmaxwidth<=boxTemplate.width){
if(item[j].price<=2000&&amount<=2000){
amount+=items[j].price;
maxlength+=items[j].length;
maxhight+=items[j].height;
maxwidth+=items[j].width;
sumlength-=items[j].length;items[j].length=0;
sumlength-=item[i].height;items[j].height=0;
sumlength-=item[i].width;items[j].width=0;}

}
}
}}boxMinNum++;sumlength=boxTemplate.length;sumheight=boxTemplate.height;sumwidth=boxTemplate.width;}return boxMinNum;}
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
boxTemplate.price = CUSTOMS_LIMIT_MONEY_PER_BOX;

while (scanner.hasNext()){
boxTemplate.length = scanner.nextInt();
boxTemplate.width = scanner.nextInt();
boxTemplate.height = scanner.nextInt();

int itemNum = scanner.nextInt();
items = new Model[itemNum];
for(int i=0; i<itemNum; i++){
Model item = new Model();
item.price = scanner.nextInt();
item.length = scanner.nextInt();
item.width = scanner.nextInt();
item.height = scanner.nextInt();
items[i] = item;
}
long startTime = System.currentTimeMillis();
boxMinNum = Integer.MAX_VALUE;
System.out.println (process());

}
}

}


在快递公司干线运输的车辆使用中,存在着单边车和双边车的两种使用场景,例如 北京中心-杭州中心,两个分拨中心到彼此的单量对等,则可以开双边车(即同一辆车可以往返对开),而当两个中心的对发单量不对等时,则会采用单边车,并且双边车的成本是低于单边车的,即将两辆对开的单边车合并为一辆往返的双边车是能够节省运力成本的

单边车优化原则:

将单边车优化的规则进行可抽象为以下三种(A,B,C均表示分拨中心):

规则-1: A-B单边车,B-A单边车 优化方案:将A-B和B-A的两辆单边车合并为双边;

规则-2: A-B单边车,B-C单边车,C-A单边车 优化方案:将A-B、B-C、C-A的三辆单边车优化为一辆环形往返车;

规则-3: A-B单边车,C-A单边车,B、C同省 优化方案:当B、C同省,将A-B、C-A两辆单边优化为一辆环形往返

问题如下:

以某快递公司的实际单边车数据为例(线路ID编码;出分拨中心; 出分拨中心所在省;到达分拨中心;到达分拨中心所在省;车型;),进行优化,优化的规则参照以上,并且优先级依次降低,合并的时候需要考虑车型(分为17.5m和9.6m两种):1、相同车型才能进行合并;2、两辆同方向的9.6m可以与一辆17.5m的对开车型合并优化 说明:优化输出结果按照规则分类,例如rule1: 2016120001+2016120002表示将单边车线路ID编码为2016120001和2016120002按照规则1合并优化

已输入字数: 1837 / 10000 运行 编程说明 +

public class Bus {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<UnilateralLine> lineList = new ArrayList<UnilateralLine>();
while (scanner.hasNextLine()) {
String[] options = scanner.nextLine().split(";");
if (options.length < 5) {
break;
}
lineList.add(new UnilateralLine(options[0], options[1], options[2], options[3], options[4], options[5]));
}
scanner.close();

// wirte your code here
List<String> result = calculateUnilateral(lineList);

for (String str : result) {
System.out.println(str);
}
}
public static List<String> calculateUnilateral(List<UnilateralLine> lineList) {
List<String> result = new ArrayList<String>();
int i,k,j;

boolean flag=true;

for(i=0;i<lineList.size()&&flag!=false;i++){
k=i+1;j=k;

//rule 1:
if(lineList.get(i)!=null){
for(;k<lineList.size();k++){
if(lineList.get(k)!=null){
if(lineList.get(i).sCen==lineList.get(k).eCen&&lineList.get(k).sCen==lineList.get(i).eCen){

if(lineList.get(i).tType==lineList.get(k).tType){
result.set(i, (lineList.get(i).id+lineList.get(k).id));
lineList.remove(i);lineList.remove(k);

flag=false;break;
}}

}}
if(flag=false){ flag=true;break;}

k=i+1;}

//rule 2:
else   if(lineList.get(i)!=null){

while(lineList.get(k)!=null&&k<lineList.size()){
if(lineList.get(i).sCen==lineList.get(k).eCen){

while(j<lineList.size()){
if(lineList.get(k).sCen==lineList.get(j).eCen){
if(lineList.get(j).sCen==lineList.get(i).eCen){
if(judge(lineList.get(i).tType,lineList.get(k).tType,lineList.get(j).tType)!= false){

result.set(i, (lineList.get(i).id+lineList.get(k).id+lineList.get(j).id));
lineList.remove(i);lineList.remove(k);lineList.remove(j);

flag=false;break;}
}}j++;}}
if(flag=false){ break;}k++;}

if(flag=false){ flag=true;break;}
}

//rule 3:
else if(lineList.get(i)!=null){

while(k<lineList.size()){
if(lineList.get(i).sCen==lineList.get(k).eCen&&lineList.get(i).eCen!=lineList.get(k).sCen&&lineList.get(i).ePro==lineList.get(k).sPro){
if(lineList.get(i).tType==lineList.get(0).tType){
result.set(i, (lineList.get(i).id+lineList.get(k).id));
lineList.remove(i);lineList.remove(k);

flag=false;break;}
}
}
}
}

return result;
}
private static boolean judge(String tType, String tType2, String tType3) {
String a="9.6m";
if(tType==tType2&&tType2==tType3){return true;}
else{
if(tType==tType2&&tType.equals(a)){return true;}
if(tType==tType3&&tType.equals(a)){return true;}
if(tType2==tType3&&tType2.equals(a)){return true;}
}
return false;
}
public static class UnilateralLine {
private String id;
private String sCen;//出发分拨
private String sPro;//出发省
private String eCen;//到达分拨
private String ePro;//到达省
//9.6m/17.5m
private String tType;//车型
public UnilateralLine(String id, String sCen, String sPro, String eCen, String ePro,String tType) {
this.id = id;this.sCen = sCen;this.sPro = sPro;this.eCen = eCen;this.ePro = ePro;this.tType = tType;}
public String getId() {return id;}
public void setId(String id) {this.id = id;}
public String getSCen() {return sCen;}
public void setSCen(String ePro) {this.ePro = ePro;}
public String getSPro() {return sPro;}
public void setSPro(String sPro) {this.sPro = sPro;}
public String getECen() {return eCen;}
public void setECen(String eCen) {this.eCen = eCen;}
public String getEPro() {return ePro;}
public void setEPro(String ePro) {this.ePro = ePro;}
public String getTType() {return tType;}
public void setTType(String tType) {this.tType = tType;}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  阿里巴巴 编程