LanQiaoTestCodes/货物摆放.java

60 lines
1.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package LanQiaoOJ;
import java.util.ArrayList;
/*
* 小蓝有一个超大的仓库,可以摆放很多货物。
现在,小蓝有箱货物要摆放在仓库,每箱货物都是规则
的正方体。小蓝规定了长、宽、高三个互相垂直的方向,每
箱货物的边都必须严格平行于长、宽、高。
小蓝希望所有的货物最终摆成一个大的长方体。即在长、
宽、高的方向上分别堆L、W、H的货物满足n=L×WxH。
给定,请问有多少种堆放货物的方案满足要求。
例如,当=4时有以下6种方案1×1×4、1×2×2、1×4×1、2×1×2、2×2×1、4×1×1。
请问当m=2021041820210418(注意有16位数字)时,总共有多少种方案?
*/
public class 货物摆放 {
public static void main(String[] args) {
int count=0;
long num=2021041820210418l;
//按此算法,时间复杂度非常大,短时间内无法出结果
// long l,w,h;
// for(l=0;l<=n;l++) {
// for(w=0;w<=n;w++) {
// for(h=0;h<=n;h++) {
// if(n==l*h*w) {
// count++;
// }
// }
// }
// }
// System.out.println(count);
// }
//声明一个ArrayList动态数组
ArrayList<Long> arr=new ArrayList<>();
//遍历求出num的因子对num开平方根以减少运算量
for(long i=1;i<Math.sqrt(num);i++) {
if(num%i==0) {
//将num的因子放入数组中
arr.add(i);
//在num能被i整除情况下再求出num的另一个因子
long n=num/i;
//如果num = Math.sqrt(num)*Math.sqrt(num),那么由较小的因子求较大的因子时,会重复,要排除这种情况
//当然此时num不会出现这种情况。如果num=4就会出现这种情况
if(n!=i)
arr.add(n);
}
}
for(long l:arr) {
for(long h:arr) {
for(long w:arr) {
if(num==l*h*w)
count++;
}
}
}
System.out.println(count);
}
}