编程 统计数字问题
- 提问者网友:欲望失宠
- 2021-05-24 06:46
- 五星知识达人网友:从此江山别
- 2021-05-24 07:24
不知道你想用什么语言编程来实现,不过算法都是一样的,我用vb语言写的程序给你做的实例,如需其他语言来实现,可以再联系,程序如下:
在form中添加一个按钮Command1,一个文本框text1,一个列表框list1,加入下面的代码:
Private Sub Command1_Click()
Dim j, n, num As Long
Dim k As Integer, a(9) As Integer, i As Integer
List1.Text = ""
n = CInt(Text1.Text)
For j = 1 To n
num = j
k = Len(CStr(num))
For i = 1 To k
Select Case num Mod 10
Case 0: a(0) = a(0) + 1
Case 1: a(1) = a(1) + 1
Case 2: a(2) = a(2) + 1
Case 3: a(3) = a(3) + 1
Case 4: a(4) = a(4) + 1
Case 5: a(5) = a(5) + 1
Case 6: a(6) = a(6) + 1
Case 7: a(7) = a(7) + 1
Case 8: a(8) = a(8) + 1
Case 9: a(9) = a(9) + 1
End Select
num = Int(num / 10)
Next i
Next j
For i = 0 To 9
List1.AddItem "数字" & i & "的个数为: " & a(i)
Next i
End Sub
运行结果如图所示:
- 1楼网友:蕴藏春秋
- 2021-05-24 08:57
不知道你想用什么语言编程来实现,不过算法都是一样的,我用vb语言写的程序给你做的实例,如需其他语言来实现,可以再联系,程序如下:
在form中添加一个按钮Command1,一个文本框text1,一个列表框list1,加入下面的代码:
Private Sub Command1_Click() Dim j, n, num As Long Dim k As Integer, a(9) As Integer, i As Integer List1.Text = "" n = CInt(Text1.Text) For j = 1 To n num = j k = Len(CStr(num)) For i = 1 To k Select Case num Mod 10 Case 0: a(0) = a(0) + 1 Case 1: a(1) = a(1) + 1 Case 2: a(2) = a(2) + 1 Case 3: a(3) = a(3) + 1 Case 4: a(4) = a(4) + 1 Case 5: a(5) = a(5) + 1 Case 6: a(6) = a(6) + 1 Case 7: a(7) = a(7) + 1 Case 8: a(8) = a(8) + 1 Case 9: a(9) = a(9) + 1 End Select num = Int(num / 10) Next i Next j For i = 0 To 9 List1.AddItem "数字" & i & "的个数为: " & a(i) Next i End Sub 运行结果如图所示:
- 2楼网友:長槍戰八方
- 2021-05-24 08:15
不知道你想用什么语言编程来实现,不过算法都是一样的,我用vb语言写的程序给你做的实例,如需其他语言来实现,可以再联系,程序如下:
在form中添加一个按钮Command1,一个文本框text1,一个列表框list1,加入下面的代码:
Private Sub Command1_Click() Dim j, n, num As Long Dim k As Integer, a(9) As Integer, i As Integer List1.Text = "" n = CInt(Text1.Text) For j = 1 To n num = j k = Len(CStr(num)) For i = 1 To k Select Case num Mod 10 Case 0: a(0) = a(0) + 1 Case 1: a(1) = a(1) + 1 Case 2: a(2) = a(2) + 1 Case 3: a(3) = a(3) + 1 Case 4: a(4) = a(4) + 1 Case 5: a(5) = a(5) + 1 Case 6: a(6) = a(6) + 1 Case 7: a(7) = a(7) + 1 Case 8: a(8) = a(8) + 1 Case 9: a(9) = a(9) + 1 End Select num = Int(num / 10) Next i Next j For i = 0 To 9 List1.AddItem "数字" & i & "的个数为: " & a(i) Next i End Sub 运行结果如图所示:
- 3楼网友:渡鹤影
- 2021-05-24 08:01