【发布时间】:2015-09-07 18:18:55
【问题描述】:
考虑自然数 N 的十进制表示。通过排列给定数字中的数字,找到所有数字的最大公约数 (GCD)。允许前导零
我不想要代码,只想要解决问题的逻辑 http://www.spoj.com/problems/GCD/
这是我尝试的伪代码:
if sum of digits divide by 3 then k=3
if sum of digits divide by 9 then k=9
else k=1
if all digits divide by 2 then o=2
if all digits divide by 4 then o=4
if all digits divide by 8 then o=8
if all digits divide by 5 then o=5
if all digits divide by 7 then o=7
else o=1
if all digits are same , print itself
else print o*k
但我每次都得到错误的答案。
【问题讨论】:
-
链接不应该在标题中:(你必须在你自己的问题中给出问题的简历。
-
对不起..我的第一个问题。
-
我也见过stackoverflow.com/questions/25698691/… ...那里的答案不完整。
-
我想你会想找到一种方法来迭代值的范围。您需要使用欧几里得算法并使用模算术测试可除性。
-
考虑 3699。所有排列的 gcd 为 27。您的算法返回 9 (k=9, o=1)。
标签: algorithm math recursion permutation