# 2017年校招全国统一模拟笔试(第三场)编程题集合

### 变换次数 (AC)

285

2

``````var readline = require('readline');
input: process.stdin,
output: process.stdout
});

rl.on('line', function(line) {
var times = 0;
var str = line.trim();
calc(str);
console.log(times);
function calc(str){
if(str.length === 1){
return times;
} else {
times++;
calc(str.split('').map(Number).reduce(function(a, b){
return a * b;
}, 1) + '');
}
}
});
``````

### 神奇数 (AC)

11 20

6

``````var readline = require('readline');
input: process.stdin,
output: process.stdout
});

rl.on('line', function(line) {
var num = line.trim().split(' ').map(Number);
var count = 0;
for(var i = num[0]; i <= num[1]; i++){
if(isMagicNum(i)){
count++;
}
}
console.log(count);

function isMagicNum(num){
arr = ('' + num).split('');
var len = arr.length;
for(var i = 0; i < arr.length; i++){
for(var j = 0; j < arr.length; j++){
if(i !== j && arr[j] !== '0' && arr[i] !== '0'){
if(isPrime(parseInt(arr[i] + arr[j]))){
return true;
} else if(isPrime(parseInt(arr[j] + arr[i]))){
return true;
}
}
}
}
return false;
}

function isPrime(num){
if((num & 1) === 0) return num === 2;
var limit = Math.sqrt(num);
for(var i = 3; i <= limit; i += 2){
if(num % i === 0) return false;
}
return true;
}

});
``````

### 添加字符 (AC)

abe
cabc

1

``````var readline = require('readline');
input: process.stdin,
output: process.stdout
});
var currLine = 0;
var strA, strB;
rl.on('line', function(line) {
if(currLine === 0){
strA = line.trim();
currLine++;
} else {
strB = line.trim();
if(strA.length === strB.length){
console.log(getDiffLen(strA, strB));
} else {
console.log(strA.length - maxCommonSubStrLen(strA, strB))
}
currLine = 0, strA = strB = '';
}

function maxCommonSubStrLen(a, b){
var maxLen = 0, len = 0;
var lenA = a.length, lenB = b.length;
var temp;
for(var i = 0; i < lenB - lenA + 1; i++){
temp = b.slice(i, i + lenA);
len = getCommonLen(a, temp);
maxLen = len < maxLen ? maxLen : len;
}
return maxLen;
}
function getDiffLen(a, b){
if(a.length !== b.length) throw new Error('Two parameters in "getDiffLen" should be same length.');
var arrA = a.split('');
var arrB = b.split('');
var num = 0;
arrA.forEach(function(item, index){
if(item !== arrB[index]){
num++;
}
});
return num;
}
function getCommonLen(a, b){
if(a.length !== b.length) throw new Error('Two parameters in "getDiffLen" should be same length.');
return a.length - getDiffLen(a, b);
}
});
``````

### 数组变换 (AC)

2
1 2

YES

``````var readline = require('readline');
input: process.stdin,
output: process.stdout
});
var currLine = 0;
var len, nums;
rl.on('line', function(line) {
if(currLine === 0){
len = parseInt(line);
currLine++;
} else {
nums = line.trim().split(' ').map(Number);
console.log(yesorno(nums));
len = currLine = 0, nums = null;

function yesorno(arr){
var flag = null, temp;
for(var i = 0; i < len; i++){
temp = nums[i];
while((temp & 1) === 0){
temp /= 2;
}
if(flag){
if(temp === flag) {
continue;
} else {
return 'NO';
}
} else {
flag = temp;
}
}
return 'YES'
}
}
});
``````

### 排序子序列 (AC)

6
1 2 3 2 2 1

2

``````var readline = require('readline');
input: process.stdin,
output: process.stdout
});
var currLine = 0;
var len, nums;
rl.on('line', function(line) {
if(currLine === 0){
len = parseInt(line);
currLine++;
} else {
nums = line.trim().split(' ').map(Number);
var count = 1;
var isAsc = null;
for(var i = 1, j = 0; i < len; i++){
if(nums[i] < nums[i - 1]){
if(isAsc === null) isAsc = true;
else if(!isAsc) {
count++;
isAsc = null;
}
} else if(nums[i] > nums[i - 1]){
if(isAsc === null) isAsc = false;
else if(isAsc){
count++;
isAsc = null;
}
}
}
console.log(count);
len = currLine = 0, nums = null;
}
});
``````

### 组队竞赛 (Memory out)

team1:{1,2,5}, team2:{5,5,8}, 这时候水平值总和为7.

team1:{2,5,8}, team2:{1,5,5}, 这时候水平值总和为10.

2
5 2 8 5 1 5

10

``````var readline = require('readline');
input: process.stdin,
output: process.stdout
});
var currLine = 0;
var group, nums;
rl.on('line', function(line) {
if(currLine === 0){
group = parseInt(line);
currLine++;
} else {
nums = line.trim().split(' ').map(Number).sort(function(a, b){
return b - a;
});
var len = nums.length;
var ans = 0;
for(var i = 1; i < len - group; i += 2){
ans += nums[i];
}
console.log(ans);
group = currLine = 0, nums = null;
}
});
``````

### 牛牛的数列 (AC)

6
7 2 3 1 5 6

5

``````var readline = require('readline');
var quantity = 0, len = 0;
var max = 0, temp = 0, flag = 0;
input: process.stdin,
output: process.stdout
});
var currLine = 0;
var len, nums;
rl.on('line', function(line) {
if(currLine === 0){
len = parseInt(line);
currLine++;
} else {
nums = line.trim().split(' ').map(Number);
var ans = 1;
var pre = [0], post = [0];

nums.shift(Infinity);
nums.push(Infinity);
for(var i = 1; i <= len; i++){
pre[i] = nums[i - 1] < nums[i] ? pre[i - 1] + 1 : 1;
}
for(var i = len; i > 0; i--){
post[i] = nums[i] < nums[i + 1] ? post[i+1] + 1 : 1;
}

for (var i = 1; i <= len; i++){
if (nums[i + 1] - nums[i - 1] >= 2) ans = Math.max(ans, pre[i - 1] + post[i + 1] + 1);
}
console.log(ans);
len = currLine = 0, nums = null;
}
});
``````

### 训练部队 (AC)

2
1 2
2 1

4

``````var readline = require('readline');
input: process.stdin,
output: process.stdout
});
var quantity = 0, len = 0;
var sum = 0, temp = 0, flag = 0;
rl.on('line', function(line) {
if(quantity === 0){
quantity = len = parseInt(line);
} else {
var input = line.trim().split(' ').map(Number);
if(input[0] >= input[1]){
temp = input[0] + input[1];
flag = flag > temp ? flag : temp;
} else {
temp = 2 * input[0];
flag = flag > temp ? flag : temp;
sum += input[1] - input[0];
}
quantity--;
if(quantity === 0){
console.log(sum + flag);
}
}
});
``````