Board logo

標題: C number of digits [打印本頁]

作者: thomsonlee35    時間: 2008-10-16 22:14     標題: C number of digits

I learnt C in the class and I've a great difficulty.

The question is to find the number of digits of an input integer.

Can you help me? thanks
作者: MacMonster    時間: 2008-10-16 22:56

floor(log10(n) + 1)

注意: 個Result係double, 要轉番做integer. 零同負數會有問題.
作者: 99bbq    時間: 2008-10-16 22:58

Simplest method

divide by 10 then counting times of the digit until 0
作者: Guillo    時間: 2008-10-17 00:49

1234%10^0=0
1234%10^1=4
1234%10^2=34
1234%10^3=234
1234%10^4=1234

呢個方法都唔錯

作者: little_keung    時間: 2008-10-17 09:58

原帖由 MacMonster 於 2008-10-16 22:56 發表
floor(log10(n) + 1)

注意: 個Result係double, 要轉番做integer. 零同負數會有問題.


更簡單o既方法,  轉做 string , count string length .


更重要o既係, 呢個 algorithm 問題, 你諗掂o左個 algorithm 先考慮點樣 implement .

[ 本帖最後由 little_keung 於 2008-10-17 10:02 編輯 ]





歡迎光臨 電腦領域 HKEPC Hardware (https://h0.hkepc.com/forum/) Powered by Discuz! 7.2