|
这个代码就你局的例子都没有问题& S' F! W. x$ E' R
保存为.vbs文件% O% i* `! ?) h, ~" t& f
# o. F0 W3 U! e4 K3 b# c3 p7 KFunction scanfnum(hfile)
2 ~% h; x. ]1 t1 b '从文件中读取一个数字
+ v& T" G5 I; N k Dim s, n, n_end9 b3 K7 A: { |* l" [0 k$ E. `( h
n = ""8 o/ H+ u6 l; V+ L, E
s = ""2 m: x' B% \* m ~' W% X% t I
n_end = False
* F& ^- p- Q7 W. D d_end = False' w" ?- F6 |% |
Do While Not (hfile.AtEndOfStream)
9 K8 U+ v2 H9 O2 _1 {) y6 ?, S( d s = hfile.read(1)
5 \0 f6 R% I: |' b& s4 g2 P* s Do While (s < "0" Or s > "9") And (Not (hfile.AtEndOfStream)), B* Y6 Y/ F9 c! s' }
If n_end = True Then" I0 Q; R# V9 }& }' m0 G3 r
d_end = True h' r* ?- U! c. [
Exit Do
/ f3 a G9 q/ }2 ]" R4 |. D End If6 k* J0 z& J H2 ?
s = hfile.read(1)
, P- U2 h& Z% E1 G+ o Loop
5 g. O# |* {$ |$ a! ~: m6 } If d_end Then Exit Do
& ?1 d1 F5 c+ {$ M If s >= "0" And s <= "9" Then
" D3 q# X1 H- V+ _2 k) u n = n + s8 ]" x( e: m+ J1 G
n_end = True
4 h$ E" @$ x7 k% L End If" d# k# V; }5 P$ W; ?) Y7 k3 ^" A# \
Loop% U' R- h) ~8 D4 e5 Q1 v
If n = "" Then scanfnum = "error" Else scanfnum = CInt(n). Y$ ~/ s; U1 y( }" [
* m+ Q' O' p+ o
End Function
- W, M3 G+ A/ H/ | K8 G( ^2 u# v: a% T e O( p& A
Function truenum(n1, n2, vx); a9 B' Y; P# Z% k0 }( W2 t
' ↑存储分数的书组
% E7 L* R/ A/ [" o '默认n1>n2
# v ~/ M g( d Dim p, v1, v24 \! H* G8 J3 n% C
v1 = n12 l& ?. |$ {* X: b- Q9 S
v2 = n2. q* O a4 _# C; C7 K- j
'下面把v1拆份成为每个数字尽可能大,数量尽可能少的组合5 Q5 K8 ]- u3 u6 z: L0 U* _' `1 h* P
p = 100) r' Y! d7 {- { |( V$ _
Do While v1 > 1 And p > 1
' o; z3 [! y0 _ o9 A; C If v1 Mod vx(p) = 0 Then8 \3 ]9 W: \% T6 K* r& G" P% I( i
v1 = v1 \ vx(p); B2 e# L6 R$ U6 X6 O! r) g R
vx(p) = 0 '用过的数字设置0* I+ o: Y% ~% n7 D( L
End If" g5 q- U4 _" p$ R& O1 o, ]1 ^7 b
p = p - 1+ n7 k0 c9 G" K! x9 d( X
Loop1 D1 q5 k4 |7 {( Y4 i8 Y& L
'下面一样尝试把v2拆份成为每个数字尽可能大,数量尽可能少的组合,但是v1用过的数字不能再用7 ~, U5 a4 f2 ]" k. f/ [
p = 1003 a2 l% w/ p/ m3 w
Do While v2 > 1 And p > 1
% ^3 s' x. I- |) f0 N9 w! I$ \7 c If vx(p) <> 0 Then: F! F0 i& C2 B3 t( G
If v2 Mod vx(p) = 0 Then# u. P: I; z' h# c% |
v2 = v2 \ vx(p)
7 ^! z( v4 i' E$ | vx(p) = 0
$ u5 v# l1 E2 {- ? End If
" ^; i9 L) k; _$ p) t, K/ b; B End If
9 m3 X7 D* ^) k. H p = p - 1
+ b# ~# L% h, e& T- q Loop
0 u- Z) d/ S- W2 X+ S+ h0 H; z! ? '查分成功则结果v2应该是1,如果v1占用了v2必需的数字就会拆份失败6 z* q# m% j- A6 s
If v2 = 1 Then truenum = 0 Else truenum = 1- d& h! i) [$ V/ n2 Z$ `9 h1 G
End Function z2 j3 W- @$ u7 ^' s- ]
% M8 M1 `% R) l
Function startprc(n, inpath, outpath)
$ }, S. r1 I7 Q6 X& ^* ^, t: ? ' ↑气球个数
* m4 }6 `- X5 H2 w9 g ' ↑输入文件路径# t( W# ~' V N8 x, l
' ↑输出文件路径
! ?4 z7 z7 N3 Q Dim nx(2), singlenum(), cnt8 a! D) [* Y$ U! a
ReDim singlenum(n)+ x. V2 z Z! z- c9 u
- Z$ |0 d1 J$ c* }
Set fso = CreateObject("scripting.filesystemobject")
. m3 [7 J9 m- P- O If fso.fileexists(inpath) Then3 f5 y8 }6 m2 u: H3 M8 p, b
Set inf = fso.opentextfile(inpath)
7 }: j" p7 s. k2 S+ i8 z# U) D0 r Set outf = fso.createtextfile(outpath, 1)
: N7 R" G$ h# ~" _' Q' k2 x Do While Not (inf.AtEndOfStream)' F- T3 ]; [/ W6 D n
For i = 1 To n; U: [7 w$ R/ K, K
singlenum(i) = i '写入每个球的分数0 B' G% t0 }/ v3 w D* N" h" x
Next
) i. \& h: Y- }) y1 d9 k '读取两个数9 |$ ]/ n) v, i5 [& ]
nx(0) = scanfnum(inf) T1 X. B1 @7 g+ @4 W( B4 t# g S
nx(1) = scanfnum(inf)
, ^% } ? Q7 p6 y0 V5 p If nx(0) <> "error" And nx(1) <> "error" Then
4 W2 P7 J. y3 I" J If nx(0) < nx(1) Then7 u+ g& i8 D% q4 y0 L. g8 N
'大叔在前
2 L- Q, f! H: x1 Q2 F0 c nx(2) = nx(0)
) V2 a% U' _/ V$ R6 J3 ]% S- l( u1 { nx(0) = nx(1)
) F" ?1 y/ k; j9 {& s% n" k* o, L nx(1) = nx(2)
& c7 {2 f1 L- ]+ ]+ b End If
) J) E! _; K" B i V opp = truenum(nx(0), nx(1), singlenum); f p, D: y6 d
outf.writeline nx(opp)1 @: Y) i3 N u& m( S( d" ?
End If! i: N4 O5 s& m$ {
Loop- z6 i% V1 K0 E# o& r
End If4 p3 H, i0 u# X! l+ Y' S% a" d
End Function
- L- A, u3 {7 ^! h# m! t'*************************** S2 G4 p' r0 [, d4 b
'这里开始运行9 T) G! A9 l" z
'***************************
- d- B9 E N5 ~: ustartprc 100, "input.txt", "output.txt"; ~8 o+ l- n& X. d5 D# Z; Y
& k: g4 d4 x$ V+ T
* X" a+ k& s8 a! u3 p4 l
; Y: n: K# a( n; y3 t4 p9 }% |7 S! {+ b* C+ Z! x, k
[此贴子已经被作者于2004-5-10 14:36:26编辑过] ) w( [* j$ L% r/ b
|
|