|
这个代码就你局的例子都没有问题% p& V$ ~: H, o9 v9 t! C% `
保存为.vbs文件
0 m, Z& Y7 k3 {
1 c7 l4 `) Q# X2 u$ Y- w) s+ DFunction scanfnum(hfile)6 I1 s+ e& K" P, \
'从文件中读取一个数字
N+ | C. r# |( Z6 S6 T Dim s, n, n_end! q0 o# g. r9 H( w) G) P
n = ""% }9 d+ d: G5 y1 l8 P
s = ""
# S, J5 ~" ~' I n_end = False9 `8 ?1 e2 W+ Z- P5 U. ]
d_end = False+ \8 s3 P8 p- |% }% z, h
Do While Not (hfile.AtEndOfStream)2 x9 e; O2 K6 @" n6 v& M6 y5 B* f) J
s = hfile.read(1). O7 S, B. T/ t, y7 z
Do While (s < "0" Or s > "9") And (Not (hfile.AtEndOfStream))" ^4 s2 K% {/ ], g1 \$ C
If n_end = True Then
! I& h. U- _ J d_end = True5 [2 b3 U1 J% G, g* g# d
Exit Do
) @2 y& W" t# V0 q8 |! U. @3 P+ H End If
* ]6 C% q( x$ i) R. P( R0 w s = hfile.read(1)8 g( W1 x, q7 M; Y( z
Loop
- O3 A; Z, L5 ] b6 k0 ?/ N/ I If d_end Then Exit Do. s1 m8 w% b9 {8 [' N8 n$ s
If s >= "0" And s <= "9" Then8 i# P& Q. ~0 [3 k0 h. J5 _
n = n + s
+ z4 y& E: ]! ?; g" c9 k8 ]) u' {& ] n_end = True
% c# j# i' w2 N8 w" ]' i% x- H- z/ m End If+ t( R8 U( K) J; u0 D& K
Loop
/ h# A z2 S8 U& B! C If n = "" Then scanfnum = "error" Else scanfnum = CInt(n)( L l F& u% ] G& e' X" h, P+ L
- C3 g/ q% ~ ?% |- ^9 P
End Function
' v0 p6 [% o- i' f5 A( L8 h, t7 ~
Function truenum(n1, n2, vx)
# P) a( h# t- e) `" x+ D3 @ ' ↑存储分数的书组0 o# w" Q2 w, s/ R6 X
'默认n1>n2( F7 H# G; X/ p9 `
Dim p, v1, v2: n) p8 Z; z6 O
v1 = n1: j# p" F5 K! B4 g
v2 = n2
" T0 d6 d' R! K2 Q3 H: Q+ s '下面把v1拆份成为每个数字尽可能大,数量尽可能少的组合+ i9 o/ t- ?7 v+ q
p = 1006 w5 x7 r+ L) w& w6 ]# ^7 d
Do While v1 > 1 And p > 1
$ H, {/ b' o$ }8 ~% y If v1 Mod vx(p) = 0 Then
* n v! F8 W$ \5 M! Q) {* ^ v1 = v1 \ vx(p)8 @6 N8 A+ b, W! |! @
vx(p) = 0 '用过的数字设置0 x8 E& z- H7 T' f. F# c5 N" f0 o) ?
End If% i8 T5 P: U" D8 x' I4 a% W2 [) Y: T
p = p - 1% l8 N2 p6 o& v; d$ l/ ~
Loop
. q' g+ k8 j8 O2 M8 D '下面一样尝试把v2拆份成为每个数字尽可能大,数量尽可能少的组合,但是v1用过的数字不能再用
7 X& l& J) p; ?4 e p = 100
3 d8 D# L2 ^ o, E2 R4 w* X9 O3 v Do While v2 > 1 And p > 1$ U. T: }% ~) ~% \' [
If vx(p) <> 0 Then
; r' U& G* ]5 c If v2 Mod vx(p) = 0 Then
5 T T9 I9 e( E+ O8 B- E4 q- T; d v2 = v2 \ vx(p)
1 L5 F1 Z _; j) l' ^ vx(p) = 0
7 s. |0 u, b5 n" R. E End If
b( n% A* ~4 [4 P3 C: p End If
" i/ q( u! l, m5 f p = p - 14 [: O, G& o7 m, y Y5 {5 _
Loop
' p: K8 G8 ?9 c- N9 r- q* q5 A, y; f '查分成功则结果v2应该是1,如果v1占用了v2必需的数字就会拆份失败$ \8 O" j, U$ t3 A5 N
If v2 = 1 Then truenum = 0 Else truenum = 1( a* Z% V9 G( E9 ?+ v
End Function
4 x7 J( [; @+ m' p1 a/ t# b3 F! x$ B8 X+ }4 p$ F5 n
Function startprc(n, inpath, outpath)$ Y% m' }, l0 z' b3 G+ O: U
' ↑气球个数
) u) w d) p2 i& }- R ' ↑输入文件路径
6 z! S' `: B8 E! u+ W @ ' ↑输出文件路径
! s0 F9 Y5 C* c/ j Dim nx(2), singlenum(), cnt, x0 z* e: \; l3 E( D0 ]
ReDim singlenum(n)% }0 @6 Q9 L% p7 v
- ]5 ~) ]% c* U& r; ^ j& N- z# E Set fso = CreateObject("scripting.filesystemobject")
6 L G- N# q, _, c0 X: w9 Q! U If fso.fileexists(inpath) Then2 i# G' d* a, X! |. a( B
Set inf = fso.opentextfile(inpath)) G' f% j+ _# ~. ]& _& }: l7 y) F
Set outf = fso.createtextfile(outpath, 1)
, x3 z! h8 ]; ^2 s Do While Not (inf.AtEndOfStream), j4 T: t. a7 D+ H; i! q2 }* S
For i = 1 To n
[6 d1 ^" X- K6 E singlenum(i) = i '写入每个球的分数
; @$ G& w5 X, B: X" i r( f2 Q Next5 w/ _! J# r! J9 v: m
'读取两个数: l3 o0 \4 e' c7 e1 Z5 j6 H' U
nx(0) = scanfnum(inf)6 w, F5 b8 S7 m$ w
nx(1) = scanfnum(inf)
5 z$ _0 k, l; v# Q& u+ r0 F If nx(0) <> "error" And nx(1) <> "error" Then
) H+ t; X& X6 U- j$ i If nx(0) < nx(1) Then
+ S; h, g2 y$ } '大叔在前# Q8 N( g- M+ X" A
nx(2) = nx(0)
' i: S' P! ~7 [9 S8 H X# l2 n nx(0) = nx(1)
) ~7 ]4 K: H8 M0 m9 A nx(1) = nx(2)2 S1 G+ @$ Q) e6 q6 v6 q" k) S" k
End If
2 N5 b. ~ f; o8 R* Q: B opp = truenum(nx(0), nx(1), singlenum)
; c9 T; g( v* T$ o: z: C2 w/ \ outf.writeline nx(opp)7 |0 ^" D' ^) n- k" @
End If. J2 N9 F6 @4 m- b
Loop
3 r( B# g5 F% X) S9 m) n: i End If
8 ]# i- i/ m% C5 g/ eEnd Function
4 a7 l3 ^4 S1 F9 q'**************************** m7 O7 L& F5 G9 N
'这里开始运行3 a) P; b2 p+ m! q0 A
'***************************
* f# O% ]0 q3 [2 dstartprc 100, "input.txt", "output.txt"/ p5 j3 k4 N! p( v& H
5 V/ [. Y# ^* f
* _5 F) ?0 I( Q8 V7 N2 ?, e/ Z
% m" C/ |# ?/ t; j* t8 W
; p/ N; u1 ^$ e: f) `0 N [此贴子已经被作者于2004-5-10 14:36:26编辑过]
7 d0 J& F7 S6 c0 I2 m |
|