|
这个代码就你局的例子都没有问题
' v. t. y# _2 ~保存为.vbs文件
; x( D8 @# {$ {: \
2 _: g5 A7 A8 o' n; VFunction scanfnum(hfile)
, a K( `& J8 } '从文件中读取一个数字
2 o1 [& a! G5 H5 d, Y0 G" [* s Dim s, n, n_end- D4 ^) ^: k$ a+ \6 ~) R' j8 c3 n" w
n = ""- o* u0 f0 F1 h+ ?$ O( r' d: o C! X
s = ""
# U1 W% e7 T6 g/ g n_end = False
1 O+ y2 |0 Z, b& w d_end = False4 d' x+ n7 p& ?! Y0 v7 ?
Do While Not (hfile.AtEndOfStream)
. k; y1 U% g9 L0 P s = hfile.read(1) o+ u1 F3 H" r$ N; V
Do While (s < "0" Or s > "9") And (Not (hfile.AtEndOfStream))
8 J1 i6 P" s& R d* r/ ` If n_end = True Then
4 \- ^5 v# c0 {) L& d+ Z d_end = True; e1 h6 V i& A, {3 Y9 O( |) A6 e
Exit Do
* ~7 ^1 ^8 }' N6 I7 ~5 m End If
" g0 ?; P# f) }5 `7 q s = hfile.read(1)
2 h, H" t/ n+ J9 k2 x) T, } Loop' q. t; ?; e, W4 L
If d_end Then Exit Do3 W8 y7 i+ t2 L3 L6 I
If s >= "0" And s <= "9" Then
; R3 U \0 P: t, i n = n + s
3 v2 t- x6 G5 S7 ]& J n_end = True, G0 J4 i% C+ l4 y
End If, W5 `$ h% ^# E
Loop. C: l8 e4 `! h# F' A
If n = "" Then scanfnum = "error" Else scanfnum = CInt(n); j1 u, D/ y$ k) e' G4 x4 i- t
0 T7 l3 d4 `1 l3 R9 O5 G3 w" j7 aEnd Function
, ^ r U5 I% d7 L
4 C$ ? F" {, e) G8 w) {1 lFunction truenum(n1, n2, vx)
! [! f1 @/ R- U. b* i; ~ ' ↑存储分数的书组
9 t; s3 u) S: U% c '默认n1>n2
) p# K8 a( `+ j3 X3 z Dim p, v1, v2
5 L: |/ h/ b8 `, Y v1 = n1+ d8 j0 u+ a! k
v2 = n2! f6 r' d V: i B1 N- _
'下面把v1拆份成为每个数字尽可能大,数量尽可能少的组合8 _- x! }+ f% M7 W- N5 n* C
p = 100# g* J- n: e5 y
Do While v1 > 1 And p > 12 I ?+ l6 p9 m4 s. W# J9 p
If v1 Mod vx(p) = 0 Then5 v8 l' F- f! j6 ^: n, N
v1 = v1 \ vx(p)& j! @" C5 f% |) Z2 `8 _
vx(p) = 0 '用过的数字设置0
. K3 e; j9 _5 s End If9 o+ u; L: p; P! n2 f; b# f& O
p = p - 1
+ ]; R W# S3 u1 s1 p1 X Loop
' R/ d) _" |& L( z3 V( |9 N '下面一样尝试把v2拆份成为每个数字尽可能大,数量尽可能少的组合,但是v1用过的数字不能再用/ ]) k3 N. S7 F( y- Q
p = 100
3 J) K0 H, p2 `% x- W Do While v2 > 1 And p > 1
" _0 O% F% Y& o/ y If vx(p) <> 0 Then
1 Q5 C! V/ ?+ j/ {0 N l8 l If v2 Mod vx(p) = 0 Then
, n* h& [1 Z$ ~# Q4 V2 Z+ J; q0 K: F v2 = v2 \ vx(p)
! \' k R6 [5 _( i9 A vx(p) = 0, [2 R0 U- F% H& G! ] j
End If: ~# {0 n9 C" r; ]0 } T9 v, z
End If6 y4 K4 ~% d% R! a$ D0 y- U
p = p - 15 H; }; A( e- Q! M. M
Loop) s( L2 W6 }/ C m
'查分成功则结果v2应该是1,如果v1占用了v2必需的数字就会拆份失败
/ b$ ]% r7 @4 s2 Y; I If v2 = 1 Then truenum = 0 Else truenum = 1
2 o8 @4 n1 m: g. @0 xEnd Function
9 ^6 o) a5 H) V$ ^
! _4 U; g. I) H5 b4 @Function startprc(n, inpath, outpath)
% b- ~$ V! |5 Q0 P7 j ' ↑气球个数1 G% |2 k k! D2 w
' ↑输入文件路径
A% }" A- Q4 x6 X1 f" \ ' ↑输出文件路径
0 s( S6 w O' H$ A5 b+ I Dim nx(2), singlenum(), cnt
/ |4 m2 h" a* T3 ]/ g( S! e" Q3 Q ReDim singlenum(n)( f& F i7 k, f. L' \( {
3 @: W+ b) @& }
Set fso = CreateObject("scripting.filesystemobject")
. h; y) a @: C# p If fso.fileexists(inpath) Then+ ~9 e& D8 n$ Y% R' o; i. P: B* ~
Set inf = fso.opentextfile(inpath)
6 H6 e/ ]1 c8 O# Y: s( l# | Set outf = fso.createtextfile(outpath, 1)
, g5 w# H$ ~0 A8 y3 U0 _ Do While Not (inf.AtEndOfStream)* {( [, [3 s/ {$ d# [) z1 a% q
For i = 1 To n
) J2 R2 {$ T% c* o singlenum(i) = i '写入每个球的分数
/ X# C1 {5 q6 b( I Next* C2 M v6 j7 w7 G' J# J5 ^
'读取两个数5 q% m: E7 w* q3 L8 d* {& u
nx(0) = scanfnum(inf)
5 s# \% k% B, W- l- g3 e9 ?/ ] nx(1) = scanfnum(inf)
) P5 i5 Y9 g X- e/ r# |: V) E If nx(0) <> "error" And nx(1) <> "error" Then, G+ `' l6 P& T/ W8 O7 u
If nx(0) < nx(1) Then Q; S7 U! x9 I5 Q9 S" p
'大叔在前
! M" m A( n" r# j1 m+ Z nx(2) = nx(0)
; E+ l! x* s5 P* M7 { nx(0) = nx(1)
( \+ `* K, A6 i" E" y: x. q$ Z9 t+ H nx(1) = nx(2)& ^: x+ m0 `/ H/ E5 [6 I
End If/ t# Y2 z) L% O' G _ q
opp = truenum(nx(0), nx(1), singlenum)
( Y& n0 x" h) W: n9 O# W0 i outf.writeline nx(opp)$ Q. ]$ y" S% k9 O. t& z
End If) {. J( F% E, }8 Q
Loop
# o3 @2 b6 X; Y/ J7 u' ^5 D: y6 O End If
$ q1 ]3 R: P. hEnd Function+ g! v# b! u z) }9 v$ m7 X
'***************************
3 }' j: B( P+ [4 c'这里开始运行
2 B8 }" D1 l z. m+ t0 u9 b# T'***************************5 F' t6 K3 {/ b; L5 Y! I
startprc 100, "input.txt", "output.txt"
. H, t3 |) Q& h. n9 I
' ^9 E# @: V2 t4 {, D6 V6 _! }: J3 D6 w R5 T
1 H' \) p2 j, H' s$ v+ X; J* n5 I! }1 _7 a8 f: l
[此贴子已经被作者于2004-5-10 14:36:26编辑过] 2 j2 a) d( i3 [" N
|
|