去哪儿 java后端笔试 9.7 前两道思路
三道编程题 : 1.滑冰体力选动作 2.RSA解密 3.德州扑克
只过了前两道,第三道模拟了一个多小时还是只过77%,想不出特殊用例来,求大家交流
1.滑冰
我直接暴力递归
used数组记录哪个动作用过了,防止重复
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 运动员可得到的最高分
* @param energy int整型 运动员体力值
* @param actions int整型二维数组 二维数组i为动作号 actions[i][0]为动作i+1消耗体力,actions[i][1]为动作i+1得分
* @return int整型
*/
public int maxScore (int energy, int[][] actions) {
// write code here
boolean[] used = new boolean[actions.length];
process(energy, actions, used, 0);
return max;
}
private int max = Integer.MIN_VALUE;
public void process(int lastEnergy, int[][] actions, boolean[] used, int score) {
if (lastEnergy < 0) {
return;
}
max = Math.max(max, score);
for (int i = 0; i < actions.length; i++) {
if (used[i]) {
continue;
}
if (actions[i][0] > lastEnergy) {
continue;
}
used[i] = true;
process(lastEnergy-actions[i][0], actions, used, score+actions[i][1]);
used[i] = false;
}
} 用了BigInteger直接模拟
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 解密
* @param encryptedNumber int整型 待解密数字
* @param decryption int整型 私钥参数D
* @param number int整型 私钥参数N
* @return int整型
*/
public int Decrypt (int encryptedNumber, int decryption, int number) {
// write code here
BigInteger num = BigInteger.valueOf(1);
for (int i = 0; i < decryption; i++) {
num = num.multiply(BigInteger.valueOf(encryptedNumber));
}
num = num.mod(BigInteger.valueOf(number));
return num.intValue();
} 我是挨个牌型模拟,然后按照从大到小顺序判断符合条件就返回
没a
求教
#23届秋招笔面经#
顺丰集团工作强度 394人发布