答复: 几个非常有意思的面试题

本文探讨了一个关于寻找四个特定权重的问题,通过编程的方式遍历可能的组合来找到唯一符合条件的四种砝码重量,确保任意1到40之间的整数都能通过这四种砝码的不同组合称量出来。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[quote="swimmer2000"]哪位解释下第二题的解题技巧吧![/quote]

public class FourWight {

/**
* Method Description
*
* @param memberID memberID
* @return member member object
*/
int a =0;
int b =0;
int c =0;
int d =0;

public static void main(String[] args) {
System.out.println("start");

FourWight fw = new FourWight();
int i = 0 ;
while(true){
i++;
if(fw.getSelf()){
System.out.println(fw.a+"|"+fw.b+"|"+fw.c+"|"+fw.d);
break;
}else{
fw.a = i%40;
fw.b = (i/40)%40;
fw.c = (i/(40*40))%40;
fw.d = (i/(40*40*40))%40;
}

}

System.out.println("end");

}
public boolean get81times(int a,int b,int c,int d,int key){
int r = 1;
for(int i = 0 ; i < 3 ; i++){
for(int j=0 ; j <3;j++){
for(int k = 0 ; k <3 ; k++){
for(int q = 0 ; q <3 ;q++){
int f = a*(i-1)+b*(j-1)+c*(k-1)+d*(q-1);
if(key==f){
//System.out.println( f+"="+a+"*"+(i-1)+"+"+b+"*"+(j-1)+"+"+c+"*"+(k-1)+"+"+d+"*"+(q-1));
return true;
}
}
}
}
}
return false;
}
public boolean getSelf(){
for(int i =1 ;i<=40;i++){
if(!get81times(a,b,c,d,i)){
//System.out.println(i+"|"+a+b+c+d);
return false;
}
}
return true;
}


}

遍历....
40以下的4种法码的重量
只有一种合适.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值