做了一份前端面試題,有兩道沒寫出標準答案 ?

第二題的答案,個人覺得比較精簡的寫法。

var arr = [13, 1, 85, 8, 99, 3, 11, 0, 0];
// var arr = [5, 3, 31, 2];
function getmax(pre, next) {
var m = pre.toString().length,
n = next.toString().length;
var preAfter = pre * Math.pow(10, n) + next,
nextAfter = next * Math.pow(10, m) + pre;
return preAfter &> nextAfter;
}
arr.sort(function(a, b) {
return !getmax(a, b);
});
alert(arr.join());


let format = (() =&> {

let unitArr = "萬千百十".split(""),

numberArr = "零一二三四五六七八九".split(""),

zero = numberArr[0],

peatReg = new RegExp(`${zero}+`, "g"),

endReg = new RegExp(`${zero}$`, "g");

return (money) =&> {

let digit = 5 - ("" + money).length;

return ("" + money).replace(/d/g, ($1, index) =&> {

return numberArr[+$1] + (+$1 (unitArr[index + digit] || "") || "")

}).replace(peatReg, zero).replace(endReg, "");

}

})();

console.log(format(12045))

console.log(format(1200))

console.log(format(120))

console.log(format(14))

console.log(format(5))

來自一枚初級搬磚工


第二題有個問題,就是2換成字元串有兩和二兩種轉換,因為你不能輸出兩萬兩千兩十兩,但是例子的結果不是二萬零八百七十六,所以我決定得這個也要考慮進去


private static String[] unit = { "","十", "百", "千", "萬", };
private static String[] number = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };

public String convert(int num){
String str = "";
int step = 0;
while(num!=0){
int j = num%10;
num = num/10;
if(j!=0){
str = number[j] + unit[step] + str;
}else if(step != 0 j==0){
if(!"零".equals(str.substring(0,1)))
str = "零" + str;
}
step++;
}
return str;
}


坐等題主(這裡我指命題人)出現


如果是大數字對比,假設有5555555555556這樣長位數,複雜度非常高,不要使用直觀的數字位數去對比。

考慮下這個做法: 不過多長的數字,複雜度都是(n+m)*m/2

//先將數字字元串化,遍歷對比 max = nm&>mn?n:m

//註:+號是js中將數字型字元串轉化為數字類型的一種方式。

function maximum(array){
if(array.length&<2){ return array;} var arr = array.join(",").split(","), //將數組數字字元串化,用於字元串組合 temp = []; function get(arr){ var max=arr[0], //對比從數組第二位開始,默認最大是第一位。 index=0, t for(var i=1, len=arr.length; i& +(max+t)){
max = t;
index = i;
}
}
//最大值排在第一位
temp.push(+max)
//數組排除max
arr.splice(index,1);
//如果剩下的數組長度不等於零,繼續檢測剩下的數
arr.length get(arr);
}
get(arr);
return +temp.join("");
}

console.log(maximum([16,89,7,32,654,42])) //897654423216
console.log(maximum([13,19,6,98,62,1])) //9866219131
console.log(maximum([4,5,7,9,9,2])) // 997542
console.log(maximum([4,5,7,9,90,2])) //9907542


static int getAge(int n)
{
if (n == 1) return 10;
else
{
n--;
return 2 + getAge(n);
}
}

static string convertNum(int i)
{
if (i &< 1 || i &> 99999)
return "輸入錯誤";

string[] dic = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
string[] dic1 = { "", "", "十", "百", "千", "萬" };

string s = "";
int j = 0;
int step = 0;
while (i != 0)
{
j = i % 10;
i = i / 10;
step++;

if (j == 0 i % 10 == 0) continue;

if (j != 0)
s = dic1[step] + s;

if (step &> 0 j == 2)
{
s = "兩" + s;
}
else
{
if (s.Length == 0 j == 0)
continue;
s = dic[j] + s;
}

}

return s;
}

static void Main(string[] args)
{
for (int i = 1; i &<= 8; i++) Console.WriteLine(getAge(i)); Console.WriteLine("問題2:"); int[] arr = { 20876, 10000, 10001, 10101, 20202 }; for (int i = 0; i &< arr.Length; i++) { string s = convertNum(arr[i]); Console.WriteLine(s); } Console.ReadLine(); }


渣代碼,求輕噴...

我覺得關鍵是list元素的比較,一開始覺得string內置的字典排序就夠了,但是細想下不對,所以重寫了compareTo方法(string為final類不能繼承,所以新寫),當元素長度一樣時直接字典序,不一樣時需要特殊處理,另外要求從大到小,所以講return值加了-號

package test;

import java.util.Arrays;

import java.util.List;

public class MyString implements Comparable& {

private String string;

public MyString(String string) {

this.string = string;

}

public String getString() {

return string;

}

public String toString() {

return string;

}

public static void main(String args[]) {

List& list = Arrays.asList(5, 3, 31, 2);

System.out.println(getMaxNum(list));

}

private static String getMaxNum(List& list) {

MyString[] myArray = new MyString[list.size()];

int i = 0;

for (Integer val : list)

myArray[i++] = new MyString(String.valueOf(val));

Arrays.sort(myArray);

StringBuilder sb = new StringBuilder();

for (MyString str : myArray)

sb.append(str);

return sb.toString();

}

public int compareTo(MyString myString) {

if (getString().length() == myString.getString().length())

return -getString().compareTo(myString.getString());

else if (getString().length() &> myString.getString().length()) {

if (getString().substring(0, myString.getString().length()).equals(

myString.getString()))

return new MyString(getString().substring(

myString.getString().length())).compareTo(myString);

else

return -getString().compareTo(myString.getString());

} else

return -myString.compareTo(this);

}

}

第二題也來個JAVA實現

package test;

public class MyPrint {

public static void main(String args[]) {

int i = 10100;

System.out.println(getCharString(i));

}

private static String getCharString(int i) {

boolean zeroToPrint = false;

boolean everPrint = false;

StringBuilder sb = new StringBuilder();

int x = 4;

while (i != 0) {

if (i / (int) Math.pow(10, x) != 0) {

if (zeroToPrint everPrint)

sb.append("零");

everPrint = true;

sb.append(getChar((i / (int) Math.pow(10, x))) + getUnit(x));

zeroToPrint = false;

} else

zeroToPrint = true;

i = i % (int) Math.pow(10, x);

x--;

}

return sb.toString();

}

static String getUnit(int i) {

switch (i) {

case 1:

return "十";

case 2:

return "百";

case 3:

return "千";

case 4:

return "萬";

}

return "";

}

static String getChar(int n) {

switch (n) {

case 1:

return "一";

case 2:

return "二";

case 3:

return "三";

case 4:

return "四";

case 5:

return "五";

case 6:

return "六";

case 7:

return "七";

case 8:

return "八";

case 9:

return "九";

case 0:

return "零";

}

return "";

}

}


選擇題第一題有正確答案嗎?求教?如果高二有一萬人,高一有十人,或者相反呢


推薦閱讀:

請問mysql「字元查詢」和「數字查詢「的效率問題?
c++中如何為一個程序寫擴展?
【20171027】- 不使用第三方程序快速啟動Windows應用程序(二)
Vue-cli 3.0 初體驗 !
如何將opengl程序從Windows上移植到linux上?

TAG:前端開發 | 邏輯 | 代碼 | 程序 |

分頁阅读: 1 2