主题:大家快来帮帮我!
小精灵LS
[专家分:580] 发布于 2007-05-08 15:13:00
编程求正整数N和M之间的每数的质数因子。N,M由键盘输人,N>2、M>N。
(质数即除了1和它本身以外不能被其它整数整除的正整数)。输出形式如下:
如N=3,M=50
3=3
4=2*2
5=5
6=2*3
……
50=2*5*5
请各位高手多多指教
[em18][em14][em15][em6]
回复列表 (共11个回复)
沙发
Matodied [专家分:7560] 发布于 2007-05-08 20:17:00
DECLARE SUB fj (k!)
DECLARE FUNCTION isprime! (j!)
CLS
INPUT n, m
FOR i = n TO m
CALL fj((i))
NEXT i
END
SUB fj (k)
PRINT k; "=";
i = 2
DO
IF k MOD i = 0 THEN
k = k / i
IF k = 1 THEN PRINT i ELSE PRINT i; "*";
END IF
IF k = 1 THEN EXIT SUB
IF k MOD i <> 0 THEN
DO
i = i + 1
LOOP UNTIL isprime(i) = 1
END IF
LOOP
END SUB
FUNCTION isprime (j)
IF j = 1 THEN isprime = 0: EXIT FUNCTION
FOR i = 2 TO INT(SQR(j))
IF j MOD i = 0 THEN isprime = 0: EXIT FUNCTION
NEXT i
isprime = 1
END FUNCTION
板凳
强强 [专家分:4740] 发布于 2007-05-08 21:33:00
强,果然厉害!
3 楼
chyx [专家分:450] 发布于 2007-05-09 18:08:00
CLS
DIM A(10000)
2 INPUT m, N: IF m = 1 OR m = 0 THEN 2
FOR Q = m TO N: N = Q: PRINT #2, Q; "="; : S$ = "": S = 0
FOR I = 2 TO Q
FOR J = 2 TO SQR(I)
IF I MOD J = 0 THEN 1
NEXT J
S = S + 1: A(S) = I
1 NEXT I
DO
FOR I = 1 TO S
IF N MOD A(I) = 0 THEN N = N / A(I): S$ = S$ + STR$(A(I)) + " *": GOTO 10
NEXT I
10 LOOP UNTIL N = 1
PRINT LEFT$(S$, LEN(S$) - 1)
NEXT Q
快给我加分,你的同学
4 楼
Matodied [专家分:7560] 发布于 2007-05-09 18:51:00
也要给我加.
5 楼
senir092 [专家分:10] 发布于 2007-05-11 22:59:00
Matodied 在输入N和M的时候有问题
6 楼
Matodied [专家分:7560] 发布于 2007-05-12 14:00:00
有什么问题?除非是你自己输错了.
7 楼
moz [专家分:37620] 发布于 2007-06-01 22:55:00
给你的建议:
[quote]IF k MOD i = 0 THEN[/quote]
改成 do until k mod i
[quote] LOOP UNTIL isprime(i) = 1 [/quote]
改成 loop until i>=k
然后把那个检测质数的函数删掉,看一下结果有什么不同的.
8 楼
verysmartboy [专家分:1260] 发布于 2010-12-03 23:29:00
哥们,看看我的方法:
CLS
INPUT N
S=1
DO
S=S+1
IF N MOD S=0 THEN
N=N\S
S=S-1
PRINT S+1;"*";
END IF
LOOP UNTIL N/(S+1)=1
PRINT S+1
END
9 楼
幽灵密码 [专家分:3510] 发布于 2010-12-05 18:50:00
Cls
Input n, m
For k = n To m
Print k;"=";: x = k: a$ = ""
Do
For i = 2 To x
If x Mod i = 0 Then a$ = a$ + Str$(i) + " *": x = x / i: Exit For
Next i
Loop Until x = 1
Print Left$(a$, Len(a$) - 1)
Next k
End
10 楼
幽灵密码 [专家分:3510] 发布于 2010-12-05 19:12:00
啰嗦点:
1 Cls
Input n, m
If n > m Then Swap n, m
If n = 1 Then
Print "1 not a Prime number and 1 not a Composite number, too."
Print "Prees any key to start again"
p$ = Input$(1)
Goto 1
End If
For k = n To m
Print k;"=";: x = k: a$ = ""
Do
For i = 2 To x
If x Mod i = 0 Then a$ = a$ + Str$(i) + "*": x = x / i: Exit For
Next i
Loop Until x = 1
Print Left$(a$, Len(a$) - 1)
Next k
End
我来回复