|
这个代码就你局的例子都没有问题
* Q8 k. z1 ^/ L' _9 ?+ ]8 C保存为.vbs文件/ Z3 W/ E. A* c$ s
6 Y$ I2 U; J0 e; ]# J- h. c3 ^Function scanfnum(hfile), W# g5 X1 Z, `% ^' ~2 ], J
'从文件中读取一个数字
4 U7 L( W: R) t& @ Dim s, n, n_end; G# b) h: }) C, x8 K* O A
n = ""
! n4 ^: K5 T7 u7 x1 P# \* \& k* F s = ""
# g9 \1 x3 q1 V8 E9 C) Z n_end = False( U; N1 f# w* Q; q9 o6 W0 X
d_end = False& m/ }" T9 ^3 \$ k& u
Do While Not (hfile.AtEndOfStream)/ M, O6 ]* k n: A
s = hfile.read(1). Y! R, t, j) R
Do While (s < "0" Or s > "9") And (Not (hfile.AtEndOfStream))8 Q( B2 D; `3 h7 Y. ~8 X5 T+ I
If n_end = True Then
) m) P. `" w/ c( `& G4 e4 N d_end = True
) s7 k9 d6 e4 l! S: _; {2 T Exit Do
! P) k" o. ]2 A7 s& n% a* T5 Q End If
" S4 M7 \5 z! [ I) L s = hfile.read(1)
2 _! c) a# j4 A, ]2 t- x Loop" F7 U1 P! Z/ t( q
If d_end Then Exit Do" `2 s2 e& C* n7 E9 [& e
If s >= "0" And s <= "9" Then- p3 p2 h: E* S0 d4 T' `- N
n = n + s
$ F8 ^- R, d+ |9 e2 }/ K( s( r n_end = True
6 n K$ S& {' F$ C% X+ n End If
% N) e: v" {2 V4 A/ y* k+ d Loop" @3 ^7 K# I' U+ P2 r! y
If n = "" Then scanfnum = "error" Else scanfnum = CInt(n)! r7 |* ^1 P' `/ s1 @8 @
( A- E! E9 r$ X2 j! ^, \End Function6 e, A2 x! e' ^6 ?
* c5 S" O4 K& B8 t4 s
Function truenum(n1, n2, vx)0 g9 Z4 m& P: b
' ↑存储分数的书组
1 [% J0 ?: @4 b8 s1 n. P' E8 U; U/ K '默认n1>n2
$ k. h) w4 |9 y& N Dim p, v1, v2; d6 o, Y3 `7 G9 h; }& i5 c
v1 = n1) a8 s$ k+ e& G+ ]
v2 = n2
H6 N8 D9 V4 C4 V- x- @. m7 M '下面把v1拆份成为每个数字尽可能大,数量尽可能少的组合. B' x) x! ^+ {1 h
p = 100
# }1 j) L8 @6 \ Do While v1 > 1 And p > 1. v8 D6 g# k+ H6 X1 S6 Z9 W4 I s- X
If v1 Mod vx(p) = 0 Then
. \) M8 c9 Z4 K! ?- C( J' T/ j; A v1 = v1 \ vx(p); `0 l, V1 S7 T
vx(p) = 0 '用过的数字设置0
; H8 {) g) g9 N; K End If9 K5 h9 X! W4 o, e
p = p - 1/ ]; T- J" r2 i, m
Loop
$ {4 S! A' ]2 q0 c7 l- a/ y. { '下面一样尝试把v2拆份成为每个数字尽可能大,数量尽可能少的组合,但是v1用过的数字不能再用 r2 r) _6 ^# F! C1 Z5 P
p = 1008 a. H" ?& \$ N
Do While v2 > 1 And p > 12 g7 y7 t* R3 T: h: j* n
If vx(p) <> 0 Then. u7 n @# U8 i& W
If v2 Mod vx(p) = 0 Then
( m4 |1 l4 }3 F [! m v2 = v2 \ vx(p)
$ \ G1 ^* K# e+ j vx(p) = 0
8 V. V, @) E: y End If
; `+ `+ Y! @/ \, c End If, m8 s$ }+ X7 j V5 g
p = p - 1
7 h; G. s* y! q: I0 t- H- I$ w Loop% ^# q+ }# i) G# B
'查分成功则结果v2应该是1,如果v1占用了v2必需的数字就会拆份失败
, z! }5 q5 ?0 `3 {( p& c9 x6 A If v2 = 1 Then truenum = 0 Else truenum = 1
! G2 p% ^' o: K( e% U7 HEnd Function
. H7 R2 [ b: }( S. _6 i
1 t- G: p% G1 U. k4 h8 MFunction startprc(n, inpath, outpath)
! C6 M& b5 ~" p; E ' ↑气球个数" _: L: {( `" n, {: X% F1 ]0 y
' ↑输入文件路径
" X i: S w' r6 ]' E& d ' ↑输出文件路径
$ N& A2 |/ t7 O& [ Dim nx(2), singlenum(), cnt
% S4 A2 f, d& t0 T0 j ReDim singlenum(n)6 @; V$ f6 ^7 W' X+ r
' R1 ], W: J& _2 P Set fso = CreateObject("scripting.filesystemobject"); X' n$ h+ R- z% C' j% j. l
If fso.fileexists(inpath) Then' @9 {0 l: P7 S& j. w' |
Set inf = fso.opentextfile(inpath)
- C& v- f9 O" P# m! X4 ~6 J Set outf = fso.createtextfile(outpath, 1)/ @' V; ?6 T9 c8 l
Do While Not (inf.AtEndOfStream)# B- r+ h5 _3 s& `
For i = 1 To n6 d8 A# e8 z& L
singlenum(i) = i '写入每个球的分数
/ o" b# \: |% f2 C Next
. n2 S% }8 g: Y' E) u' m3 V '读取两个数# p4 ]; h3 a) ~: Q0 v" G. G# Z
nx(0) = scanfnum(inf)8 ?: ~6 ]) d4 P0 x6 c4 O% @
nx(1) = scanfnum(inf)
5 o: W7 P3 Y D1 W5 ^ P If nx(0) <> "error" And nx(1) <> "error" Then6 P& I+ d: x. N7 W3 O+ R' a
If nx(0) < nx(1) Then
+ x/ l+ e5 l7 u. ]9 s' J '大叔在前" K2 M z) u; S$ p; N2 Y3 d# ?
nx(2) = nx(0)
( f( q0 z- v) d nx(0) = nx(1)+ ]/ T. f$ j' j% i
nx(1) = nx(2)
- z' M) L1 b: v8 I( C/ Q' a% g End If
, H% A! w( K% u5 N- j& P opp = truenum(nx(0), nx(1), singlenum)
0 Y# `7 n6 ] z( j p3 I outf.writeline nx(opp)
) q& B% f9 b' g7 a7 r End If
) l5 n5 @$ ^ r' I! ]2 r Loop
0 W; k4 P! e& O# K End If
! v) J/ r6 f: X+ o4 FEnd Function. m; X2 o; w* b: T- f* a
'***************************
* W7 Y8 I2 W5 o! r'这里开始运行
! G/ s# ~1 s* z3 D0 p5 u'***************************" q# U# ` n, s9 {# ]- x
startprc 100, "input.txt", "output.txt"
- _* u# q8 n3 s$ b2 ~7 H
# d" j( \1 x! ~2 M3 C
3 h# C2 A" b& [3 K; c" `6 h+ }& }
* u) o" C1 n! r, q7 G' K7 h3 i, S1 c! u! a
[此贴子已经被作者于2004-5-10 14:36:26编辑过]
$ p2 C, Y& O5 F* ^0 n7 ^ |
|