CS50 week1作業

Problem1 hello world

  1. $mkdir pset1   //創建一個目錄pset1
  2. $cd ./pset1    //進入pset1目錄
  3. check50 2016.hello hello.c      //CS50 IDE提供的檢查檔案是否正確
  4. submit50 cs50/2017/x/hello           //提交檔案如下圖所示








Problem2 water

Suffice it to say that the longer you shower, the more water you use. But just how much? Even if you have a "low-flow" showerhead, odds are your shower spits out 1.5 gallons of water per minute. A gallon, meanwhile, is 128 ounces, and so that shower spits out 1.5 × 128 = 192 ounces of water per minute. A typical bottle of water (that you might have for a drink, not a shower), meanwhile, might be 16 ounces. So taking a 1-minute shower is akin to using 192 ÷ 16 = 12 bottles of water. Taking (more realistically, perhaps!) a 10-minute shower, then, is like using 120 bottles of water. Deer Park, that’s a lot of water! Of course, bottled water itself is wasteful; best to use reusable containers when you can. But it does put into perspective what’s being spent in a shower!








Problem3 mario.less



Problem3 mario.more



Problem4 greedy
如果直接用float去做會出錯,因為在C當中的某些decimal的小數無法用binary表示,所以我的方法是*100,在做四捨五入
O hai! How much change is owed?
0.38   #input is 0.38
float = 0.37999999523  #但出來的數字卻是0.37999999523
round to float = 38.000000  #此動作是做過*100跟四捨五入的



Problem5 credit

首先要知道題目再說什麼XD,超長QQ,他在說信用卡的卡號是用一個Luhn algorithm來校驗是否正確,又稱mod 10演算法

以下分為三種卡:
  1. 美國運通 15-bits 開頭是34,37
  2. Master Card 16-bits 開頭是51,52,53,54,55
  3. Visa 13 and 16-bits 開頭是4
Step1.從倒數最後二位開始,由右往左分別乘以2,拉出來
Step2.將這些數字相加的"位元"相加,在與沒拉出來的位元加總,得值
Step3.若此值是十的倍數則合法,否則違法

留言

這個網誌中的熱門文章

面試 (網路搜尋的資源)

bitwise operation 面試考題