最新消息:

VB_Python代码对照算法百题(016)

Python 少儿编程 1537浏览 0评论

VB代码

 (2018年4月浙江)14. 某种编码以4位二进制码为一组,每组前两位表示方向,后两位表示距离。编写一个程序,将编码翻译成方向和距离,距离值为每组编码后两位二进制码转换为十进制数的值。具体功能如下:在文本框Text1中输入连续多组编码,单击“翻译”按钮Command1,结果显示 在列表框List1中。程序运行界面如图所示。

VB_Python代码对照算法百题(016)

(1)要使程序运行时,文本框Text1的Width属性值为2018,可在FormLoad事件过程中添加语句   (单选,填字母:A.Width = 2018 / B.Text1.Width=2018/ C. Width.Text1=2018)。 (2)实现上述功能的VB程序如下,请在划线处填入合适的代码。 Private Sub Command1_Click() Dim s As String, c As String, d As String Dim n As Integer, b1 As Integer, b2 As Integer, v As Integer, i As Integer s = Text1.Text: n = Len(s): i = 1 Do While i <= n

 c = Mid(s, i, 2)

 If c = “00″ Then

  d = “东”

 ElseIf c = “01″ Then

  d = “南”

 ElseIf c = “10″ Then

  d = “西”

 Else

  d = “北”

 End If

 b1 = Val(Mid(s, i + 2, 1))

 b2 = Val(Mid(s, i + 3, 1))

 v =       
 List1.AddItem d + ” ” + Str(v)

         
Loop End Sub (3)若文本框Text1中输入内容为“1111”,单击“翻译”按钮后,在列表框List1中显示的内容是      

参考答案:

(1)B

(2)①b12+b2 ②i = i + 4 

(3)北 3

Python代码:

s=input(‘请输入四位的倍数的二进制数:n’) n=len(s) i=0 while i<=n-4:  

 c=s[i:i+2]  

 if c==’00′:  

  d=’东’ 

 elif c==’01′:  

  d=’南’  

 elif c==’10′:  

  d=’西’  

 else:  

  d=’北’  

 b1=int(s[i+2])  

 b2=int(s[i+3])  

 v=b12+b2

 print(d,str(v))

 i=i+4

VB_Python代码对照算法百题(016)

Python代码运行结果:

VB_Python代码对照算法百题(016)

您必须 登录 才能发表评论!