英语流利说技术类笔试题

版权声明:本文为博主原创文章,未经博主允许不得转载。欢迎关注我的微博@叮當了個和諧 https://blog.csdn.net/qq_25073545/article/details/82749941

编程题

在这里插入图片描述
在这里插入图片描述

//通过率只有0.8。不知道哪里判断出问题了
const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});


var inputs=[]; //用于存储每行的输入

rl.on('line',function(data){
    var can=[1,5,10,20,50]
    var t=parseInt(data)
    if(t===0){
        console.log(0)
    }else {
        console.log(combinationSum(can,t))
    }



});
var combinationSum = function(candidates, target) {
    candidates.sort((a,b) => a - b);
    const results = [];
    const innerFunction = (currArr, target, index) => {
        if (target === 0) {
            results.push(currArr.slice());
            return;
        }
        for (let i = index; i < candidates.length; i += 1) {
            if (target >= candidates[i]) {
                const newTarget = target - candidates[i];
                currArr.push(candidates[i]);
                innerFunction(currArr, newTarget, i);
                currArr.pop();
            } else {
                return;
            }
        }
    }
    innerFunction([], target, 0);
    return results.length;
};




简答题

You all know the simple 2 egg puzzle
Statement: You are given 2 identical eggs. You have access to a 100-storey building. Eggs can be very hard or very fragile means it may break if dropped from the first floor or may not even break if dropped from 100th floor. But if an egg breaks from certain floor it will certainly break from higher floors too. You are given task to figure out the highest floor of a 100-storey building an egg can be dropped without breaking. Now the question is how many drops you need to get certain about the floor from which eggs start breaking. You are allowed to break only 2 eggs in the process.
Unlike my previous posts where most people know the solution part for simple problem, for this problem mostly people only know the answer part and completely unaware of how it has been derived.

参考答案

Suppose N is the maximum number of drops to determine the breaking floor then first egg should be dropped from N if it breaks we can identify the floor with second egg in linear time. If the 1st egg doesnt break then we left with N-1 more attempts so instead of 2N floor we have to drop egg from 2N-1 floor (so that if it break we can identify the floor linearly in N-2 attempts with 2nd egg)
Dropping of 1st egg so follow this pattern(let say S): N…(N+N-1)…(N+N-1+N-2)…(N+N-1+N-2+N-3)……upto…(N+N-1+N-2……+1)
And as mentioned before if egg 1 breaks then follow with egg 2 linearly.
Summing the equation S we get
S=N*(N+1)/2 ….i hope no need to explain this simple AP.
Now we have 100 floors so S should just be greater than or equal to 100 to make it work
Therefore, N*(N+1)/2 >= 100
Hence we get N=14…the smallest satisfying this.

猜你喜欢

转载自blog.csdn.net/qq_25073545/article/details/82749941