ThoughtWorks Merchant's Guide To The Galaxy


ThoughtWorks筆試題之Merchant's Guide To The Galaxy解析

一、背景

在某網站上看到ThoughtWorks在武漢招人,待遇在本地還算不錯,就投遞了簡歷。第二天HR就打開電話,基本了解了一下情況(工作環境不錯,男人妹子比例:1:1,雙休,六險一金,滿一年年假15天,病假8天,月薪1W--2W)。然后立馬收到一封:Coding Assignment的筆試題目。網上搜索了一下,發現這個公司還是挺大的,公司面試流程是出了名的繁雜和苛刻。據說有8輪:電話面試=》筆試=》Homework=》結對編程(中午管飯)=》技術面試=》PM面試=》HR Manager的面試=》Boss面談。

了解到武漢這邊的主要是做知道華為這樣的公司做敏捷開發培訓,需要出差,也會有自己公司的一些些代碼的需求。這邊公司規模也就20幾個人。希望對其他想去的童鞋有所幫助。

管它呢,試試再說/本文重點說一下筆試題目,因為我覺得還挺有意思的。

 

不好意思,剛收到郵件通知,我己經陣亡,說是沒有適合我 skills and background 的職位。莫非跟這個題目有關,大家幫我看一下哦。

二、題目:

三個問題任選其一:我就選了第一個”Problem One: Merchant's Guide To The Galaxy“,另外兩道題有需要的原有找我要。

題干給大家貼出來,下面有中文說明,可跳過。

You decided to give up on earth after the latest financial collapse left 99.99% of the earth's population with 0.01% of the wealth. Luckily, with the scant sum of money that is left in your account, you are able to afford to rent a spaceship, leave earth, and fly all over the galaxy to sell common metals and dirt (which apparently is worth a lot).
 
Buying and selling over the galaxy requires you to convert numbers and units, and you decided to write a program to help you.
 
The numbers used for intergalactic transactions follows similar convention to the roman numerals and you have painstakingly collected the appropriate translation between them.
 
Roman numerals are based on seven symbols:

Symbol

Value

I

1

V

5

X

10

L

50

C

100

D

500

M

1,000

 
Numbers are formed by combining symbols together and adding the values. For example, MMVI is 1000 + 1000 + 5 + 1 = 2006. Generally, symbols are placed in order of value, starting with the largest values. When smaller values precede larger values, the smaller values are subtracted from the larger values, and the result is added to the total. For example MCMXLIV = 1000 + (1000 − 100) + (50 − 10) + (5 − 1) = 1944.
 
  • The symbols "I", "X", "C", and "M" can be repeated three times in succession, but no more. (They may appear four times if the third and fourth are separated by a smaller value, such as XXXIX.) "D", "L", and "V" can never be repeated.
  • "I" can be subtracted from "V" and "X" only. "X" can be subtracted from "L" and "C" only. "C" can be subtracted from "D" and "M" only. "V", "L", and "D" can never be subtracted.
  • Only one small-value symbol may be subtracted from any large-value symbol.
  • A number written in [16]Arabic numerals can be broken into digits. For example, 1903 is composed of 1, 9, 0, and 3. To write the Roman numeral, each of the non-zero digits should be treated separately. Inthe above example, 1,000 = M, 900 = CM, and 3 = III. Therefore, 1903 = MCMIII.
(Source: Wikipedia ( [17] http://en.wikipedia.org/wiki/Roman_numerals)
 
Input to your program consists of lines of text detailing your notes on the conversion between intergalactic units and roman numerals.
 
You are expected to handle invalid queries appropriately.
 
Test input:
glob is I
prok is V
pish is X
tegj is L
glob glob Silver is 34 Credits
glob prok Gold is 57800 Credits
pish pish Iron is 3910 Credits
how much is pish tegj glob glob ?
how many Credits is glob prok Silver ?
how many Credits is glob prok Gold ?
how many Credits is glob prok Iron ?
how much wood could a woodchuck chuck if a woodchuck could chuck wood ?
 
Test Output:
pish tegj glob glob is 42
glob prok Silver is 68 Credits
glob prok Gold is 57800 Credits
glob prok Iron is 782 Credits
I have no idea what you are talking about

 

可以用:Java, Ruby, or C#, JavaScript.

 

中文:大意就是我成了地球首富,然后有很多錢,但是地球已經不適合人類居住了,需要跑到銀河系去做生意,但是銀河系使用的是羅馬字母表示錢,所以座位程序員的我打算自己寫一個羅馬字母和十進制數字互轉的小程序,以幫助我做生意。

羅馬字母有: I,V,X,L,C,D,M

轉換規則

1. I:可以表示十進制數字1,V:可以表示十進制數字5,X:可以表示十進制數字10,L:可以表示十進制數字50,C:可以表示十進制數字100,D:可以表示十進制數字500,M:可以表示十進制數字1000;

2.I, X, C, and M可以重復出現最多3次;

3.一般是從后往前排列:即MDCLXVI的順序,當然也允許相鄰的兩個倒序,但是需要符合以下規則:

   I :只能組合IV ,IX

   X:只能組合XL,XC

   C:只能組合CD,CM

   V,L,D不能倒序

 

大概是這個意思,上周做的,現在大腦還殘存這些碎片。

 

三、我的思路

既然要考察面向對象的編程,就建一個Roman的類表示單個羅馬字母,根據轉換規則添加一些Property,如下:

  View Code

在新建一個類專門做轉換:

  View Code

程序輸出:

 

代碼不解釋,請大牛批評指正。

 代碼下載:http://files.cnblogs.com/deepleo/RomanNumber.rar


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM