项目euler problem17 vb代码bug

时间:2015-02-03 14:38:07

标签: vb.net

如果数字1到5用文字写出:一,二,三,四,五,那么总共有3 + 3 + 5 + 4 + 4 = 19个字母。

如果所有1到1000(一千)的数字都用文字写出来,会用多少个字母?

注意:不要计算空格或连字符。例如,342(三百四十二)包含23个字母,115(一百一十五)包含20个字母。在写出数字时使用“和”符合英国的用法。

我的回答是21148.我发现这里的错误是我在vb中的代码 答案应该是21124

    Dim length As UInt32 = 11
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 1 To 999
    If i / 10 < 1 Then
        single_digit(i)
    ElseIf i / 10 = 1 Then
        ten()
    ElseIf i / 10 > 1 And i / 10 < 2 Then
        ele_twe_thi(i)
    ElseIf i / 10 >= 2 And i / 10 < 10 Then
        multiple_10(Math.Floor(i / 10) * 10)
        single_digit(i Mod 10)
    ElseIf i = 100 Then
        single_digit(i / 100)
        length += 7
    ElseIf i Mod 100 >= 11 And i Mod 100 <= 19 Then
        length += 10
        single_digit(Math.Floor(i / 100))
        ele_twe_thi(i Mod 100)
    ElseIf i Mod 100 = 10 Then
        length += 3
        single_digit(Math.Floor(i / 100))
        length += 10
    Else
        length += 10
        single_digit(Math.Floor(i / 100))
        multiple_10(Math.Floor((i Mod 100) / 10) * 10)
        single_digit(i Mod 10)
    End If
Next
MsgBox(length)
End Sub
Private Sub single_digit(num)
If num = 1 Then
    length = length + 3
ElseIf num = 2 Then
    length = length + 3
ElseIf num = 3 Then
    length = length + 5
ElseIf num = 4 Then
    length = length + 4
ElseIf num = 5 Then
    length = length + 4
ElseIf num = 6 Then
    length = length + 3
ElseIf num = 7 Then
    length = length + 5
ElseIf num = 8 Then
    length = length + 5
ElseIf num = 9 Then
    length = length + 4
End If
End Sub
Private Sub ele_twe_thi(num)
If num = 11 Or num = 12 Then
    length = length + 6
ElseIf num = 13 Or num = 14 Or num = 18 Or num = 19 Then
    length = length + 8
ElseIf num = 17 Then
    length = length + 9
ElseIf num = 15 Or num = 16 Then
    length = length + 7
End If
End Sub
Private Sub multiple_10(num)
If num = 20 Then
    length = length + 6
ElseIf num = 30 Then
    length = length + 6
ElseIf num = 40 Then
    length = length + 5
ElseIf num = 50 Then
    length = length + 5
ElseIf num = 60 Then
    length = length + 5
ElseIf num = 70 Then
    length = length + 7
ElseIf num = 80 Then
    length = length + 6
ElseIf num = 90 Then
    length = length + 6
End If
End Sub
Private Sub ten()
length = length + 3
    End Sub

1 个答案:

答案 0 :(得分:0)

每次程序达到100,200,300等时,是否会不必要地添加“和”?