网易2017秋招内推前端笔试题

三道算法题AC也挂,心好累

Posted by Eleven on 2017-08-19

No1.算法题

problem-1

  • 环境:V8 6.0.0

  • 优化:当超过三个不同字母,则循环break,打印0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
while(line=readline()){
var lines = line.split("");
var count = 0;
var arr = [];
for(var i =0; i < lines.length; i++){
if (arr.indexOf(lines[i]) < 0 && count < 3){
arr.push(lines[i]);
count++;
}else if(count >= 3) {
console.log(0)
break;
}
}
if (arr.length === 2){
console.log(2);
} else if(arr.length === 1){
console.log(1);
}
}

No2.最长01子串长度

problem-2

  • 环境:Node 0.12.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function(line){
var lines = line.split("");
var result = 0;
var pointer = -2;
var tempresult = 0;
for(var i = 0, len = lines.length; i < len; i++){
if (line[i] != line[i+1]){
pointer = i;
tempresult++;
} else if(line[i] == line[i+1] && pointer == (i - 1)){
tempresult++;
if(tempresult > result){
result = tempresult;
}
tempresult = 0;
pointer = -2;
}
}
if(pointer != -2){
if(tempresult > result){
result = tempresult;
}
tempresult = 0;
pointer = -2;
}
console.log(result);
});
rl.on('close', function() {
process.exit(0);
});

No3.小易生活费

problem-3

  • 环境:Node 0.12.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function(line){
var lines = line.split(" ");
var x = parseInt(lines[0]), f = parseInt(lines[1]), d = parseInt(lines[2]), p = parseInt(lines[3]);
var result;
if(parseInt(d/x) < f){
result = parseInt(d/x);
}else {
result = parseInt(f) + parseInt((d - f * x)/(p + x));
}
console.log(result);
});
rl.on('close', function() {
process.exit(0);
});