+ -
当前位置:首页 → 问答吧 → 大家交流一下方法

大家交流一下方法

时间:2011-11-22

来源:互联网

以下的数字密码中,每个字母其实代表一个独一无二的数字,而且符合下列条件: 
1. 任何一列中,最左边的数字不可为 "0" 
2. 字母与数字为一对一对应。即假设 M代表 "3", 则所有的M都为"3",而且其它字母皆不可为"3" 。 
  S E N D 
+ M O R E 
─ ─ ─ ─ ─ 
M O N E Y

上面这题目用VB做,我是用多层for循环做的,请问大家还有什么好的方法?大家交流一下

作者: yonex00345   发布时间: 2011-11-22

9567
+ 1085
-------
 10652

作者: worldy   发布时间: 2011-11-22

我是问这题目用VB做,有什么好的方法

作者: yonex00345   发布时间: 2011-11-22

引用 2 楼 yonex00345 的回复:
我是问这题目用VB做,有什么好的方法


最简单就是用循环咯
Private Sub Command1_Click()
' S E N D
'+ M O R E
'─ ─ ─ ─ ─
'M O N E Y

Dim s As Long
Dim e As Long
Dim n As Long
Dim d As Long
Dim m As Long
Dim o As Long
Dim r As Long
Dim y As Long

For s = 1 To 9 's<>0
  For e = 0 To 9
  If e <> s Then
  For n = 0 To 9
  If n <> s And n <> e Then
  For d = 0 To 9
  If d <> s And d <> e And d <> n Then
  For m = 1 To 9
  If m <> s And m <> e And m <> n And m <> d Then
  For o = 0 To 9
  If o <> s And o <> e And o <> n And o <> d And o <> m Then
  For r = 0 To 9
  If r <> s And r <> e And r <> n And r <> d And r <> m And r <> o Then
  For y = 0 To 9
  If y <> s And y <> e And y <> n And y <> d And y <> m And y <> o And y <> r Then
  If 1000 * (s + m - o) + 100 * (e + o - n) + 10 * (n + r - e) + d + e - y - 10000 * m = 0 Then
  Debug.Print s & e & n&; d & "+" & m & o & r & e & "=" & m & o & n & e & y
  End If
  End If
  Next
  End If
  Next
  End If
  Next
  End If
  Next
  End If
  Next
  End If
  Next
  End If
  Next
Next
End Sub

作者: worldy   发布时间: 2011-11-22