{"action":"opened","issue":{"url":"https://api.github.com/repos/zeqing-guo/algorithms-study/issues/5","labels_url":"https://api.github.com/repos/zeqing-guo/algorithms-study/issues/5/labels{/name}","comments_url":"https://api.github.com/repos/zeqing-guo/algorithms-study/issues/5/comments","events_url":"https://api.github.com/repos/zeqing-guo/algorithms-study/issues/5/events","html_url":"https://github.com/zeqing-guo/algorithms-study/issues/5","id":114759580,"number":5,"title":"258: Add Digits","user":{"login":"zeqing-guo","id":4294005,"avatar_url":"https://avatars.githubusercontent.com/u/4294005?v=3","gravatar_id":"","url":"https://api.github.com/users/zeqing-guo","html_url":"https://github.com/zeqing-guo","followers_url":"https://api.github.com/users/zeqing-guo/followers","following_url":"https://api.github.com/users/zeqing-guo/following{/other_user}","gists_url":"https://api.github.com/users/zeqing-guo/gists{/gist_id}","starred_url":"https://api.github.com/users/zeqing-guo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zeqing-guo/subscriptions","organizations_url":"https://api.github.com/users/zeqing-guo/orgs","repos_url":"https://api.github.com/users/zeqing-guo/repos","events_url":"https://api.github.com/users/zeqing-guo/events{/privacy}","received_events_url":"https://api.github.com/users/zeqing-guo/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/zeqing-guo/algorithms-study/labels/leetcode","name":"leetcode","color":"f7c6c7"},{"url":"https://api.github.com/repos/zeqing-guo/algorithms-study/labels/Math","name":"Math","color":"006b75"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-11-03T07:14:35Z","updated_at":"2015-11-03T07:14:35Z","closed_at":null,"body":"# Add Digits #\r\n\r\n## Describtion ##\r\n\r\nGiven a non-negative integer `num`, repeatedly add all its digits until the result has only one digit.\r\n\r\nFor example:\r\n\r\nGiven `num = 38`, the process is like: `3 + 8 = 11`, `1 + 1 = 2`. Since `2` has only one digit, return it.\r\n\r\n## Solution ##\r\n\r\n程序的run time是2ms，完整代码如下：\r\n\r\n``` java\r\npublic class Solution {\r\n    public int addDigits(int num) {\r\n        if (num == 0) {\r\n            return 0;\r\n        }\r\n        int ans = num % 9;\r\n        ans = (ans == 0) ? 9 : ans;\r\n        return ans;\r\n    }\r\n}\r\n```\r\n\r\n## Analysis ##\r\n\r\n这道题需要使用基础的数论知识。假设$\\overline{abcd}$是一个四位正整数，则有$1000 * a + 100 * b + 10 * c + d \\equiv a + b + c + d \\pmod{9}$。然后再对$a + b + c + d$的和做同样的事情直到和变为个位数。此时我们发现$\\overline{abcd}$模9和将$\\overline{abcd}$各位反复相加直到得到的个位数同余。由于我们模的是9，因此$\\overline{abcd}$除以9得到的余数就是答案。\r\n\r\n## Reasons For Not Accepted ##\r\n\r\n没审清楚题目，题目说的是非负数。\r\n"}}