|
这个代码就你局的例子都没有问题# d# t8 f& o$ L7 t+ T
保存为.vbs文件
7 h$ g+ P6 P, D! N6 @& C) ]- p) _, H& ]3 \/ Q
Function scanfnum(hfile)5 g3 U9 O5 ^3 \9 |3 `; G
'从文件中读取一个数字: v% n: d! Q4 k; P* g* N3 Z
Dim s, n, n_end
. H! `1 ^* A5 Y8 p5 \0 Q n = ""
$ t" Q! v" Y6 {% }$ T1 l+ p2 f s = "": s# U1 }8 ~, \% V3 {2 I `
n_end = False+ a) \2 Y" ^ {* X
d_end = False
4 f4 q* b8 L# z9 C: J% k) m$ J Do While Not (hfile.AtEndOfStream)" `- `$ s2 B8 v) I! L
s = hfile.read(1)
0 T& }( g3 ]( ]- m Do While (s < "0" Or s > "9") And (Not (hfile.AtEndOfStream))! ]- m2 H4 I7 Z# j
If n_end = True Then
1 ]0 X, U% V$ X. d d_end = True
x" [2 ~7 Y! u Exit Do- y4 k) I4 ^0 Q& V- Y6 F4 U
End If
1 @) s. k0 E6 E5 E9 B5 s s = hfile.read(1)) q! H( q! [% U: ]+ z9 _
Loop
/ `: y7 F, ^1 L' ^9 p2 w* {) ?; W If d_end Then Exit Do! C; b% R4 B0 e" t: P! k- G' }
If s >= "0" And s <= "9" Then
; l1 S' x: J: U+ I/ l2 r n = n + s
7 z% T$ f$ q9 U n_end = True& G) k; W+ Z: R2 l* F! S( m5 r+ X' W
End If. T" f9 r5 X `" {! h7 q
Loop
7 M' c- z9 c. C: Z/ p If n = "" Then scanfnum = "error" Else scanfnum = CInt(n)' l- A1 r# J& Y' \1 n
0 ^7 r5 q, r) y, L- e4 \0 g9 t
End Function
# [4 p0 k, U! n/ t) d% a& @8 T) C G2 ~4 Q! h0 D' X" B
Function truenum(n1, n2, vx)
5 M: U2 {5 _, ?; `9 `8 O% @3 h ' ↑存储分数的书组; t4 F+ U* X$ Q# [5 {) }2 M
'默认n1>n2
; Y0 a3 I/ Q t* p! ` Dim p, v1, v2
" e8 x/ X4 e0 }' U1 v. B) `! Q _ v1 = n1 M: J# W0 v) `) s8 O5 _
v2 = n2
- _: N; o ~$ D. Y6 n '下面把v1拆份成为每个数字尽可能大,数量尽可能少的组合6 {8 g9 W7 B! ?- K- G( F
p = 100
% ^: v3 e- K- ?: n Do While v1 > 1 And p > 12 ?5 A$ b& T$ ?& L! x `
If v1 Mod vx(p) = 0 Then1 H0 {* u6 x+ r5 s+ F
v1 = v1 \ vx(p)4 [) M9 X' L5 @* G1 A" s
vx(p) = 0 '用过的数字设置0
8 r l7 P. u' ~ End If
# B! o9 l: Q5 v: r1 I1 W p = p - 1 g# T$ M2 ~/ e$ y& s3 Q( \4 F
Loop
* d! P, e9 k; [! G$ T* h '下面一样尝试把v2拆份成为每个数字尽可能大,数量尽可能少的组合,但是v1用过的数字不能再用
' Q! |5 O/ |" D4 E- I p = 100
) s( `2 j+ Y' d: }' }5 ?* x Do While v2 > 1 And p > 1+ M$ [ U6 T( |" K: ^! k, j
If vx(p) <> 0 Then
) t. j) ]) D- q$ c# q* m If v2 Mod vx(p) = 0 Then
5 B( N# s* _* y, u' E' ^ B v2 = v2 \ vx(p)
# t3 U+ t% D4 p# m* q7 E- D$ @6 f vx(p) = 0. A }2 v/ i, L; C! p4 D) n
End If
- o' v" A) M4 f' A( l: J8 f" ~- ^ End If
- e- Y+ x* F6 g' J6 K/ T, l2 p# L p = p - 1/ E: V. v1 S% u7 e4 u% P5 h. _
Loop) M# e X& \2 C1 ^0 W% V
'查分成功则结果v2应该是1,如果v1占用了v2必需的数字就会拆份失败
/ h' O! Y8 I' g: o" e5 D! c If v2 = 1 Then truenum = 0 Else truenum = 1
6 o2 L) z6 s- z8 G) U. \) aEnd Function
: [" T$ _/ F, o q! C% ?" z
( U f) K1 M) I. {% xFunction startprc(n, inpath, outpath)# h h! l j& i, C
' ↑气球个数
9 u! ^" J7 G: w# k4 ~( A" { ' ↑输入文件路径
% B3 T J9 T/ h& j- z ' ↑输出文件路径
) _7 |6 D0 D6 B0 { Dim nx(2), singlenum(), cnt1 o- a: N' F8 J! S/ E
ReDim singlenum(n)
8 }% o+ m, M3 J1 x0 a ! r) }/ \9 w9 O/ I' ~3 Y, `
Set fso = CreateObject("scripting.filesystemobject")
' R+ p8 [. U+ t } If fso.fileexists(inpath) Then
. L0 B, m m- n, K) X" i* E7 U0 p Set inf = fso.opentextfile(inpath)
8 j4 A* G+ @2 z Set outf = fso.createtextfile(outpath, 1)
1 j" H# ?# u6 Q% R4 n6 ` Do While Not (inf.AtEndOfStream)
" \& z9 m) e9 @& y For i = 1 To n8 O! t1 M: e0 }+ a: \! x& a1 v
singlenum(i) = i '写入每个球的分数
2 h7 u9 u9 F Q& A ~ m Next
$ c$ N. Q. s3 ]/ [$ b9 w '读取两个数1 \ j3 [% S' r2 i9 W+ r
nx(0) = scanfnum(inf)
$ ]" y. n' k- F- p nx(1) = scanfnum(inf). b8 F) g0 E& D+ B a( ]
If nx(0) <> "error" And nx(1) <> "error" Then) j- {3 [' V7 [
If nx(0) < nx(1) Then
. t+ h b& q: Y '大叔在前
4 h! d. s: m( y nx(2) = nx(0)
3 O; a7 d: v1 V/ O, f nx(0) = nx(1)
- x* {# H- k6 Z3 u& _0 j$ R! |& r# d0 q nx(1) = nx(2)
! X F$ G7 a5 Y% m" Z0 L; { End If1 o2 d* l1 E, q+ B, }3 O; Z
opp = truenum(nx(0), nx(1), singlenum)
/ ?7 L( I3 a! K3 s2 b# \& j, @ outf.writeline nx(opp)5 ?1 T! U' a& L: X
End If! n0 _# m. i, Y5 s! Q; q* z
Loop) A& P$ n8 d! ]6 I+ j: O4 j) \
End If/ V" w6 j: Q7 X' o& K
End Function! Q1 S: ~) R$ ]1 A
'***************************+ H7 c% t* M- _$ \+ B
'这里开始运行- S9 ]9 Y: g* U: N T5 c8 ~' B
'***************************
* ~' \& i$ {9 [7 n) Qstartprc 100, "input.txt", "output.txt"
/ v( d4 S2 E5 w8 O+ h: h- p% G9 k) q" p' ^4 }+ E, e: ]
/ U4 z9 V# J6 P/ S8 j3 b
& a) w: _# w" t5 z2 P5 Z4 C
" _3 m' A9 d: k; @ [此贴子已经被作者于2004-5-10 14:36:26编辑过]
. E6 Q) N2 g" k9 u8 \- A |
|