多字符串比较

jp0077777 2009-08-23 12:58:58
有10个字符串固定不变
"1 2 3 4 5"
"2 3 4 5 6"
"3 4 5 6 7"
"4 5 6 7 8"
"5 6 7 8 9"
"6 7 8 9 10"
"7 8 9 10 11"
"8 9 10 11 12"
"9 10 11 12 13"
"10 11 12 13 14"

另有一字符串s 可变化 "3 8 9 4 10 7"
求字符串s 在上面10个字符串中有4个且仅有4个数字相同 并且显示出来
如例子中的结果为"6 7 8 9 10" "7 8 9 10 11" 并且要显示出那个不同的数字 如"6 7 8 9 10"则为6 "7 8 9 10 11"则为11

有好一点的方法吗?
是不是要用到字符串数组?
谢了
...全文
260 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
jp0077777 2009-08-24
  • 打赏
  • 举报
回复
谢了 辛苦了各位
vbman2003 2009-08-24
  • 打赏
  • 举报
回复
昨天那台电脑没中文输入法,也没VB,随手写了个思路,今天改一下贴一下吧...


Private Function NumComp(pStr As String, bol() As Boolean) As String
Dim a
Dim b() As Boolean
Dim N As Long, i As Long

b = bol
a = Split(pStr, Chr(32))
For i = 0 To UBound(a)
If b(a(i)) Then
N = N + 1
b(i) = False
a(i) = ""
End If
Next
If N = 4 Then
NumComp = pStr & " '" & Trim(Join(a))
End If
End Function

Private Sub Command1_Click()

Const N = 99 '假设你最大数是99
Dim s As String
Dim s1(9) As String
Dim arr
Dim ii As Long
Dim bol(N) As Boolean

s1(0) = "1 2 3 4 5"
s1(1) = "2 3 4 5 6"
s1(2) = "3 4 5 6 7"
s1(3) = "4 5 6 7 8"
s1(4) = "5 6 7 8 9"
s1(5) = "6 7 8 9 10"
s1(6) = "7 8 9 10 11"
s1(7) = "8 9 10 11 12"
s1(8) = "9 10 11 12 13"
s1(9) = "10 11 12 13 14"

s = "3 8 9 4 10 7"

arr = Split(s, Chr(32))
For ii = 0 To UBound(arr)
bol(arr(ii)) = True
Next

For ii = 0 To 9
arr = NumComp(s1(ii), bol)
If Len(arr) > 0 Then MsgBox arr
Next

End Sub
jhone99 2009-08-24
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim a(9) As String
Dim b As String
Dim intCount As Integer

a(0) = "1 2 3 4 5"
a(1) = "2 3 4 5 6"
a(2) = "3 4 5 6 7"
a(3) = "4 5 6 7 8"
a(4) = "5 6 7 8 9"
a(5) = "6 7 8 9 10"
a(6) = "7 8 9 10 11"
a(7) = "8 9 10 11 12"
a(8) = "9 10 11 12 13"
a(9) = "10 11 12 13 14"

b = "13 6 4 5 10 10"
b = " " & b & " "

For i = 0 To UBound(a)
intCount = 0

For j = 0 To UBound(Split(a(i), " "))
If InStr(b, " " & Split(a(i), " ")(j) & " ") > 0 Then
intCount = intCount + 1
Else
strtemp = Split(a(i), " ")(j)
End If
Next j

If intCount = 4 Then
Debug.Print a(i) & ", " & strtemp
End If
Next i

End Sub
chinaboyzyq 2009-08-23
  • 打赏
  • 举报
回复
我也学99的方式写给你,希望你还能看懂我的程序:
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim a(9) As String
Dim b As String
Dim intCount As Integer

a(0) = "1 2 3 4 5"
a(1) = "2 3 4 5 6"
a(2) = "3 4 5 6 7"
a(3) = "4 5 6 7 8"
a(4) = "5 6 7 8 9"
a(5) = "6 7 8 9 10"
a(6) = "7 8 9 10 11"
a(7) = "8 9 10 11 12"
a(8) = "9 10 11 12 13"
a(9) = "10 11 12 13 14"

