1 / 100

超過 int 和 long long 能存的長度

大數. 超過 int 和 long long 能存的長度. int int 宣告的變數最大只能儲存 大約 10 位數 ,也就是 2 的 32 次方的數字 int (4bytes) -2,147,483,648 ~ 2,147,483,647. 那超過 -2,147,483,648 ~ 2,147,483,647 這個範圍的數字怎麼辦呢? 翻開課本好像沒有的那一頁 C++ 提供的整數變數型態有幾種選擇: char (1byte) -128 ~ 127 short (2bytes) -32768 ~ 32767

helia
Télécharger la présentation

超過 int 和 long long 能存的長度

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 大數 超過int和long long能存的長度

  2. int int宣告的變數最大只能儲存 大約10位數,也就是 2的32次方的數字 int (4bytes) -2,147,483,648 ~ 2,147,483,647

  3. 那超過-2,147,483,648 ~ 2,147,483,647 這個範圍的數字怎麼辦呢? 翻開課本好像沒有的那一頁 C++提供的整數變數型態有幾種選擇: char (1byte) -128 ~ 127 short (2bytes) -32768 ~ 32767 int (4bytes) -2,147,483,648 ~ 2,147,483,647 long (4bytes) -2,147,483,648 ~ 2,147,483,647 long long (8bytes) -263 ~ 263 -1

  4. 讓我們來小小計算一下 哇~~263是9,223,372,036,854,775,808 long long的儲存範圍有19位數呢, 遠高過int的10位數 ^^ 可是!! 如果我們想計算的數字還是超過19位數, 該怎麼辦? 數字太大了,該怎麼辦?

  5. 想想看數字加法的原理 我們都是怎麼加的呢? 怎麼把兩個數字加起來變成第三個新數字? 看一個例子: 2468 + 2468

  6. 2468 + 2468 2 4 6 8 +) 2 4 6 8 __________________

  7. 2468 + 2468 2 4 6 8 +) 2 4 6 8 __________________ 16

  8. 2468 + 2468 2 4 6 8 +) 2 4 6 8 __________________ 16 ㄜ喔~~糟糕了 個位數超過10啦 十進位裡一個位數是不能超過10的喔

  9. 2468 + 2468 1 把他進到下一位 2 4 6 8 +) 2 4 6 8 __________________ 6

  10. 2468 + 2468 1 2 4 6 8 +) 2 4 6 8 __________________ 13 6 ㄜ喔~~又超過10啦

  11. 2468 + 2468 再進一次位 1 1 2 4 6 8 +) 2 4 6 8 __________________ 3 6

  12. 2468 + 2468 1 1 2 4 6 8 +) 2 4 6 8 __________________ 9 3 6 繼續加

  13. 2468 + 2468 1 1 2 4 6 8 +) 2 4 6 8 __________________ 4 9 3 6

  14. 2468 + 2468 1 1 2 4 6 8 +) 2 4 6 8 __________________ 4 9 3 6 大功告成 ^O^

  15. 有沒有發現,我們都把一個個位數當個體喔 一個位數滿了才到下一位去 所以!! 數字有沒有變得很像什麼東西呢? 一長串的又各自獨立? 一長串又各自獨立

  16. 不要懷疑 他就是array啦~~

  17. 用array來重複之前的運算 +)

  18. 一樣從個位開始加 +)

  19. ㄜ喔 超過10了喔~ +)

  20. 進到carry去 讓十位知道個位數有進位 +)

  21. 十位數相加 記得要加carry +)

  22. 又超過10 再放入carry裡 不過這次是進到百位喔~ +)

  23. 繼續加 百位 +)

  24. 千位 +)

  25. 大功告成 ^O^ +)

  26. 注意!!最後要檢查carry還有沒有進位喔~ +)

  27. 大數,就這麼產生了!! 大數就是 用array來擴大儲存範圍的一種方法 回想小時候從個位數一位一位慢慢加上去 用這種方式來寫大數就對了 要一位一位慢慢加上去喔~~

  28. 除了用int array來儲存大數外 還有一種常見的儲存方式是用char array char array的優點是可以一次用cin.getline() 讀入 (不過10013的數字不在同一行就不行啦) 像這樣: input  6 5 4 3 code  cin.getline(charArr, 100, ‘\n’);

  29. 還可以直接output整個字串 像這樣: code  cout << charArr; input  6 5 4 3 印到‘\0’結束 (ASCII code == 0) 要記得在每個字串後面加’\0’喔~ 不然會Runtime Error (不知道印到哪去了)

  30. 至於要用int array還是char array呢? 這就 ~~看個人喜歡啦~~ 因為各有優缺點, 學弟妹可以兩種都試試看喔

  31. 寫大數會遇到最大的問題就是進位了吧 英文叫carry 所以我們拿carry當作進位變數的名子 1234 + 1234 = 2468 沒有任何進位發生

  32. 2468 + 2468 = 4936 進位了兩次

  33. 6543 + 6543 = 13086 不但進位了兩次,看! 數字多一位了 多出來的那一位怎麼辦!!?

  34. 6543 + 6543 = 13086 不要擔心 不要擔心~~ 我們使用的是array耶,不夠存 再加位子就好啦 只是,我們怎麼知道什麼時候要加呢?

  35. 以加法來說 最多就是 9 + 9 + carry 最多進一位 所以只要比最長的數字多一位就好

  36. 以減法來說 不但不會多位數,或許還會少呢 ^^” 所以存答案的array跟最長的數字一樣長就好

  37. 以乘法來說 第一個數字的每位數都要乘第二個數字一次 再全部加起來 所以答案array的長度是所有數字的長度相加

  38. 至於怎麼知道各個數字的長度呢? .. ..嘿嘿嘿.. ..

  39. 用int array的人會開始羨慕用char array的人 因為int array要用迴圈跑到數字結束 才知道長度 char array則用strlen()就行嘍 int array: for(i = 0 ; intArr[i] != -9999 ; i++){} int len = i; char array: int len = strlen(charArr);

  40. 在聽完有關大數的一些注意事項後 我們來講乘法吧 :D 一起回想國小的直式乘法 ^^

  41. 直式乘法 1 2 3 4 ×) 3 4 16

  42. 直式乘法 1 1 2 3 4 ×) 3 4 6

  43. 直式乘法 1 1 2 3 4 ×) 3 4 13 6

  44. 直式乘法 1 1 1 2 3 4 ×) 3 4 3 6

  45. 直式乘法 1 1 1 2 3 4 ×) 3 4 9 3 6

  46. 直式乘法 1 1 1 2 3 4 ×) 3 4 9 3 6

  47. 直式乘法 1 1 1 2 3 4 ×) 3 4 4 9 3 6

  48. 直式乘法 個位數結束~~ (換乘十位數) 1 2 3 4 ×) 3 4 4 9 3 6

  49. 直式乘法 1 2 3 4 ×) 3 4 4 9 3 6 12

  50. 直式乘法 1 1 2 3 4 ×) 3 4 4 9 3 6 2

More Related