辐射岛第一塔位置:一道vb 题目 请高手解答

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/28 19:46:41
要求完成下面的表达式
a b c d e
+2 0 0 8 5
------------
f g h i j
其中2008表示2008年在北京举办奥运会,
5表示奥运五环,所以合起来20085刚好表示2008奥运。
要求abcdefghij分别表示从0到9的一位数字,而且不允许重复使得上面的加法表达式成立
用vb 来解决着道题
编程不要太大
请大家用vb

我用的是很直接的方法: 逐数检查,然后把所有合格的显示出来。
运算的结果是发现六对合格的数字。

主函数 Form_Load 里调用 Format 是为了确保检查重复性的时候,
abcde 足五个字符。 (若不足,Format 会往左边塞入适量的“0”。)

为了简化代码,我把检查重复性的代码写进函数 noRepeated 里。

值得一提的是,刚刚我用 Python 写了同样的程序,发现只用了五行代码。

VB 的完整代码如下。有疑问尽管问。

Private Sub Form_Load()
    ' abcde
    '+ 20085
    '-------
    ' fghij
    '-------

    For abcde = 0 To 99999
        abcde = Format(abcde, "00000")
        fghij = abcde + 20085
        If noRepeated(abcde & fghij) Then
            result = result & abcde & " : " & fghij & vbNewLine
        End If
    Next

    MsgBox "abcde : fghij" & vbNewLine & vbNewLine & result

End Sub

Function noRepeated(s)

    noRepeated = True

    For i = 1 To Len(s) - 1
        sought = Mid(s, i, 1)
        If InStr(i + 1, s, sought) Then
            noRepeated = False
            Exit Function
        End If
    Next
End Function

for(a=0;a<=9;a++)
for(b=0;b<=9;b++)
{
if(b==a)continue;
else
for(c=0;c<=9;c++)
{
if(c==a||c==b)continue;
else
for(d=0;d<=9;d++)
{
if(d==a||d==b||d==c)
continue;
....
for(j=0;j<=9;j++)
{
if(a==j||b==j||c==j||d==j...||i==j)
continue;
else if((a*10000+b*1000+c*100+d*10+e)+20085==(f*10000+g*1000+h*100+i*10+j)
goto end;

}
}
}
}
end:
//get the result;

楼上的同志请注意了,楼主是需要VB语言的编程,而不是C语言。

把c语言编的改成vb不就行了

for a=0 to 9
for b=0 to 9

if(b<>a)
for b=0 to 9
{
if(c<>a and c<>b)then
for b=0 to 9
{
if(d<>a and d<>b and d<>c)

....
for i=0 to 9

if(a<>j||b<>j||c<>j||d<>j...||i<>j)

else if((a*10000+b*1000+c*100+d*10+e)+20085==(f*10000+g*1000+h*100+i*10+j)
goto end

next

end:
//get the result;

14982+20085=35067
写下此程序仅供参考!
另外,我的QQ号是110807087,有事可以联系!

'————————以下是该程序的代码————————
Dim data(1 To 10) As Integer
Private Sub Form_Click()
Dim i As Integer
For i = 1 To 9
data(1) = i
Call lp(i)
Next i
MsgBox "刚刚完成运算^-^"
End Sub
Private Sub lp(ByVal old As Integer)
Dim j As Integer
For i = 0 To 9
For j = 1 To old
If i = data(j) Then
Exit For
End If
Next j
If j = old + 1 Then
data(j) = i
If j = 10 Then
Call check
Else
Call lp(j)
End If
End If
Next i
End Sub
Private Sub check()
Dim total As Double, add1 As Double, add2 As Double
add1 = data(1) * 1000 + data(2) * 100 + data(3) * 10 + data(4) + data(5) * 0.1
add2 = 2008.5
total = data(6) * 1000 + data(7) * 100 + data(8) * 10 + data(9) + data(10) * 0.1
If add1 + add2 = total Then
MsgBox add1 * 10 & " +" & add2 * 10 & "=" & total * 10
End If
End Sub