主题:任给出4个非零的不同数字,求出由这4个数字组成的所有的4位数。
我是QB爱好者
[专家分:250] 发布于 2009-09-19 14:31:00
例如:
输入:3,2,1,6
输出:1236 1263 1326 1362 1623 1632
2136 2163 2316 2361 2613 2631
3126 3162 3216 3261 3612 3621
6123 6132 6213 6231 6312 6321
程序要求:从键盘输入4个不同的非0数字,列出由这4个数字组成的所有4位数,每行输出6个。
回复列表 (共6个回复)
沙发
神鬼天王 [专家分:30] 发布于 2009-09-19 14:49:00
排列
板凳
我是QB爱好者 [专家分:250] 发布于 2009-09-19 15:10:00
怎么做呢?
3 楼
phile [专家分:2310] 发布于 2009-09-21 18:53:00
CLS
INPUT N
DIM A(N)
S=1
FOR I=1 TO N
INPUT A(I)
S=S*I
NEXT I
FOR I=1 TO S
FOR J=1 TO N
PRINT USING"#";A(I);
NEXT J
PRINT " ";
K=N
DO WHILE A(K)<=A(K-1)
K=K-1
LOOP
J=N
DO WHILE A(J)<=A(K-1)
J=J-1
LOOP
SWAP A(J),A(K-1)
FOR H=K TO N-1
FOR P=H+1 TO N
IF A(H)>A(P) THEN SWAP A(H),A(P)
NEXT P,H
NEXT I
组合
CLS
INPUT N,M
DIM A(M),B(M)
FOR I=1 TO M
INPUT A(I)
B(I)=I
NEXT I
DO WHILE B(0)=0
FOR J=1 TO M
PRINT USING"#";A(B(J));
NEXT J
PRINT " ";
K=M
DO WHILE B(K)=N-M+K
K=K-1
LOOP
B(K)=B(K)+1
FOR J=K+1 TO M
B(J)=B(J-1)+1
NEXT J
LOOP
END
4 楼
593170024 [专家分:500] 发布于 2009-11-12 12:33:00
我用goto优化了
可以不用goto
CLS
INPUT n
DIM b(n)
FOR i = 1 TO n
INPUT b(i)
NEXT i
i = 1
t# = TIMER
DIM a(n)
10
IF i = 0 THEN GOTO 20
a(i) = a(i) + 1
IF a(i) > n THEN
a(i) = 0
i = i - 1
GOTO 10
ELSE
FOR j = 1 TO i - 1
IF a(i) = a(j) THEN GOTO 10
NEXT j
IF i = n THEN
s = s + 1
FOR k = 1 TO n
PRINT USING "#"; b(a(k));
NEXT k
PRINT
GOTO 10
ELSE
i = i + 1
GOTO 10
END IF
END IF
20
PRINT s, TIMER - t#
END
5 楼
zihao1231 [专家分:150] 发布于 2009-12-01 20:41:00
方法一:
直接输出
CLS
INPUT A,B,C,D
IF A>B THEN SWAP A,B
IF A>C THEN SWAP A,C
IF A>D THRN SWAP A,D
IF B>C THEN SWAP B,C
IF B>D THEN SWAP B,D
IF C>D THEN SWAP C,D
PRINT USING"#";A;B;C;D
PRINT USING"#";A;B;D;C
PRINT USING"#";A;C;B;D
PRINT USING"#";A;C;D;B
PRINT USING"#";A;D;B;C
PRINT USING"#";A;D;C;B
PRINT USING"#";B;A;C;D
PRINT USING"#";B;A;D;C
PRINT USING"#";B;C;A;D
PRINT USING"#";B;C;D;A
PRINT USING"#";B;D;A;C
PRINT USING"#";B;D;C;A
PRINT USING"#";C;A;B;D
PRINT USING"#";C;A;D;B
PRINT USING"#";C;B;A;D
PRINT USING"#";C;B;D;A
PRINT USING"#";C;D;A;B
PRINT USING"#";C;D;B;A
PRINT USING"#";D;A;B;C
PRINT USING"#";D;A;C;B
PRINT USING"#";D;B;A;C
PRINT USING"#";D;B;C;A
PRINT USING"#";D;C;A;B
PRINT USING"#";D;C;B;A
END
虽然要打很多字,但是速度很快。
方法二:
四重 for 循环
CLS
INPUT A(1),A(2),A(3),A(4)
FOR I = 1 TO 4
FOR J = 1 TO 4
IF I<>J THEN
FOR K = 1 TO 4
IF I<>K AND J<>K THEN
FOR L = 1 TO 4
IF I<>L AND J<>L AND K<>L THEN PRINT USING"#";A(I);A(J);A(K);A(L)
NEXT L
END IF
NEXT K
END IF
NEXT J,I
END
方法三:
对付n个数时用这种方法。
CLS
INPUT N}在这里可以省略,因为N确定=4
DIM A(N),B(N)
FOR I = 1 TO N
INPUT B(I)
NEXT I
I=1
DO WHILE I>0
A(I)=A(I)+1
IF A(I)>N THEN
A(I)=0
I=I-1
ELSE
F=-1
FOR J = 1 TO I-1
IF A(I)=A(J) THEN F=0:EXIT FOR
NEXT J
IF F=-1 THEN
IF I=N THEN GOSUB 10 ELSE I=I+1
END IF
END IF
LOOP
END
10
FOR J = 1 TO I
PRINT USING"#";B(A(J));
NEXT J
PRINT
RETURN
6 楼
斤斤计较 [专家分:0] 发布于 2010-01-31 13:27:00
哈哈!稀客
我来回复