|
这个代码就你局的例子都没有问题
; @* @" E4 Z6 H2 x保存为.vbs文件+ ` u- C* _& v
: I3 l0 `$ o. u8 G' c& f2 W
Function scanfnum(hfile)
$ h6 I6 r' `& @ '从文件中读取一个数字" j( x7 X5 j$ p
Dim s, n, n_end
: E% F% m7 o5 U: j! R: I, R n = "": u; {" ~& U+ o9 F) K5 J
s = "" M: q" O- H# }% h
n_end = False" W" H- w$ N, g+ c: B( [
d_end = False' `/ i- c2 ^. X5 v
Do While Not (hfile.AtEndOfStream). O7 S0 A. T4 `" F6 j
s = hfile.read(1)
1 x# z2 Q* k# M+ e Do While (s < "0" Or s > "9") And (Not (hfile.AtEndOfStream))* _, d. I+ Q6 e' v" y
If n_end = True Then' D+ z) L* @. Y8 Q% ]+ c6 |. N( W
d_end = True+ J) [2 Y3 L6 E* K
Exit Do
4 \( _, ~$ ^3 `0 L5 Q End If
[& b8 L6 x8 E! n e" [ s = hfile.read(1)# m s" H i& B
Loop
1 O, S( E6 _6 r" J$ Z8 B! C* |5 u" W( i If d_end Then Exit Do
1 B+ m) D" S$ y If s >= "0" And s <= "9" Then
2 o7 q$ n9 E" t n = n + s0 y6 `) X/ l. b/ v
n_end = True
' F, \! ?$ ^" ^ End If
3 Z7 Y! A- j; k. ?9 w2 S Loop& n9 r8 G/ I9 y4 i8 q
If n = "" Then scanfnum = "error" Else scanfnum = CInt(n)1 s5 G8 `: k8 `
7 ~; l! v' V y5 e
End Function. a3 `: r8 ?% M
( C1 H* n5 U( Q: g" Y
Function truenum(n1, n2, vx)
9 H7 z. f9 I% K! |) ^- m ' ↑存储分数的书组
$ Q: y# T; t4 T( N '默认n1>n2
' ?# C1 C6 h* J; ?: p0 p& P0 v Dim p, v1, v2
5 ]3 j4 ^" ]6 R2 \3 t v1 = n1
f+ `( F# V: t- ^4 c8 F+ O v2 = n2! B% m8 Y! a& y7 U
'下面把v1拆份成为每个数字尽可能大,数量尽可能少的组合
' j0 S0 }4 K' V G- i* A, p* m p = 100
" p/ U% Z* K! N: X- F) ` ]0 z Do While v1 > 1 And p > 1
7 w7 A9 J9 { ~% P |( C- h7 d If v1 Mod vx(p) = 0 Then
+ p4 o1 n( }. r* l$ ~1 z v1 = v1 \ vx(p)) T9 |, |! q! a) O& c( s2 |3 u
vx(p) = 0 '用过的数字设置0
- r4 l0 `/ ?7 n End If- n7 e1 H0 D k* k" z7 A
p = p - 10 A. I. I5 O) X. k- C
Loop
) R) B/ q6 w& {/ _; q1 ~ '下面一样尝试把v2拆份成为每个数字尽可能大,数量尽可能少的组合,但是v1用过的数字不能再用& _; ^: Y8 |- `9 r
p = 100
, y4 T+ i7 i, Q2 a# ~0 C) ~3 V Do While v2 > 1 And p > 1! ]7 z( ?5 \5 _8 N! |: p# g
If vx(p) <> 0 Then
2 h7 Y- @2 T* L9 x If v2 Mod vx(p) = 0 Then" o( e% k! {: h7 B! ^5 u3 W, x- n
v2 = v2 \ vx(p)5 o, L w2 T# `' E: P& S: [
vx(p) = 0) `1 @2 }; L B& S8 a
End If
; S2 x+ i/ s5 y/ o3 Y8 W End If
, U4 Q: `6 D" l. m( w1 Q( K! R p = p - 1) }* {6 Y+ |& e" m$ a! X2 v
Loop' W, i! Q! `( h
'查分成功则结果v2应该是1,如果v1占用了v2必需的数字就会拆份失败
3 t/ S8 q7 d8 @5 G If v2 = 1 Then truenum = 0 Else truenum = 1
& H) i, @: t! n* YEnd Function
. m+ K4 _/ r8 F) \6 V' T
5 a ~5 p2 H4 W/ D; e* e6 g0 QFunction startprc(n, inpath, outpath)
! J" n" g3 u7 r2 c ' ↑气球个数
) f" P* i. L+ _* C M ' ↑输入文件路径# C- S. z& x4 _7 r( N
' ↑输出文件路径
& I. w1 S7 f+ c, C9 H' C8 N Dim nx(2), singlenum(), cnt4 Q! \4 {% Y2 v0 d2 L- d, e
ReDim singlenum(n)1 m3 h- [! D5 f W3 p
* Y* H' x/ D# P8 g9 @& W0 J Set fso = CreateObject("scripting.filesystemobject")
. ?( S& h1 B' @, a2 u If fso.fileexists(inpath) Then
- y }/ F: h$ {, n$ ` Set inf = fso.opentextfile(inpath)9 W8 t" {9 w9 d3 H1 B0 g
Set outf = fso.createtextfile(outpath, 1)
3 G6 ?! ^: o+ D6 w3 K0 h Do While Not (inf.AtEndOfStream)8 d5 i7 |4 i: a( t! w
For i = 1 To n
( m3 p6 H. B) g5 Q' R singlenum(i) = i '写入每个球的分数
: t k o" `5 ` F2 f% ^ Next$ ^8 P! E7 |& f
'读取两个数8 U6 {& W& R4 _4 L& X
nx(0) = scanfnum(inf)
' m1 a. c, L3 ]. t5 | nx(1) = scanfnum(inf)1 \$ {& V4 ?7 A0 O( H9 s7 Q
If nx(0) <> "error" And nx(1) <> "error" Then
" g* ?5 L1 j, J If nx(0) < nx(1) Then2 {( H& y, K: K1 V
'大叔在前
! W% T, ]) |1 u& D7 D nx(2) = nx(0)
) c c, e) Y: s& x/ \ nx(0) = nx(1)
" x" D6 e: Q9 O/ W9 M [ nx(1) = nx(2)1 y" q B: ]! }' v7 n9 {! |0 c8 W, U
End If
t/ `3 a0 H2 N& _2 \ opp = truenum(nx(0), nx(1), singlenum)2 l/ L- G3 r% \* X. \$ o
outf.writeline nx(opp)( k1 Q, m) i( ~0 ~& u/ U
End If( b. S& v4 d% |! p& `2 B
Loop; M4 ] k4 p& v
End If& m' a U% v' x0 P
End Function$ Q: n q; [. F, n* I# E e
'***************************7 T# M2 T7 F ^( X+ ?+ @( F
'这里开始运行
2 ^1 i! A( r1 r- ~ f'***************************
2 D! g9 |. |4 dstartprc 100, "input.txt", "output.txt"- B# g( ~6 u, r+ X( M/ n+ A) |
4 C; g) Y! t" m) {6 D5 L3 K; `( X3 m# ^5 f& f
+ f$ x! P; S+ l/ [, i; r
5 ~: G/ {4 v7 k [此贴子已经被作者于2004-5-10 14:36:26编辑过] + Z- L" J2 ?6 a; E7 o, H! Y
|
|