java两人猜数字游戏,三人背后猜数字游戏

1.背景:         猜数字,ABC三人背后各贴一个数字,正整数,各自能看到对方的数字,其中一人为另外两人之和。依次问ABC,第一轮,问A,A猜不出,问BC,同样猜不出,第二轮,AB猜不出,C说我知道,我背后是49。问AB各位多少?

2.解题思路{遍历}

正整数范围内,两数相等则则只第三个数为两者之和。猜出数的人则是其派出了两外一个数,因为他的数只有两种可能,为另两者之和或只差。假如他的数为排除的那个数,则判定他前一个人也能排除自己的那个数…..   依次倒推过去,一定会出现有两个数相等的情况,此时即是满足条件,则输出。

3.代码实现{Java}

/**

* 三个孩纸背后贴纸,只能看到对方的数字, 第一轮,abc顺序,都猜不出,第二轮,ab还是猜不出,c说自己是49,问ab背后各是多少

* 解题思路:两人相等,第三人一定能确定是另外两人之和。

*/

public class TestAbc {

final static int 循环次数 = 5;// 所有人第几次猜出来就是几。

public static void main(String[] args) {

long begainTime = System.currentTimeMillis();

System.out.println("===================begain=====================\n");

for (int b = 1; b < 100000; b++) {

int a = 49 - b;

int notPassNum = Math.abs(a - b);

int args1 = b;

int args2 = a;

for (int i = 0; i < 循环次数; i++) {

if (is3EqualsNo(args1, args2, notPassNum, a, b)) {

System.out.println("answer : a=" + a + " b=" + b + " c=" + 49);

break;

}

int temp = notPassNum;

notPassNum = getNum(args1, args2, notPassNum);

args1 = args2;

args2 = temp;

}

}

System.out.println("\n===================finished and cost time: "+(System.currentTimeMillis()-begainTime)+" ms");

}

private static int getNum(int self, int other1, int other2) {

return self == (other2 + other1) ? Math.abs(other1 - other2) : other2 + other1;

}

public static boolean is3EqualsNo(int a, int b, int c, int aa, int bb) {

return (a == b || b == c || a == c) && a * b * c * aa * bb > 0;

}

}

4.答案: ===================begain===================== answer : a=21 b=28 c=49 ===================finished and cost time: 8 ms

5.PS:绕死了,小学奥数题,有简便方法,自己想。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值