4月16网易笔试
### 算法一:画O字
function shengchengO(n) {
let len = n * 4
for(let i = 1; i <= len; i++){
let row = Math.ceil(i / n)
let line = ""
for(let j = 1; j <= len; j++) {
if(row == 1) {
if( j <= n - i + 1) {
line += '.'
} else if(j >= 3 * n + i) {
line += '.'
} else {
line += '*'
}
} else if(row == 4) {
if( j <= ( i - 1 ) % n + 1 ) {
line += '.'
} else if(j > len - (( i - 1 ) % n + 1 )) {
line += '.'
} else {
line += '*'
}
} else {
let col = Math.ceil(j / n)
if(col == 1) {
line += '*'
} else if(col == 4) {
line += "*"
} else {
line += "."
}
}
}
console.log(line)
}
}
shengchengO(10) ### 算法二: > 要求输入一个长度为n的不重复数组,且里面的值不超过k,和为x(大概是这个意思~)
示例:
输入
> 4 6 15
输出:
> 1 3 5 6
function zuidaArrFn(n,k,x){
let count = 0
let set = new Set()
let arr = []
for(let i = 1; i <= n; i++) {
set.add(i)
arr.push(i)
}
let index = arr.length - 1
let sum = arr.reduce((pre,cur) => pre + cur,0)
while(count < x - sum && index >= 0) {
if(arr[index] < k && !set.has(arr[index] + 1)) {
set.delete(arr[index])
arr[index] = arr[index] + 1
count++
set.add(arr[index])
} else {
index--
}
}
if(index >= 0) {
return arr.join(' ')
} else {
return -1
}
}
console.log(zuidaArrFn(4,6,15)); ### 第三题:一道图的题目 没做
### 第四题求权重和
> 输入长度为3,输入数组数字为[10,2,5],权重就是尾数的数字0的个数,求权重和
> [1,1] 对应10 权重1
> [1,2] 对应20 权重1
> [1,3] 对应100 权重2
> [2,2] 对应2 权重0
> [2,3] 对应10 权重1
> [3,3] 对应5 权重为0
暴力超时:
> [1,1] 对应10 权重1
> [1,2] 对应20 权重1
> [1,3] 对应100 权重2
> [2,2] 对应2 权重0
> [2,3] 对应10 权重1
> [3,3] 对应5 权重为0
暴力超时:
function quanCount(n,arr) {
const zeroCount = function(num) {
let count = 0
while(true) {
num = num / 10
if(num >=1) {
count++
} else {
break;
}
}
return count
}
let res = 0
for(let i = 0;i < n;i++){
for(let j = i;j<n;j++){
let tempArr = arr.slice(i,j+1)
let sum = tempArr.reduce((pre,cur) => pre * cur,1)
res = res + zeroCount(sum)
}
}
return res
}
console.log(quanCount(3,[10,2,5]));