0

## 面試題討論

sion 2019-12-03 17:18:471768 瀏覽

``````input : 10000
ouput : Ten thousand dollars
-----------------------
input : 1999999
ouput : One million,nine hundred and ninety nine thousand,nine hundred and ninety nine dollars
-----------------------
input : 123456789
ouput : One hundred and twenty three million,four hundred and fifty six thousand,seven hundred and eighty nine dollars
``````

### 8 個回答

2

iT邦大神 1 級 ‧ 2019-12-03 17:43:54

``````public static string NumberToWords(int number)
{
if (number == 0)
return "zero";

if (number < 0)
return "minus " + NumberToWords(Math.Abs(number));

string words = "";

if ((number / 1000000) > 0)
{
words += NumberToWords(number / 1000000) + " million ";
number %= 1000000;
}

if ((number / 1000) > 0)
{
words += NumberToWords(number / 1000) + " thousand ";
number %= 1000;
}

if ((number / 100) > 0)
{
words += NumberToWords(number / 100) + " hundred ";
number %= 100;
}

if (number > 0)
{
if (words != "")
words += "and ";

var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

if (number < 20)
words += unitsMap[number];
else
{
words += tensMap[number / 10];
if ((number % 10) > 0)
words += "-" + unitsMap[number % 10];
}
}

return words;
}
``````

0

iT邦大師 1 級 ‧ 2019-12-03 19:48:55
1. 哪種程式語言?
2. 說好的程式碼呢?

sion iT邦新手 4 級 ‧ 2019-12-04 09:24:30 檢舉

``````if(a==1)
return one;
else if(a==2)
return two;
.
.
.
if(a==11)
return eleven;
.
.
.
``````

2

iT邦大師 1 級 ‧ 2019-12-04 09:04:18

sion iT邦新手 4 級 ‧ 2019-12-04 09:21:59 檢舉

backcall真的沒聽過

WQ iT邦新手 4 級 ‧ 2019-12-04 09:41:36 檢舉

WQ iT邦新手 4 級 ‧ 2019-12-04 09:44:24 檢舉

froce iT邦大師 3 級 ‧ 2019-12-04 11:29:36 檢舉

ㄜ...我只知道遞迴是recursive，backcall這我還真的第一次看見。哈

backcall 就是再call自已一次。

callback 就是準備好一個程序等待回應回呼。

(抱歉，今天本人28天來，有點無理取鬧中)

0
listennn08
iT邦新手 2 級 ‧ 2019-12-04 11:28:11

Sample

Billion 1,000,000,000
Million 1,000,000
Thousand 1,000

``````units = ['', 'Thousand', 'Million', 'Billion']
lessThanTwenty = ['', 'One', ..., 'Nineteen']
tenDigits = ['', 'Ten', ..., 'Ninety']
``````

``````while (num > 0) {
num3 = _Hundred(num%1000)
if (num3) {
res.unshift(`\${num3} \${units[i]}`);
}
num = Math.floor(num/1000);
i++
}
return res.join(' and ');
``````

``````const _Hundred =  (num) => {
if (num < 20) {
return `\${lessThanTwenty[num]}`
} else if (num < 100) {
return `\${tenDigits[Math.floor(num/10)]} \${_Hundred(num%10)}`
} else {
return `\${lessThanTwenty[Math.floor(num /100)]} Hundred \${_Hundred(num % 100)}`
}
}
``````

1
ccutmis
iT邦高手 8 級 ‧ 2019-12-04 12:44:48

test.htm

``````<script>
const alias_arr=['Trillion','Billion','Million','Thousand'];
const dig_str_dict={'0':'none','1':'One',  '2':'Two','3':'Three','4':'Four','5':'Five','6':'Six','7':'Seven','8':'Eight','9':'Nine'}; //百位數&個位數
const teen_str_dict={
'10':'Ten','11':'Eleven','12':'Tweleve','13':'Thirteen','14':'Fourteen','15':'Fifteen','16':'Sixteen','17':'Seventeen','18':'Eighteen','19':'Nineteen'
}; //10~19的特別處理
const ten_str_dict={
'0':'none','1':'none','2':'Twenty','3':'Thirty','4':'Fourty','5':'Fifty','6':'Sixty','7':'Seventy','8':'Eighty','9':'Ninety'
}; //非10~19的十位數處理

//用正則把'123456'轉成'123,456'的函式
function numToCommaStr(input_num) {
return num.toString().replace(/(\d)(?=(?:\d{3})+\$)/g, "\$1,");
}

