破碎的砝码
来源:互联网 发布:js金额大写转换函数 编辑:程序博客网 时间:2024/04/19 11:00
public class 破碎的砝码 {
/**
* 题目:重40磅的砝码,碎成重量各不等且重量为整数磅的4块。
* 且这4块砝码可以称出1~40 磅直接任意整磅数的重量。求这4块砝码碎片的重量各是多少?
* 分析:A!=B!=C!=D ;A+B+C+D = 40
*/
public static void main(String[] args) {
pieces();
}
public static void pieces(){
for(int i=01;i<=40;i++){
for(int j=i+1;j<=40-i;j++){
for(int m=j+1;m<=40-j-i;m++){
for(int n=m+1;n<=40-m-j-i;n++){
if(i+j+m+n==40){
if(justify(i,j,m,n)==1){
System.out.println(i+" "+j+" "+m+" "+n);
}
}
}
}
}
}
}
public static int justify(int i,int j,int m,int n){
for(int weight=1;weight<=40;weight++){
if(getWeight(i,j,m,n,weight)==0){
return 0;
}
}
return 1;
}
public static int getWeight(int i,int j,int m,int n,int weight){
for(int x1=-1;x1<=1;x1++){
for(int x2=-1;x2<=1;x2++){
for(int x3=-1;x3<=1;x3++){
for(int x4=-1;x4<=1;x4++){
if(x1*i+x2*j+x3*m+x4*n==weight){ //存在解
return 1;
}
}
}
}
}
return 0;
}
}
1 0
- 破碎的砝码问题
- 破碎的砝码
- 破碎的砝码C语言代码
- java深度搜索解决破碎的砝码
- 破碎的杯子
- 属于破碎的完美
- 破碎的假期
- 破碎的百合
- 回望破碎的家园
- 破碎的记忆
- 破碎的百合
- 破碎的梦
- 情感日记:蒙太奇的破碎
- 破碎的美丽
- 清华梦的破碎
- "创业梦"的破碎
- 都有一颗破碎的心
- “破碎”的md5恢复
- 连续整数固定和
- 数据之间的转换
- 冒泡排序
- Gimp去除图片背景色方法
- table的一些扩展方法
- 破碎的砝码
- runTime 初级
- NGUI UILabel计算文字行数
- #!/usr/bin/env python与#!/usr/bin/python的区别
- 求最大次大数
- 1042. 字符统计(20)
- 四皇后
- ASP.NET中如何实现FORM认证登录
- Android编程之fill_parent、wrap_content和match_parent的区别