b = "3 8 9 4 10 7"
'b = "13 6 4 5 10 10"
For i = 0 To 5
L = L + Len(Split(b)(i)) + 1
If InStr(L + 1, b, Split(b)(i)) Then
Mid(b, InStr(L + 1, b, Split(b)(i))) = "T"
End If
Next
For i = 0 To 9
intCount = 0
For j = 0 To UBound(Split(a(i)))
For k = 0 To UBound(Split(b))
If Split(b)(k) = Split(a(i))(j) Then
intCount = intCount + 1
Else
strtemp = Split(a(i))(j)
End If
Next k
Next j
If intCount = 4 Then
MsgBox a(i) & ", " & strtemp
End If
Next i
End Sub
chinaboyzyq 2009-08-23
  • 打赏
  • 举报
回复
现在看来,不是你不能编得NB,而是你懒得编得NB,我是不会给你三小时的钱编这样一个程序了,肯定赔本.
chinaboyzyq 2009-08-23
  • 打赏
  • 举报
回复
把-1去掉就OK了

For i = 0 To UBound(ss)
For j = i + 1 To UBound(ss)
If ss(i) = ss(j) Then
ss(j) = "t"
End If
Next
Next
jp0077777 2009-08-23
  • 打赏
  • 举报
回复
你们的程序编得太NB了 我看来是没机会超过你们了 我再进步也没用了 还是省点时间在其它方面发展吧 人还是要有自知之明的 给钱当然是先商量一个事做好了给多少钱啊 花多少时间是你
的事 再有我给你钱就是给10分钟的钱 因为你是10分钟做完 不可能因为你10分钟做完 我给你3小时的钱吧 没这么傻的老板吧 你给我钱才是3个小时的钱 因为我要3小时才做完 哈哈
chinaboyzyq 2009-08-23
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 jp0077777 的回复:]
猴子的程序也不错 但jhone99的就很NB了 一看就是专业的
[/Quote]
jhone99的程序的确不错!
不过我想是这样的,碰到问题首先要自己想办法去解决,然后再去对照理解学习最简洁的编程方式,我们才可以进步.

[我想是不是我干脆给钱他让他帮我编程得了]你当然可以这样;他10分钟做了你3小时做的事,只是不知道你是付10分钟的钱呢?还是付3小时的钱?
jp0077777 2009-08-23
  • 打赏
  • 举报
回复
我晕 程序都不对啊 JHONE99 和猴子的 当字符串为s = "13 6 4 5 10 10"时就都不对了
jhone99 2009-08-23
  • 打赏
  • 举报
回复

看不懂的不是好程序,俺是懒,没那么做。
jp0077777 2009-08-23
  • 打赏
  • 举报
回复
猴子的程序也不错 但jhone99的就很NB了 一看就是专业的
jp0077777 2009-08-23
  • 打赏
  • 举报
回复
猴子的程序我也编得出来 可能时间花得多些 但jhone99的程序我是编不出来的了
看得我呆了半天 我想是不是我干脆给钱他让他帮我编程得了
chinaboyzyq 2009-08-23
  • 打赏
  • 举报
回复
我不是VB老师,不知道我的程序你看明白了没有.
jp0077777 2009-08-23
  • 打赏
  • 举报
回复
jhone99是不是教VB的老师啊 程序搞得很透啊 我看半天没看明白 工资多少一个月啊 有点水平
chinaboyzyq 2009-08-23
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim s As String
Dim ss() As String
Dim s1(9) As String
Dim ss1() As String

