主题:[讨论]请教高手几题:
嘟嘟小熊
[专家分:0] 发布于 2009-05-21 17:20:00
1.整数拆段
【问题描述】
将一个位数为L(4≤L≤10)的自然数N拆成4段,使各段对应的数的乘积最小。你能编一个程序实现吗?
【输 入】:键盘输入一个自然数N。
【输 出】:输出到屏幕 。一个整数,最小乘积。
【样 例】:
输入 输出
321427 2268 注:3*2*14*27=2268
2 选夏令营旗手
【问题描述】
一年一度的江苏省“信息与未来”小学生夏令营活动又开始了。与每年一样,组织者又设计安排了许多有趣的活动,其中第一项依然是挑选本次夏令营的旗手!由于这是一个非常具有荣誉感的角色,所以报名参加夏令营旗手角逐的营员仍然非常多,营委会于是规定:
将N个人排成一排,编号1~N。从第1人开始进行1~M正向报数,报到M的人出列,再从下一个人开始继续1到M报数、出列。(注意:按某个方向报数报到尾部时,再反方向继续报数)。如此进行下去,直到剩下一人为止,这个人就是本次夏令营的旗手。
小明非常渇望能成为旗手,你能编一个程序帮助他实现愿望吗?如果可以的话,你的程序应输出小明的编号。
【输 入】:键盘输入二个整数N,M (2≤N,M≤300,N≥ M ),用一个逗号分隔。
【输 出】:输出到屏幕 。一个整数,表示小明在队列中的编号。
【样 例1】:
输入 输出
9,3 8 注:出列顺序为:3、6、9、5、1、7、2、4
【样 例2】:
输入 输出
8,3 8 注:出列顺序为:3、6、7、2、5、1、4
3.任何一个自然数的立方都可以写成一串连续奇数之和,输入N,求N^3 是哪些奇数之和?
【样 例1】:
输入 输出
5 21,23,25,27,29
谢谢了
回复列表 (共3个回复)
沙发
gewenbo [专家分:450] 发布于 2009-08-03 16:29:00
1.
DIM Num AS DOUBLE
INPUT "N = ", Num
Num = INT(Num)
n$ = LTRIM$(RTRIM$(STR$(Num)))
l = LEN(n$)
DIM a(l * l)
FOR i = 1 TO l - 3
FOR j = 1 TO l - 2
FOR k = 1 TO l - 1
a$ = MID$(n$, 1, i): b$ = MID$(n$, i + 1, j): c$ = MID$(n$, i + j + 1, k)
IF l - i - j - k > 0 THEN
d$ = RIGHT$(n$, l - j - k - 1)
s = VAL(a$) * VAL(b$) * VAL(c$) * VAL(d$)
t = t + 1
PRINT t, "Num = "; a$, b$, c$, d$: a(t) = s
END IF
NEXT k, j, i
FOR i = 1 TO t - 1
FOR j = i + 1 TO t
IF a(i) > a(j) THEN SWAP a(i), a(j)
NEXT j, i
PRINT "Min = "; LTRIM$(RTRIM$(STR$(a(1))))
END
2.
INPUT "N = ", n, m
DIM a(n + 1) AS INTEGER
i = 0: f = 1
DO
IF f = 1 THEN
u = i + 1: flag = 0
DO WHILE u <= n AND a(u) = -1
u = u + 1
LOOP
IF u > n THEN f = -1
ELSE
u = i - 1: flag = 0
DO WHILE u >= 1 AND a(u) = -1
u = u - 1
LOOP
IF u < 1 THEN f = 1
END IF
i = i + f
IF a(i) <> -1 THEN j = j + 1
IF j = m THEN
j = 0: t = t + 1
a(i) = -1
IF t = n - 1 THEN EXIT DO
END IF
IF i = n THEN f = -1
IF i = 1 THEN f = 1
LOOP
FOR i = 1 TO n
IF a(i) = 0 THEN PRINT "Win = "; LTRIM$(RTRIM$(STR$(i))): END
NEXT i
END
3.
DIM s AS DOUBLE
INPUT "N = ", n
DIM a(n ^ 3): n = INT(n): s = n ^ 3
DO
js = js + 1: i = 0
FOR i = 1 TO total: a(i) = 0: NEXT
t = js - 1: i = 0: total = 0
IF t MOD 2 = 0 THEN t = t + 1
DO
t = t + 2: i = i + t: total = total + 1
a(total) = t
LOOP UNTIL i >= s
IF i = s THEN
FOR i = 1 TO total
PRINT a(i),
NEXT i: PRINT : END
END IF
LOOP
板凳
梦幻小樱 [专家分:140] 发布于 2009-08-04 13:03:00
1.
DIM Num AS DOUBLE
INPUT "N = ", Num
Num = INT(Num)
n$ = LTRIM$(RTRIM$(STR$(Num)))
l = LEN(n$)
DIM a(l * l)
FOR i = 1 TO l - 3
FOR j = 1 TO l - 2
FOR k = 1 TO l - 1
a$ = MID$(n$, 1, i): b$ = MID$(n$, i + 1, j): c$ = MID$(n$, i + j + 1, k)
IF l - i - j - k > 0 THEN
d$ = RIGHT$(n$, l - j - k - 1)
s = VAL(a$) * VAL(b$) * VAL(c$) * VAL(d$)
t = t + 1
PRINT t, "Num = "; a$, b$, c$, d$: a(t) = s
END IF
NEXT k, j, i
FOR i = 1 TO t - 1
FOR j = i + 1 TO t
IF a(i) > a(j) THEN SWAP a(i), a(j)
NEXT j, i
PRINT "Min = "; LTRIM$(RTRIM$(STR$(a(1))))
END
3 楼
phile [专家分:2310] 发布于 2009-08-06 18:15:00
这是整数拆段
CLS
DIM A(200), B(200), C(200)
DIM H1 AS DOUBLE, H2 AS DOUBLE
INPUT S$
L = LEN(S$)
MI$ = S$
FOR I = 1 TO L - 3
FOR J = I + 1 TO L - 2
FOR K = J + 1 TO L - 1
A1$ = MID$(S$, 1, I)
A2$ = MID$(S$, I + 1, J - I)
A3$ = MID$(S$, J + 1, K - J)
A4$ = MID$(S$, K + 1)
S1 = VAL(A1$): S2 = VAL(A2$): S3 = VAL(A3$): S4 = VAL(A4$)
H1 = S1 * S2
H2 = S4 * S3
H = S1 * S2 * S3 * S4: H11$ = LTRIM$(STR$(H1)): H22$ = LTRIM$(STR$(H2))
IF H <> 0 OR H = 0 AND (A1$ = "0" OR A3$ = "0" OR A2$ = "0" OR A4$ = "0") THEN
FOR Q = 1 TO LEN(H11$)
A(Q) = VAL(MID$(H11$, LEN(H11$) + 1 - Q, 1))
NEXT Q
FOR Q = 1 TO LEN(H22$)
B(Q) = VAL(MID$(H22$, LEN(H22$) + 1 - Q, 1))
NEXT Q
FOR PP = 1 TO LEN(H11$)
FOR QQ = 1 TO LEN(H22$)
XX = A(PP) * B(QQ)
WW = PP + QQ - 1
C(WW) = C(WW) + XX MOD 10
C(WW + 1) = C(WW + 1) + C(WW) \ 10 + XX \ 10
C(WW) = C(WW) MOD 10
NEXT QQ, PP
P = 200
DO WHILE C(P) = 0 AND P > 1
P = P - 1
LOOP
R$ = ""
FOR Q = P TO 1 STEP -1
R$ = R$ + LTRIM$(STR$(C(Q)))
C(Q) = 0
NEXT Q
IF LEN(MI$) > LEN(R$) OR LEN(MI$) = LEN(R$) AND MI$ > R$ THEN MI$ = R$
END IF
NEXT K, J, I
PRINT MI$
2.选夏令营旗手
CLS
INPUT N, M
DIM A(-1 TO 100)
T = 1
W = N
F = 1
DO
L = L + 1
P = P + F
IF P = W THEN F = -1
IF P = T THEN F = 1
IF A(P) = 0 THEN
S = S + 1
IF S = M THEN S = 0: A(P) = 1: H = H + 1
END IF
WHILE A(W) = 1
W = W - 1
WEND
WHILE A(T) = 1
T = T + 1
WEND
LOOP UNTIL H = N - 1
FOR I = 1 TO N
IF A(I) = 0 THEN PRINT I: END
NEXT I
严禁抄袭!!!
(保证无误)
我来回复