function numStrToFinanStr(input_str){
console.log('Input: '+input_str) // 傳入字串
const tmp_arr=input_str.split(',');
if(tmp_arr.length<2){
return strToDesStr(tmp_arr[0],'');
}else{
const aliasFlag=(tmp_arr.length==2?3:(tmp_arr.length==3?2:(tmp_arr.length==4?1:0)));
let outstr='';
for(let ti=0;ti<tmp_arr.length;ti++){
const tmpDesStr=strToDesStr(tmp_arr[ti],(aliasFlag+ti<4?alias_arr[aliasFlag+ti]:''));
outstr+=(ti>0&&(tmpDesStr!=' ')?', ':'')+tmpDesStr;
}
return 'Output: ' + outstr +'dollars.';
}
}

function strToDesStr(input_str,baseStr){
const tmp_num=parseInt(input_str[0]=='0'?input_str[1]+input_str[2]:input_str);
const hunNum=Math.floor(tmp_num/100);
const tenDigNum=Math.floor(tmp_num%100);
const tenNum=Math.floor(tenDigNum/10);
const digNum=Math.floor(tenDigNum%10);
//console.log('tmp_num:'+tmp_num+ ' hunNum:'+hunNum+' tenDigNum:'+tenDigNum+ ' tenNum:'+tenNum+ ' digNum:'+digNum);
let tmpStr = (hunNum!==0)? dig_str_dict[hunNum.toString(10)] + ' hundred':'';
if(tenDigNum>19||tenDigNum<10){
//非10~19在這邊處理
tmpStr+= (tmpStr!=''?' and ':'')+(tenNum!=0?ten_str_dict[tenNum.toString(10)]+' ':'') + (digNum!=0?dig_str_dict[digNum.toString(10)]:'');
}else{
//10~19在這邊處理
tmpStr+= (tmpStr!=''?' and ':'')+ teen_str_dict[tenDigNum.toString(10)];
}
return tmpStr +' '+ baseStr;
}

let num='10000';
console.log(numStrToFinanStr(numToCommaStr(num)));

num='1999999';
console.log(numStrToFinanStr(numToCommaStr(num)));

num='123456789';
console.log(numStrToFinanStr(numToCommaStr(num)));
</script>
``````

1.先宣告相關字典變數 dig_str_dict 處理個位數與百位數 , teen_str_dict 處理10~19數字 ,ten_str_dict處理十位數
2.輸入字串加上千分符的部份用正則處理
3.然後把 輸入字串加上千分符 用','分割傳回陣列 tmp_arr
4.最後用 tmp_arr 跑一次廻圈套字典處理完就完成了。

===

``````<script>
let cardNo=1;
cardFace=cardNo>2&&cardNo<11?cardNo:(cardNo==11?'J':(cardNo==12?'Q':(cardNo==13?'K':'A')));
/* 上面這行等同於 if(cardNo>2&&cardNo<11){...}else if(cardNo==11){...}else if(cardNo==12){...}else if(cardNo==13){...}else{...} */
console.log(cardFace);
</script>
``````
0
ckp6250
iT邦新手 2 級 ‧ 2019-12-04 17:33:47

0
marlin12
iT邦新手 1 級 ‧ 2019-12-05 00:28:58

C# 程式在綫測試

``````using System;

class Program
{
static void Main()
{
Console.WriteLine( NumberToWord(10000) );

Console.WriteLine( NumberToWord(1999999) );

Console.WriteLine( NumberToWord(123456789) );
}

static string NumberToWord(int value)
{
string[] scaleWords = { "", "throusand", "million", "billion" };

string result = "";

for(int x = 0; value != 0; x++, value /= 1000)
{
string block = ThreeDigitsToWord(value % 1000);

if(block.Length > 0) {
result = block + scaleWords[x] + ((result.Length > 0) ? "," + result : ((x > 0) ? " " : ""));
}
}

return FirstCharToUpper(result + "dollars");
}

static string ThreeDigitsToWord(int value)
{
string[] under20Words = { "", "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine ", "ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen " };

string[] tenthWords = { "", "", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety " };

int valDiv100 = value / 100;

string front = (valDiv100 > 0) ? under20Words[valDiv100] + "hundred " : "";

int valMod100 = value % 100;

string back = (valMod100 < 20) ? under20Words[valMod100] : tenthWords[valMod100 / 10] + under20Words[valMod100 % 10];

return front + (((front.Length * back.Length) > 0) ? "and " : "") + back;
}

static string FirstCharToUpper(string input) =>
char.ToUpper(input[0]) + input.Substring(1);
}
``````
0

iT邦新手 5 級 ‧ 2019-12-09 16:35:47

use swift

``````import Foundation

let format = NumberFormatter()
format.numberStyle = .spellOut
format.locale = Locale(identifier: "en")
for value in [10000, 1999999, 123456789] {
print("input : \(value)")

let number = NSNumber(value: value)
print("ouput : \(format.string(from: number) ?? "--") dollars")
}
``````