|
这个代码就你局的例子都没有问题+ l6 Q, j) I M# h! b
保存为.vbs文件$ k4 I# \: x- r$ l6 F- ?. ^6 K6 {
& @: Q" I, G5 E9 R6 Q
Function scanfnum(hfile)& y9 V4 [+ v1 ?3 g( `+ M1 ?
'从文件中读取一个数字
8 O# m v5 Q, m Dim s, n, n_end
4 A5 x2 f+ ~6 @" J2 \5 h n = ""
, k6 \. L q- c6 V& o6 R$ M$ | s = ""5 Z& d, Z5 o# o3 f% p, ^! \$ G- O
n_end = False: E( e, M4 O$ z+ U0 O! V
d_end = False
" ?) A5 k3 H8 \" n+ D) J Do While Not (hfile.AtEndOfStream)
P$ F8 c: S0 G s = hfile.read(1)' G4 M! X8 }: H0 T# i" a
Do While (s < "0" Or s > "9") And (Not (hfile.AtEndOfStream))
) s* m" U# t9 R: e. y If n_end = True Then
7 |4 Z. o* q* U% e d_end = True" \" a! m; @5 a4 Q) i3 S& v
Exit Do
1 R- V- m7 J2 Q9 m8 z: L) F4 r& c5 a End If8 l0 }: Q* H0 t
s = hfile.read(1)
& ?2 R5 ]- p$ G Loop$ v4 S6 f* D1 s# i) ]' B
If d_end Then Exit Do* B+ e) s1 c5 b* _
If s >= "0" And s <= "9" Then
$ j" P9 \! m# d n = n + s. E' e( S( S+ {' I. X$ |
n_end = True- O+ N8 {% `3 g* P
End If
( \6 ]& F8 W: u2 J& N Loop
* N! V9 r% U+ L7 R# g2 X If n = "" Then scanfnum = "error" Else scanfnum = CInt(n), `) W8 ]9 s1 G& h" e' q
/ b; {9 M% G7 ^
End Function n7 u' y6 V$ H
) k) z( b9 C5 f/ ~. ?3 Q* Z9 zFunction truenum(n1, n2, vx)) B- K8 }* ?* }. ?, A: i
' ↑存储分数的书组
; ?) D$ R/ Q2 g. R4 y$ E! ] '默认n1>n2
: Y* W% g$ Z( J9 ~& N, E* y Dim p, v1, v2
6 \; Q4 u0 ~6 U6 a v1 k$ Q v1 = n1 R L$ O8 U9 A7 u& v! j [
v2 = n2% W' Y6 k! e, |3 R$ ?/ }: G1 V
'下面把v1拆份成为每个数字尽可能大,数量尽可能少的组合
) L# O5 n+ H5 N7 }! H p = 100
4 A5 b" |' N9 c* I Do While v1 > 1 And p > 17 |8 u" V' i m# e, t3 V% \
If v1 Mod vx(p) = 0 Then
6 `- L6 ]7 P9 \( d2 D6 m1 P v1 = v1 \ vx(p)% h" C) A4 }4 x& t; o* b
vx(p) = 0 '用过的数字设置0; t" f% c1 q( y' S) M8 q& D' L- Z
End If/ R2 v7 M/ ]! ]/ W# ?
p = p - 1* g0 Q4 I- j: M; s b+ `4 ^
Loop& N4 m0 S; s: ?2 e. m
'下面一样尝试把v2拆份成为每个数字尽可能大,数量尽可能少的组合,但是v1用过的数字不能再用
1 J" h+ @, u& K" P/ g p = 100
) h- W9 w" q2 z1 C8 l Do While v2 > 1 And p > 12 ]. U4 h; i: `- K* ]$ o( j" J
If vx(p) <> 0 Then
/ k$ x4 Q5 W1 a If v2 Mod vx(p) = 0 Then
1 G$ ~6 _, y4 t* Q e v2 = v2 \ vx(p)
) C0 @) H* [5 [4 f vx(p) = 0' l Z; y- d5 z# v
End If$ R5 J. A. Q2 [: \; }. t4 [
End If
: e1 @+ D* ^8 n) A p = p - 10 u* {. G1 k) L* c' }0 ~
Loop
# b9 B& g$ g9 J7 D O5 ^* C '查分成功则结果v2应该是1,如果v1占用了v2必需的数字就会拆份失败
3 L4 u) G% Q( H$ u& m9 R, x If v2 = 1 Then truenum = 0 Else truenum = 1
a, F' y/ K7 N" X0 YEnd Function
0 |! Z8 x+ Q/ ?; M8 q+ a" T
. f! y; o- g1 g: w7 I! m [# ], RFunction startprc(n, inpath, outpath)* J/ @ t, X' c3 ]7 z/ @
' ↑气球个数+ N6 c# X+ y$ p+ C7 X, {0 ^& q
' ↑输入文件路径
. z' Q0 P- G. E, ~ ' ↑输出文件路径 G4 ?+ d+ U0 A
Dim nx(2), singlenum(), cnt1 K/ b4 @: Z/ N* Y' n0 Y4 q
ReDim singlenum(n)
- |/ l6 k: I# I( o8 I
6 X# ?% i. A, f Set fso = CreateObject("scripting.filesystemobject")
' {4 A& ?* h) E# p7 |: r/ Y If fso.fileexists(inpath) Then N+ F* x3 j' D3 {) S& w, z
Set inf = fso.opentextfile(inpath)
# d. V5 ?: A7 y" ]1 A Set outf = fso.createtextfile(outpath, 1), d: q3 X5 \) e7 ^- H: Z
Do While Not (inf.AtEndOfStream), l8 H! C: _5 e
For i = 1 To n! c' f! z$ G- h M
singlenum(i) = i '写入每个球的分数7 k. m% G' @/ X! ]8 B) e8 P0 h
Next! E: y6 O! m- v7 |+ F5 @
'读取两个数
# s, f% ^3 K8 p. _0 U# [ nx(0) = scanfnum(inf)% E O- T9 n$ ]' T5 y- |* p1 d. X
nx(1) = scanfnum(inf)* ~5 Q" c# m7 \/ U
If nx(0) <> "error" And nx(1) <> "error" Then; {* `8 q" ~! O% `( B! k/ ?0 M+ Y
If nx(0) < nx(1) Then$ Q9 f4 e. q: j- ?
'大叔在前
' _! N( T) B4 y- s) M nx(2) = nx(0)
6 K8 z6 b( i* W9 \ Q& Z& ` nx(0) = nx(1)0 h) d) s3 G& [# y- ?. W) p: ^
nx(1) = nx(2)
0 @) K( k7 e; J" {% L( L0 P End If. z7 _) H8 j' g( Z$ S
opp = truenum(nx(0), nx(1), singlenum)- L, J: d1 }6 V* T9 {8 b9 p9 ?1 j
outf.writeline nx(opp)
, Q( H' Y& H0 D; | End If
* H+ \. c4 }6 o; [: ~; `1 z; V Loop
4 \% z7 [, }$ \# h; l3 P$ G8 n( U End If
) w0 |7 s1 |' Y3 ?End Function$ D3 p. w' M; s' }+ z
'***************************
# N, Q! Z( U6 K'这里开始运行2 Y+ e5 P' a1 J- g/ m0 b+ i) o
'***************************: `$ F t; @( p
startprc 100, "input.txt", "output.txt"$ S. y3 i) {- c* Z" P! t: P
& X+ v' m) @" l" ^2 Z, ^! V) K3 Y, J- q3 V+ U9 m
) ]9 p, V& k% W5 y- i
2 f; j) {4 G, Q' O5 x# E" g [此贴子已经被作者于2004-5-10 14:36:26编辑过] 1 C# O; k; A5 D" m' T6 C/ V7 T
|
|