题目:有n(n<=8)个物品需要装入盒子,形状为圆型,且物品必须紧贴盒子底部。情况类似图中所示:

 题目:有n(n<=8)个物品需要装入盒子,形状为圆型,且物品必须紧贴盒子底部。情况类似图中所示:


求盒子宽度的最小值。  
//forth
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.LinkedList;
import java.util.Scanner;


public class forth {


private final static String fStreamIn = "d.in";
private final static String fStreamOut = "d.out";
private static double width;
public static void main(String[] args) {
try{
//declare global variable for program
Scanner reader = new Scanner(new File(fStreamIn));
Writer writer = new FileWriter(new File(fStreamOut));

try{
int totalNum = reader.nextInt();
LinkedList<Double> datas = new LinkedList<Double>();
for(int i=0;i<totalNum;i++){
datas.add(reader.nextDouble());
}
for(int i=0;i<totalNum;i++){
width+=(datas.get(i));
}
width*=2;
//System.out.println(width);
//按圆半径排序
LinkedList<Double> result = new LinkedList<Double>();
//求值
dfs(datas,result);
System.out.println(String.format("%.3f", width));
//写入文件
writer.write(String.format("%.3f", width));

}catch(Exception ex){
ex.printStackTrace();
}finally{
//close stream
reader.close();
writer.close();
}
}catch(Exception e){
e.printStackTrace();
}
}

public static void dfs(LinkedList<Double> rest,LinkedList<Double> result){
if(rest.size()>0){
for(int i=0;i<rest.size();i++){
LinkedList<Double> next = (LinkedList<Double>)rest.clone();
next.remove(i);
result.add(rest.get(i));
dfs(next,result);
}
}
double sum = 0.0;
for(int i=1;i<result.size();i++){
double r1 = result.get(i);
double r2 = result.get(i-1);
double d1 = (r1+r2)*(r1+r2);
double d2 = (r1-r2)*(r1-r2);
sum += Math.sqrt(d1-d2);
}
sum+=result.get(0);
sum+=result.get(result.size()-1);
if(sum<width){
width = sum;
}
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白爱吃猪头肉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值