0—9数字组成abcdefghi数,其中满足a能被1整除,ab能被2整除.以此类推,abcdefghi能被9整除,求
0—9数字组成abcdefghi数,其中满足a能被1整除,ab能被2整除.以此类推,abcdefghi能被9整除,求
abcdefghi各为多少?我编了一个程序,但是算不对,希望各位不吝赐教#include
#include
void main()
{
\x05int a,b,c,d,e,f,g,h,i,k;
for(a=1;a
不好意思,最后那个k该写成sum,没注意!
本人只会VB代码,所以只能提供思路和结果:
以0-9不重复使用,符合要求的数有(EXCEL VBA 测试):
381654720
381654729
783204165
801654723
另:081654327 (因为0在首位,所以只有8位数,不符合要求)
计算思路如下(可以根据以下思路检查代码过程逻辑顺序是否正确):
abcdefg分别从0-9依次循环
0-9循环时首先判断是否与前位重复(a判断是否为0)
不重复则判断前面已当前数组成的数是否能被位数整除
如果能整除(如果为i循环则生成结果)则继续
EXCEL VBA 代码如下:
Sub abcdefghi()
For a = 0 To 9 Step 1
If a 0 Then
s = a
If Val(s) Mod Len(s) = 0 Then
For b = 0 To 9 Step 1
If b a Then
s = a & b
If Val(s) Mod Len(s) = 0 Then
For c = 0 To 9 Step 1
If c a And c b Then
s = a & b & c
If Val(s) Mod Len(s) = 0 Then
For d = 0 To 9 Step 1
If d a And d b And d c Then
s = a & b & c & d
If Val(s) Mod Len(s) = 0 Then
For e = 0 To 9 Step 1
If e a And e b And e c And e d Then
s = a & b & c & d & e
If Val(s) Mod Len(s) = 0 Then
For f = 0 To 9 Step 1
If f a And f b And f c And f d And f e Then
s = a & b & c & d & e & f
If Val(s) Mod Len(s) = 0 Then
For g = 0 To 9 Step 1
If g a And g b And g c And g d And g e And g f Then
s = a & b & c & d & e & f & g
If Val(s) Mod Len(s) = 0 Then
For h = 0 To 9 Step 1
If h a And h b And h c And h d And h e And h f And h g Then
s = a & b & c & d & e & f & g & h
If Val(s) Mod Len(s) = 0 Then
For i = 0 To 9 Step 1
If i a And i b And i c And i d And i e And i f And i g And i h Then
s = a & b & c & d & e & f & g & h & i
If Val(s) Mod Len(s) = 0 Then
结果 = 结果 & vbCrLf & s
End If
End If
Next i
End If
End If
Next h
End If
End If
Next g
End If
End If
Next f
End If
End If
Next e
End If
End If
Next d
End If
End If
Next c
End If
End If
Next b
End If
End If
Next a
MsgBox "0-9 不重复使用组成 9 位数 abcdefghi,满足以下要求:" & vbCrLf & vbCrLf & "左起 1-9 位能被对应的位数整除.即:" & vbCrLf & "a 能被 1 整除." & vbCrLf & "ab 能被 2 整除." & vbCrLf & "." & vbCrLf & "abcdefghi 能被 9 整除." & vbCrLf & vbCrLf & "符合要求的结果如下:" & 结果,vbInformation,"0-9 组成的 9 位数"
End Sub