帮我设计个算法啊

来源:百度知道 编辑:UC知道 时间:2024/06/20 11:16:01
编程求12709到45368之间有多少个其各位数字之和为3的倍数的正整数

设有a、b、c、d、e五个数字,

则令x=a*10000+b*1000+c*100+d*10+e
y=a+b+c+d+e
循环(嵌套5层)
for a=1 to 4
for b=0 to 9
for c=0 to 9
for d=0 to 9
for e=0 to 9

当: x>=12709 and x<=45368 时
如果 y/3=0 则输出 a、b、c、d、e
next e
next d
next c
next b
next a

各位数字之和为3的倍数的正整数即为3的倍数,因此:
ans=((45368 div 3)-(12709 div 3))+1
O(1)的复杂度...