s1(0) = "1 2 3 4 5"
s1(1) = "2 3 4 5 6"
s1(2) = "3 4 5 6 7"
s1(3) = "4 5 6 7 8"
s1(4) = "5 6 7 8 9"
s1(5) = "6 7 8 9 10"
s1(6) = "7 8 9 10 11"
s1(7) = "8 9 10 11 12"
s1(8) = "9 10 11 12 13"
s1(9) = "10 11 12 13 14"
s = "3 8 8 4 10 7"
ss = Split(s)
For i = 0 To UBound(ss)
For j = i + 1 To UBound(ss) - 1
If ss(i) = ss(j) Then
ss(j) = "t"
End If
Next
Next

For i = 0 To UBound(s1)
ss1 = Split(s1(i))
For j = 0 To UBound(ss)
For k = 0 To UBound(ss1)
If ss(j) = ss1(k) Then
n = n + 1
End If
Next
Next
If n = 4 Then
Debug.Print s1(i); "不同数:";
For m = 0 To UBound(ss1)
If InStr(s, ss1(m)) = 0 Then Debug.Print ss1(m)
Next
End If
n = 0
Next
End Sub
vbman2003 2009-08-23
  • 打赏
  • 举报
回复

private function NumComp(pStr as string,bol() as boolean) as string
dim a
dim b() as boolean
dim n as long,i as long

b=bol
a=split(pStr,chr(32))
for i=0 to ubound(a)
if b(a(i))then
n=n+1
b=false
a(i)=""
end if
next
if n=4 then
NumComp=trim(join(a))
else
NumComp=0
end if
end function

Dim s as string
dim s1(9) string
dim arr
dim ii as string
dim bolIs(36) as boolean '

s1(0) = "1 2 3 4 5"
s1(1) = "2 3 4 5 6"
s1(2) = "3 4 5 6 7"
s1(3) = "4 5 6 7 8"
s1(4) = "5 6 7 8 9"
s1(5) = "6 7 8 9 10"
s1(6) = "7 8 9 10 11"
s1(7) = "8 9 10 11 12"
s1(8) = "9 10 11 12 13"
s1(9) = "10 11 12 13 14"

s = "3 8 9 4 10 7"

arr=split(s,chr(32))
for ii=0 to ubound(arr)
bolIs(arr(ii))=true
next

for ii=0 to 9
msgbox NumComp(s1(ii),bolIs)
next
jp0077777 2009-08-23
  • 打赏
  • 举报
回复
jhone99 改后的程序没看太明白 但问题解决了 果然NB 猴子加油了
jhone99 2009-08-23
  • 打赏
  • 举报
回复
dim i as integer
dim j as integer
dim a(9) as string
dim b as string
dim intCount as integer

a(0)="1 2 3 4 5"
a(1)="2 3 4 5 6"
a(2)="3 4 5 6 7"
a(3)="4 5 6 7 8"
a(4)="5 6 7 8 9"
a(5)="6 7 8 9 10"
a(6)="7 8 9 10 11"
a(7)="8 9 10 11 12"
a(8)="9 10 11 12 13"
a(9)="10 11 12 13 14"

b="3 8 9 4 10 7"


for i=0 to 9
intCount=0

for j=0 to ubound(split(a(i)," "))
if instr(b,split(a(i)," ")(j))>0 then
intcount=intcount+1
else
strtemp=split(a(i)," ")(j)
end if
next j

if intcount=4 then
msgbox a(i) & ", " & strtemp
end if
next i
jp0077777 2009-08-23
  • 打赏
  • 举报
回复
没人性啊 你们花10分钟 我可能要花3个小时 那不是浪费社会资源
你们可能没有明白我的意思 我是说你们擅长编程 而我擅长其它 你们用你们擅长的10分钟去做你们的事 我用我擅长的10分钟去做我的事 而不是用我不擅长的3小时去做你们擅长的10分钟的事 不然就是浪费资源啊
jp0077777 2009-08-23
  • 打赏
  • 举报
回复
当把字符串改成"3 8 8 4 10 7"后 你们的程序就不对了 因为 8 重复了 能改一下吗?
加载更多回复(17)

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