|
这个代码就你局的例子都没有问题
7 X. ~3 {$ }- \" o保存为.vbs文件
* O7 ]! q, a: r2 p; o6 x5 i+ f* x. i3 ~+ w2 s
Function scanfnum(hfile)
$ U7 O5 \8 R) i# @9 H8 ?+ T '从文件中读取一个数字
V7 \4 u$ x5 J/ w Dim s, n, n_end
8 e/ s6 k% h. {2 B8 z* I- _' g n = ""
! D. n0 {3 ^" ?- W1 E s = ""
; k) C5 W' y( m8 R6 m6 [3 V. k* M9 p n_end = False
- E# V L- }6 ]. a# w d_end = False
: i3 ^9 G. Q* g3 a- ~0 j( h" x% O: s Do While Not (hfile.AtEndOfStream)
1 E* U! Y* @9 f6 y" @. k* U6 X s = hfile.read(1)
/ V, k3 o! X0 l" C+ Y Do While (s < "0" Or s > "9") And (Not (hfile.AtEndOfStream))
1 e9 m3 b! ?2 E: ~% ? P. v& \( Q If n_end = True Then
, U: R- X. E N+ B/ D! n d_end = True
% K" C( B0 U4 b: r/ k) D5 ~) j Exit Do. G( W, G! C' ]0 U& M) p7 m. o9 e
End If
2 x) a4 z" `4 n4 Q& b4 b s = hfile.read(1)6 M: j" s1 T5 Y4 k! }
Loop
0 Y4 E7 z3 N- K+ ~) ?+ o If d_end Then Exit Do
# D Y7 v% A# }! x$ ] If s >= "0" And s <= "9" Then+ q- t( d/ I$ m+ q1 c
n = n + s3 Z6 x1 v# E) u. ?: r- z, m! x
n_end = True
8 ~% L- _! ?4 w( y7 P" C End If# r* E, @7 A" U3 ~
Loop5 j7 P' o. t8 \& m# G
If n = "" Then scanfnum = "error" Else scanfnum = CInt(n)
: y. _6 [8 h& O, v' s
2 L; J6 @: g' REnd Function
* y! X W& A# A: |- D: C7 C1 |$ e! ~$ R% N
Function truenum(n1, n2, vx)5 {; R# M, P/ @2 ~0 l
' ↑存储分数的书组
7 C9 ?# o3 O, P. c5 D& e '默认n1>n2
+ ?' k& k4 i8 v' V3 R1 [0 x; n Dim p, v1, v2
) [6 p! U! I4 k: C v1 = n1# O) |! ?3 v' D
v2 = n2
! b1 f: a1 N; T# ~ '下面把v1拆份成为每个数字尽可能大,数量尽可能少的组合
# b- {4 s+ y: ~# ?* X9 i4 _, `1 i p = 100* G9 f8 N7 x! |8 E" ^% r
Do While v1 > 1 And p > 1! z6 a1 B+ i! r, w! Z5 i4 z) x
If v1 Mod vx(p) = 0 Then
* n% O8 E- B5 ~0 b v1 = v1 \ vx(p)" H. T1 \7 P5 D( R2 I8 r
vx(p) = 0 '用过的数字设置0* \5 P8 B; u4 N& C
End If
* A) Z$ s4 D8 g( e/ S p = p - 1
4 b# I* M/ b1 @4 a8 {9 o1 v Loop1 U% |- q4 `: H- u, D
'下面一样尝试把v2拆份成为每个数字尽可能大,数量尽可能少的组合,但是v1用过的数字不能再用
( y. D/ `. {- l. W2 c. Z- Q p = 1002 Q7 I" g5 G$ j/ ~2 b* G
Do While v2 > 1 And p > 1
9 X4 \" D. W* b+ G, C7 R If vx(p) <> 0 Then
0 S+ h9 e- s& [( g$ S If v2 Mod vx(p) = 0 Then
1 j9 `8 \* R4 l& p: U9 v8 Q v2 = v2 \ vx(p)9 C8 r! H& X! J+ T) K
vx(p) = 0' E# |3 M- c4 R
End If- b0 P) m/ H2 Q& ]7 x8 g
End If
# ~1 P5 s G9 C7 G3 d5 A p = p - 18 W- o; n: t% b# N1 T8 y
Loop- h) {$ t- r: X z8 ?
'查分成功则结果v2应该是1,如果v1占用了v2必需的数字就会拆份失败( H% C3 `( @" b1 V U* N
If v2 = 1 Then truenum = 0 Else truenum = 1. r; r* L7 u8 D" B. P b
End Function
" }1 h+ W) g# c+ H! J: C0 H
. e/ Q1 I( }# ?/ YFunction startprc(n, inpath, outpath)/ O( \0 V' V7 W# Q7 l, r/ }; d
' ↑气球个数+ n8 F8 i3 F% \7 Y$ w) x1 J6 e
' ↑输入文件路径
/ q* h( K7 e9 f; O0 b+ [4 k L* w ' ↑输出文件路径
; e# h: ?5 ^4 o+ I3 h Dim nx(2), singlenum(), cnt
7 r% P' \: T* U ReDim singlenum(n)# i4 j: J' H9 j1 M" D# g- a
/ I, x# f8 c' B0 n: @& v Set fso = CreateObject("scripting.filesystemobject")8 j, q3 A- I, n$ d0 j% c/ z
If fso.fileexists(inpath) Then
y% k* K p! z8 H* C Set inf = fso.opentextfile(inpath), M7 B* W- |* Q$ k+ K* H1 k3 ^
Set outf = fso.createtextfile(outpath, 1)- L/ H8 B" z, H9 ^& g3 S" a- h" U" T
Do While Not (inf.AtEndOfStream)" I- P: s; @; T) `' F* a
For i = 1 To n
( _7 s4 ?3 {$ a \7 ], L& e singlenum(i) = i '写入每个球的分数* c9 Q& Z9 ~! ~
Next
; ]4 Q& P+ G: |6 L- [ '读取两个数 T" C' U$ `0 w4 L
nx(0) = scanfnum(inf)8 B1 J1 X6 l' N) @+ E1 b u
nx(1) = scanfnum(inf)& M" B9 m; y p9 r9 ?; s
If nx(0) <> "error" And nx(1) <> "error" Then
# y# D5 A2 c1 ?4 j( F6 l If nx(0) < nx(1) Then
0 I( A% J3 G/ e, Y( \2 l2 a+ P '大叔在前
" Z( \5 u E* Y# c7 `1 k4 Z nx(2) = nx(0)
& k2 C6 k: ~, Z2 e2 [ ` nx(0) = nx(1)
1 L3 U5 S" G# H2 i% N nx(1) = nx(2)
' s X7 I( C* V) e1 p End If
; p$ D! @" C8 Z; Q! \ opp = truenum(nx(0), nx(1), singlenum)0 ^+ C2 j! V% V! s/ e! |6 H2 r' Y
outf.writeline nx(opp)
2 e# D! y1 M" I' K End If+ d3 a. l" w4 j' P! @$ n
Loop, x1 c9 T' j* a, b( e0 o/ C
End If
3 q e! z m; x5 Y( uEnd Function/ c: [: O6 r# N. {% @$ d* m0 H2 C
'***************************4 {$ B9 n* a) c4 n- o2 H
'这里开始运行
3 @$ X) ~; W7 M* Z9 D. Q'*************************** d% g, Z6 q2 F8 P2 Y6 H
startprc 100, "input.txt", "output.txt"
1 I/ `2 C. v: T J
/ z7 [6 ^- _7 p/ l9 [0 }
+ p# t" H! I% q2 c; _& k+ q$ c5 {! I) H) L8 B! ^6 W
8 \) R. q+ J/ ^7 C
[此贴子已经被作者于2004-5-10 14:36:26编辑过]
* c: R/ Y T6 L- f |
|