|
这个代码就你局的例子都没有问题& H' M% d1 @* e" a
保存为.vbs文件
% P' C5 p" X" [' B6 m, p" D! G0 K: i7 O% y) R0 C! s
Function scanfnum(hfile)" U; q$ W) L. J, P% d7 D# }
'从文件中读取一个数字7 L/ h; E( H0 c; S
Dim s, n, n_end
- \% j% s5 D% K0 i n = "": P0 r! j! H& L( n$ Y6 p
s = ""
% I, T0 M2 V) m/ M9 K n_end = False2 E" X; L8 x/ w6 b
d_end = False
3 i9 u5 }$ U/ F9 c. k Do While Not (hfile.AtEndOfStream) K( a- J' c; b/ I+ {0 { b- A
s = hfile.read(1) s+ W, U% M6 C$ `, X
Do While (s < "0" Or s > "9") And (Not (hfile.AtEndOfStream))
% H( N {& U6 N V/ h$ Z9 @ If n_end = True Then
& Q4 o0 m% C' D/ ~ d_end = True g( ]2 M5 ^! _8 {5 p) L
Exit Do, d7 c7 p; P6 |
End If
$ a' @* x# ?% p* X; [ s = hfile.read(1)( x6 o% |" D5 ^- n# H( F
Loop
% U6 c3 ^. `6 P( @3 B If d_end Then Exit Do) b1 Y+ O% D, a) V
If s >= "0" And s <= "9" Then; K" H1 m2 \" A1 @6 ^* x1 t
n = n + s% T2 f3 E% E1 R2 v0 |6 ?! f, h6 x
n_end = True% v; U+ g- [) I- N M- M8 `* v
End If7 L# d! l4 _/ E* C& ]
Loop
! X/ ~) b0 {; f* K8 P0 g8 c If n = "" Then scanfnum = "error" Else scanfnum = CInt(n)& \0 c$ ~2 k2 L( d" Q# J$ O6 D
% n5 ], f* L$ U1 t5 g& ]8 B# f7 Q, w# EEnd Function5 W6 K% [* N8 Y8 L
( _7 q7 B; V6 I5 A+ e- o$ l
Function truenum(n1, n2, vx)) [+ E1 Q! S- M2 a
' ↑存储分数的书组
/ |- f" q& A& X6 ]4 d) I4 k" g '默认n1>n2
* G3 H8 {5 U5 u9 n5 z Dim p, v1, v2
& m8 K0 V5 [7 a8 Z9 m v1 = n17 a! [* F0 a4 U3 t+ Q# Q! y; a3 W
v2 = n2
- J( h ^! J7 R* _ '下面把v1拆份成为每个数字尽可能大,数量尽可能少的组合
6 f* I* u1 R* Y p = 1003 A8 z9 [, v8 u
Do While v1 > 1 And p > 1
6 {; O7 U1 F; W- G If v1 Mod vx(p) = 0 Then
( D( K2 z# }$ n7 ]; _9 J! |" p9 Z v1 = v1 \ vx(p)) |/ a: d; r8 O) O, }+ a
vx(p) = 0 '用过的数字设置0
. Y) l6 o( V+ m' [/ w End If' I1 W) w$ I- I% X7 n
p = p - 1
# |5 l9 d. _8 L; D4 t) W! ~2 N Loop
/ U! o* t& u, j5 T! Q '下面一样尝试把v2拆份成为每个数字尽可能大,数量尽可能少的组合,但是v1用过的数字不能再用
$ }3 a5 z8 q* B4 s# y: q! J9 [ p = 100
7 t8 h4 W# X* y7 g7 l Do While v2 > 1 And p > 1
$ K! j5 m# c1 S! z9 h If vx(p) <> 0 Then
2 V. F+ W3 H( C. Q# A2 G If v2 Mod vx(p) = 0 Then& X4 h* N4 N6 \! Q
v2 = v2 \ vx(p)
: e. _: S% @5 D9 t6 L vx(p) = 0
* W" I- J! k+ ^) x End If
( S) Z: \, ~( b End If
" A- g. B4 j# P4 H- [ r p = p - 1
4 p3 y6 n1 m- g7 |4 @' B0 a+ f Loop8 O8 F! _: M& m; N8 }& g$ o
'查分成功则结果v2应该是1,如果v1占用了v2必需的数字就会拆份失败
( ?" v$ ]. B6 x9 @ If v2 = 1 Then truenum = 0 Else truenum = 1
' o9 z; |" U3 L+ [" Y* h+ zEnd Function
9 ]3 @. ^$ ]9 [% O [! |0 S8 x; g2 ?
) n$ F* \3 S7 VFunction startprc(n, inpath, outpath)5 _! [; h! V- `7 l5 ]" U
' ↑气球个数
% ~- Y# t2 r+ q, d( z& W ' ↑输入文件路径
! ^6 x- F/ M+ C+ ` o ' ↑输出文件路径
6 A* z: N: _% D L' l; x4 j2 O. L. F Dim nx(2), singlenum(), cnt' A( q$ i/ i. ^ r" i7 N
ReDim singlenum(n)
; j' \$ ?* z# t) U0 E/ q8 w 5 z I' F. o; `9 a7 v6 h5 N
Set fso = CreateObject("scripting.filesystemobject")% V7 X% e' m, Z: K: H. L
If fso.fileexists(inpath) Then
( w( g' R1 s% J# t Set inf = fso.opentextfile(inpath)
|/ i, l+ ]2 [' e/ x1 N" {8 t Set outf = fso.createtextfile(outpath, 1)! K. S5 ?- r6 j) [, M* U6 H
Do While Not (inf.AtEndOfStream)
* ^: y4 ?% o3 G& f3 a For i = 1 To n0 r- b5 ?# N5 D6 a& K5 x
singlenum(i) = i '写入每个球的分数
: k3 i+ a: {. d) L, d, \ Z Next
) X% T. j8 S w2 q+ V0 V' i '读取两个数$ ?4 t. E0 @: p1 W
nx(0) = scanfnum(inf)
( Q2 l# G9 K2 Q5 U+ o( D, Y8 o nx(1) = scanfnum(inf)1 [% y% {9 w( [- M" k- f
If nx(0) <> "error" And nx(1) <> "error" Then
& y, q1 S# v) o4 i If nx(0) < nx(1) Then! F8 C& B4 u2 U) v
'大叔在前
; ~4 q7 o4 n$ Z nx(2) = nx(0)* X% n1 q( @6 K
nx(0) = nx(1)( E8 ?/ {/ S5 \* [) V1 t
nx(1) = nx(2)# Q8 D/ P$ U" S" ~6 N* G( D9 m
End If/ a2 ^, t5 }" x: a# a; a0 u
opp = truenum(nx(0), nx(1), singlenum)
# m6 K, U. @* r) R/ g7 h6 l outf.writeline nx(opp)7 O( Q7 D2 Q( V, W0 I
End If/ p1 w& e& ^- W1 Z q
Loop+ F* P! f8 J& I& M" J& _& @; K7 F
End If
& z* [. L3 E; JEnd Function
" p5 K" d! r/ N, H- t* R5 i'***************************
2 c$ B$ c, e! T& Z. s2 j1 h'这里开始运行
- I! C. R2 c' n6 F; J, k'***************************
7 K% K' c6 Z+ R# {startprc 100, "input.txt", "output.txt"
& o) b) u; n- k' Q3 f. c& L" ?, P) t
' d- h6 ?4 Z! _7 j" G1 E, X
5 [3 c1 I, [9 f9 ~" b
x1 P. e8 ^ g& k1 n& J6 I [此贴子已经被作者于2004-5-10 14:36:26编辑过]
, H4 a6 r" R! B. Q |
|