主题:找字符【50】
QUICKBASIC
[专家分:50] 发布于 2009-09-05 19:55:00
找字符
有一天,小明编了一个程序,能很快找出一个字符串中出现次数最多的字符,你能做到吗?
输入:一个字符串(长度不超过50)
输出:按字符在字符串中出现的先后顺序,显示出现次数最多的字符
样例
输入:ACABBCCDDB
输出:CB
回复列表 (共1个回复)
沙发
gewenbo [专家分:450] 发布于 2009-09-06 11:22:00
INPUT "N$ = ", n$
DIM a(256)
FOR i = 1 TO LEN(n$)
t$ = MID$(n$, i, 1): a(ASC(t$)) = a(ASC(t$)) + 1
NEXT i
FOR i = 1 TO 256: IF a(i) > max THEN max = a(i)
NEXT
FOR i = 1 TO 256: IF max = a(i) THEN ts$ = ts$ + CHR$(i)
NEXT
DIM b(LEN(ts$)) AS STRING
DIM f(LEN(ts$))
FOR i = 1 TO LEN(ts$): b(i) = MID$(ts$, i, 1): NEXT
FOR i = 1 TO LEN(n$)
FOR j = 1 TO LEN(ts$)
IF MID$(n$, i, 1) = b(j) AND f(j) = 0 THEN PRINT b(j); : f(j) = 1
NEXT j, i: PRINT
END
我来回复