|
这个代码就你局的例子都没有问题/ J( e. d/ S, @- D2 w
保存为.vbs文件
; u4 [/ ? J) h1 D* X; B& n" L/ m" \! ? B8 t
Function scanfnum(hfile)% n6 t& l* b, D, h
'从文件中读取一个数字
' D3 I6 k( k5 u5 g; z; h7 h* Z9 C1 P Dim s, n, n_end, D' m5 Z. E. o. [" o7 ]/ h/ h5 ~2 I
n = ""8 e# ~# l& ?, P' Q
s = ""
1 T1 N3 W" G; d- m n_end = False
) e; t3 V" q; ~7 _# s d_end = False
- A5 m% Z9 k2 ` H+ } Do While Not (hfile.AtEndOfStream)
; Z3 R- H8 `4 t, u9 l s = hfile.read(1)* B7 ^" x" G3 E" Z0 @( Z0 m8 W$ ]
Do While (s < "0" Or s > "9") And (Not (hfile.AtEndOfStream))
1 B- d8 X; c7 P( w0 P0 U# D' }/ _2 |$ [) h If n_end = True Then0 G& f& e/ q6 G' `! `; F
d_end = True2 a/ V2 w5 h+ m Q; E
Exit Do
* G0 ^- V. b/ Q End If
6 t9 j, O% L/ n; J+ l. B, T5 }" l; R s = hfile.read(1)
U$ E/ ?2 v$ u0 R$ C Loop
1 s6 m- ]7 z: U( i; g+ C* G8 F If d_end Then Exit Do9 p% p7 ^% U+ E- \- S/ Z
If s >= "0" And s <= "9" Then$ m. B1 Z7 U5 [ z
n = n + s
* a2 }( e- G# @) z" r Z7 f n_end = True, d* ?1 H. S1 T- ~
End If @$ l, B! K% k3 |2 p! S! ]" `
Loop0 {; T7 _6 W/ Z7 e+ T
If n = "" Then scanfnum = "error" Else scanfnum = CInt(n)
7 w( v5 @4 Y: z% [( B* P4 c. |
, e7 u" {% G% j* H3 F1 @End Function1 m/ A: i3 M u
m0 [& {8 h4 V$ ^3 |Function truenum(n1, n2, vx)
6 W* ]5 r5 O0 m& u {: p: {. B* y ' ↑存储分数的书组
3 s& {4 T3 C: G. ?' a, p '默认n1>n2( m& D* I% L! o' x; S3 {3 O
Dim p, v1, v21 D9 @8 V1 v6 Z! }0 p" \5 T& \
v1 = n1
1 O: V7 L1 a8 U" o- D c6 } v2 = n29 @2 [2 G5 U0 z* G
'下面把v1拆份成为每个数字尽可能大,数量尽可能少的组合; Y" x0 f' W4 J3 \& ? {6 E1 D5 \
p = 100# ?2 l! `$ y" z6 k, ?$ q( W* V
Do While v1 > 1 And p > 10 S: Y6 U! H' F/ L/ b( q0 C
If v1 Mod vx(p) = 0 Then; ~6 J7 F5 P# p6 _1 t/ \% ^2 k
v1 = v1 \ vx(p)
6 s A) }5 d4 ]0 R/ @5 q; s( ]/ M vx(p) = 0 '用过的数字设置00 [! L7 J& }, W, q5 ^
End If3 L l U( V& U' k) F
p = p - 1) g9 L/ H: |# |
Loop
( p) O7 r& q( M, m! {. e- Y- t& } '下面一样尝试把v2拆份成为每个数字尽可能大,数量尽可能少的组合,但是v1用过的数字不能再用
+ w" w5 [7 Q5 C" V7 ~; t p = 100- @3 \5 O7 n7 o" O# }6 D
Do While v2 > 1 And p > 19 U* {( [8 E9 ~$ l/ D$ \
If vx(p) <> 0 Then: p' u9 I; @ s
If v2 Mod vx(p) = 0 Then
1 L" N! v4 R ?/ n; D- n3 N8 { v2 = v2 \ vx(p)
1 r2 i1 E& w/ T vx(p) = 0
2 h- {/ h& p9 ^2 a0 u+ f* C End If
+ p/ A6 H0 c( m* x( n End If
# L6 [9 g4 U* I. N p = p - 1, C- O: y7 g! o8 {
Loop; \+ _6 G, D% h a& Q. w0 W6 i: `
'查分成功则结果v2应该是1,如果v1占用了v2必需的数字就会拆份失败
) C7 _7 u" }/ q* P9 K If v2 = 1 Then truenum = 0 Else truenum = 1
7 x/ X$ K1 ?$ Z$ b# eEnd Function7 K: \. t- s# W* k
* P1 g! i l X; ?% _! u4 m/ QFunction startprc(n, inpath, outpath)) F/ D# B! A: x) l @) G
' ↑气球个数& j" F6 [' g. z5 r. A$ h
' ↑输入文件路径0 ^* p2 ?& a( l! u5 I
' ↑输出文件路径
9 o2 d. M. q) b7 E: Z Dim nx(2), singlenum(), cnt7 V B3 ~: W% O0 d, D X% k
ReDim singlenum(n)
6 H9 r+ s7 }" w
) P/ L- h8 Y0 V7 c3 ?# i Set fso = CreateObject("scripting.filesystemobject")
7 Z& o$ p" k$ {. \ If fso.fileexists(inpath) Then
5 R1 D/ q: C3 ~' ~ Set inf = fso.opentextfile(inpath)/ l" i. \! V4 R5 R$ p" Z5 ?$ m
Set outf = fso.createtextfile(outpath, 1)
1 [& l* {6 v9 k8 p. H# E& \# e Do While Not (inf.AtEndOfStream)
7 u$ S; d: h/ O For i = 1 To n
e3 U, }+ o: S. i1 {) n* _ singlenum(i) = i '写入每个球的分数
' u, ^& [( g& i y# J* t; y Next
9 y! P" e9 {! K7 s+ v8 o" b- y3 h '读取两个数7 C& [7 P3 W6 c! B T" f# K. N
nx(0) = scanfnum(inf)
1 F0 h' H6 m$ g. ~- Y) f# \ nx(1) = scanfnum(inf)7 l2 W: z- o1 h2 \
If nx(0) <> "error" And nx(1) <> "error" Then
* L) J6 ^" n+ u8 a! y If nx(0) < nx(1) Then3 n- X8 c! o4 W8 A
'大叔在前
5 ~: G8 L% o: m9 K2 @# O) {. |/ L nx(2) = nx(0)
' Y b( s! N8 f; b: f( N) t nx(0) = nx(1)* z7 R0 D0 j) Q
nx(1) = nx(2)
7 j5 j# i+ z/ q0 ?+ i9 x End If
- G+ t" G# m. P9 @' Z opp = truenum(nx(0), nx(1), singlenum)
5 c& E9 U1 y$ l1 k; G8 R outf.writeline nx(opp)( k+ N) Y$ Y) @! ]( R1 L! ~
End If
0 ~% P- B' P; Y; j6 k Loop9 f: j. B( [6 C2 ?
End If" x+ E3 i, J0 h* Z2 ^- }% D
End Function% x" X9 ? K Z
'***************************
5 `- r7 z9 m9 {& R. i/ ~# N; I'这里开始运行
! m' ]$ T b! p5 O9 F# ]4 _'***************************; e {% q. R6 T' X( \
startprc 100, "input.txt", "output.txt". R. H" f" Q4 y+ [" d+ ?. _0 s
# Y0 o4 W2 v8 K2 K, {% V4 y, K, Z+ P' k0 F) d% W
, w; w" Y* x/ j. P- f0 F) |2 k$ x) J' b" Z
[此贴子已经被作者于2004-5-10 14:36:26编辑过]
0 [0 M. ^( q) b I/ M# u6 h |
|