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

floor(log10(n) + 1)

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

TOP

Simplest method

divide by 10 then counting times of the digit until 0

TOP

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

呢個方法都唔錯

TOP

原帖由 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 編輯 ]

TOP