5 月 17, 2015

博客咁耐毋更新,發覺似乎無乜嘢可以分享,既然博客都係寫俾自己睇,隨便發D做水題嘅日誌嚟填充博客= =

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

For example:

Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.

Follow up:
Could you do it without any loop/recursion in O(1) runtime?

不能用循環?先寫個用循環的列出幾個結果,再從中搵規律。

得到examples:

input / output
33 6
34 7
35 8
36 9
37 1
38 2
39 3
40 4
41 5
42 6
43 7
44 8
45 9
46 1

呃,看起來還是很有規律的。。。。於是得出 output = (input - 1) mod 9 + 1 (其實我都唔知點搞出嚟嘅)

於是:

   

已有 1 條評論

  1. 日本大美人 9 年前 (2015-10-09)
    @

    我也想了解,请大家都说说