编写程序,把具有这种特征的一对两位正整数都找出来。

来源:百度知道 编辑:UC知道 时间:2024/06/07 01:07:23
一个两位的正整数,如果将它的个位数字与十位数字对调,则产生另一个正整数,我们把后者叫做前者的对调数。现给定一个两位的正整数,请找到另一个两位的正整数,使得这两位正整数之和等于它们各自的对调数之和。例如,12+32=44=23+21。要用两个text,一个按钮控件。

假设在Text1中输入给定的正整数,在Text2中输出结果

在按钮事件中的代码就是:
dim a as integer,b as integer,x as integer,y as integer
dim i as integer
dim Find as boolean
find=false
a=val(text1.text) mod 10
b=val(text1.text) \ 10
for i=11 to 99
x=i mod 10 :y=i\10
if a*10+b+x*10+y=b*10+a+y*10+x then
text2.text=i
find=true
exit for
end if
next
if find=false then msgbox "没有这样的数"

其实可以用一个幽默点的办法:
a*10+b+x*10+y=b*10+a+y*10+x
即 a*9+x*9=b*9+y*9
即 a+x=b+y
dim a as integer,b as integer
dim i as integer,j as integer
dim Find as boolean
find=false
a=val(text1.text) mod 10
b=val(text1.text) \ 10
for i=1 to 9
for j=1 to 9
if a+i=b+j then
text2.text=i*10+j
find=true
exit for
end if
next
next
if find=false then msgbox "没有这样的数"

两个text,将text2设为可接收多行文本,因为答案不唯一