关于去哪儿笔试(前端)
第一题AC,第二题超时只有56%,似乎是应该存入之前已经检查过的点的次数,不过在本地没调好。
求第二题AC的答案
第一题:
let M=16000; function getRealSalary(m) { if(m<0||m>1000000) return 0; let needTax=m-getWXYJ(m)-getStart(); let tax=needTax*getTaxRateAndDec(needTax).taxRate-getTaxRateAndDec(needTax).dec; return m>getStart()?m-Math.round(tax)-getWXYJ(m):m-getWXYJ(m); } function getStart() { let start=3500; return start; } function getWXYJ(num) { return Math.round(num*20/100); } function getTaxRateAndDec(num){ let taxRate=0; let dec=0; if(num<1500){ taxRate=0.03; dec=0; }else if(num<4500){ taxRate=0.1; dec=105; } else if(num<9000){ taxRate=0.2; dec=555; } else if(num<35000){ taxRate=0.25; dec=1005; } else if(num<55000){ taxRate=0.3; dec=2755; } else if(num<80000){ taxRate=0.35; dec=5505; }else{ taxRate=0.45; dec=13505; } return { taxRate, dec }; } console.log(getRealSalary(M));
第二题:
let lines=[[1,2],[2,1],[2,3],[1,0]]; let checks=[[1,2],[3,0],[1,3],[2.0]]; addLine(lines); removeSame(lines); removeSame(checks); console.log(getThroughTimes(lines,checks)); function addLine(arr) {//根据已经连通的点,判断哪些点连通了,并加进去 let len=arr.length for(let i=0;i<len;i++){ for(let j=len-1;j>=i+1;j--){ if(arr[i][1]==arr[j][0]&&arr[i][0]!=arr[j][1]){ let tmp=[]; tmp.push(arr[i][0]); tmp.push(arr[j][1]); arr.push(tmp); } } } } function removeSame(arr) {//去重 for(let i=0;i<arr.length;i++){ for(let j=arr.length-1;j>=i+1;j--){ if(arr[i][0]==arr[j][0]&&arr[i][1]==arr[j][1]){ arr.splice(i,1); } } } } function getThroughTimes(lines,checks) { let i=checks.length; let times=0; let timeArr=[]; while(i--){ if(!timeArr[i]) { let t = 0; for (let j = 0; j < lines.length; j++) { if (checks[i][0] == lines[j][0] && checks[i][1] == lines[j][1] || checks[i][0] == lines[j][1] && checks[i][1] == lines[j][0]) { t++; } let tmp = []; tmp.push(checks[i][0]); tmp.push(checks[i][1]); tmp.push(t); timeArr.push(tmp); } }else{ for (let j = 0; j < timeArr.length; j++) { if (checks[i][0] == timeArr[j][0] && checks[i][1] == timeArr[j][1]) { times+=timeArr[j][3]; } } } } return times; } function checkValue(value,n) {//检查值 if(value>=0&&value<=n){ return true; }else return false; }