|
这个代码就你局的例子都没有问题/ [: a' L; k6 o" I
保存为.vbs文件
# h/ b* ?3 h! i/ \" `' R& _- C; S" L" V5 U
Function scanfnum(hfile)
0 L! L( a+ ]" @1 Z" X* q '从文件中读取一个数字
3 ~1 _5 A. W2 u7 y# g$ w7 @ D Dim s, n, n_end: d/ ~+ u, L0 P, y @4 l
n = ""
: d/ j+ r/ Q6 T. a2 r+ g* t0 o s = ""
* Q7 V) D6 l8 S8 Z$ y; W n_end = False* y* ~8 ~ C2 ?+ v5 B& p
d_end = False
- @* ]! R9 m# G- J9 c5 n3 M Do While Not (hfile.AtEndOfStream)
" |, d$ b8 s4 \' U8 z* s6 { s = hfile.read(1)
. I& J9 E' C( `+ G& o, H7 o Do While (s < "0" Or s > "9") And (Not (hfile.AtEndOfStream))- P/ `2 Y, \$ d
If n_end = True Then
) S! G- e- `; `, i' V7 [! W2 a d_end = True+ l3 B- J3 F# n) h: a2 u
Exit Do
+ ~% W- g; O& H, ^' w: y End If6 Q% K- v" _# A$ v
s = hfile.read(1)
+ [. Y; g; r' N. ^" e. }, n8 L Loop
* Z! `# Y# K# d! E- h: h' {' j' c If d_end Then Exit Do
6 l2 Z! j, k* X4 @% u* ? If s >= "0" And s <= "9" Then
( M m( q9 i# V3 ?: r- Y/ N n = n + s
1 [1 Y+ ?$ c3 ]$ ? n_end = True) H( v2 E2 P5 l# ]: N$ i- m) X) j
End If
|5 @& U& ]& M( q4 v, X g' m Loop
6 n/ K/ {, O- I$ s- w. O If n = "" Then scanfnum = "error" Else scanfnum = CInt(n)( \1 u- l/ v9 N
. m. U6 B. b, M1 @& X4 R
End Function
y1 E" y- F/ z9 h1 j, W; s
& w$ W$ r' E3 T. ^Function truenum(n1, n2, vx)
& l9 Z; } o3 t2 K, j ' ↑存储分数的书组6 ]0 W1 A. ^5 z8 V+ V# Y* X0 v
'默认n1>n2- S; [5 }, k/ @& J8 G% O( Z
Dim p, v1, v2
8 I h( n A3 k# a0 V9 W$ ]( s v1 = n14 O; ~) e' e' D6 B7 O) j8 v
v2 = n2( W# j% ?* x0 L5 b
'下面把v1拆份成为每个数字尽可能大,数量尽可能少的组合* s- f3 N! V) i1 P; A+ l
p = 100
: n/ g* I( ]5 q! K# B6 O Do While v1 > 1 And p > 1 H5 z/ B& F2 U7 ?$ B1 ]( L0 i8 B0 P
If v1 Mod vx(p) = 0 Then
8 q; d! V5 c6 a* X v1 = v1 \ vx(p)
- X1 }, F! w* q* H vx(p) = 0 '用过的数字设置0
e n* {5 d. H6 w* \. d' Y: E8 | M End If
' s) g* \, O/ e5 a1 y p = p - 1
" @# d5 N$ b/ }4 u8 F' R Loop" U! d4 L8 o; A$ D! Y) z/ C
'下面一样尝试把v2拆份成为每个数字尽可能大,数量尽可能少的组合,但是v1用过的数字不能再用. y* }6 f: w: l% A8 Z- }/ R
p = 1000 l* c3 G/ ^$ M$ q
Do While v2 > 1 And p > 1' O! q5 d2 a0 e$ L# B& w
If vx(p) <> 0 Then
c8 S4 i' L$ T0 x If v2 Mod vx(p) = 0 Then
& U a; D, d- V2 \) w; z- u v2 = v2 \ vx(p)
- n7 B1 s9 G h vx(p) = 0
/ t1 O" o/ _# M% P E- n9 i1 F2 t End If1 y% u4 {* W* N C
End If. Q% {8 w8 d% V% F
p = p - 1, `) r1 s8 q9 B; o2 e
Loop
; Y4 W! i0 ]! x' u '查分成功则结果v2应该是1,如果v1占用了v2必需的数字就会拆份失败- z# o5 |' Y& d6 n7 k
If v2 = 1 Then truenum = 0 Else truenum = 1
: f' ^8 p8 m p7 x; ]End Function
/ ^- G5 C" j, A+ f
( C7 B8 U6 U4 i7 ZFunction startprc(n, inpath, outpath)# Q2 f8 S. Q7 L ^# ?; P
' ↑气球个数
: J( j( J2 W! C Z, u& \1 V ' ↑输入文件路径% S& p9 g1 R! E4 c
' ↑输出文件路径
4 R, Z2 A6 T7 B9 d: R Dim nx(2), singlenum(), cnt
# C' ]+ Z; x8 Z3 p9 o" Z% h% ] ReDim singlenum(n)3 S V7 G5 w8 K
# d" z0 ]) k; U) R9 K7 x Set fso = CreateObject("scripting.filesystemobject")
' ]8 D& ?) U3 _; ` If fso.fileexists(inpath) Then" z: N+ e8 s w1 t6 L" u4 B
Set inf = fso.opentextfile(inpath)7 \% V; m: S+ ]! ?
Set outf = fso.createtextfile(outpath, 1)" w+ [2 d7 R, e) C
Do While Not (inf.AtEndOfStream)- R4 ~8 J$ L% d- v4 H# n# r
For i = 1 To n& ]' t2 |8 {) Y+ M$ M' \: T
singlenum(i) = i '写入每个球的分数1 t, |. C2 a7 H9 F
Next2 r$ Z# S. J$ t5 [. }* H
'读取两个数: R' E; r9 G3 k4 i& `4 \
nx(0) = scanfnum(inf); V3 b m) Y0 Y( O- G1 X) {# l
nx(1) = scanfnum(inf) I6 e6 t6 i/ h s. O
If nx(0) <> "error" And nx(1) <> "error" Then. Y5 L" O4 F. g3 c! B. j: I6 w- W, q
If nx(0) < nx(1) Then7 v" o) g4 w, f8 n! ~2 k
'大叔在前. ^7 o/ x x! f4 h4 `
nx(2) = nx(0)
/ x8 g6 e, T4 p/ c# e# y nx(0) = nx(1)
/ l; |% Y; u! i' I' y4 o nx(1) = nx(2)
. Z. J n, {# ?3 C' e End If
' E2 {* d: u- t$ L- ~7 k opp = truenum(nx(0), nx(1), singlenum)
& b# N, {, @, a1 Q& e5 N6 r% P outf.writeline nx(opp)5 \5 }5 J: p: A4 m" v+ @) Q( e8 w
End If
: R: F- d6 K( ]% C/ S: \/ [: w Loop1 `! ?- }4 v# I4 k/ b
End If
: ^) x# q9 n$ I9 \" ZEnd Function( U" c" B4 w! K2 o6 [% O8 A! P
'***************************& H4 C( O* d- q. c8 Z; n1 {& F5 X
'这里开始运行
# c: P2 g. g2 _) b' e'***************************
) g, k! Q; I; r; N; L. A, ]startprc 100, "input.txt", "output.txt"
& [. p- z$ `; l2 M( Q% a4 M8 o7 x( @* Z: [: k' a
& n ~1 S$ u9 b Z" {5 _
3 H& D( |/ V" g7 B% A* H/ J9 U. {% ^* X$ r* p% V
[此贴子已经被作者于2004-5-10 14:36:26编辑过]
" S+ H1 n: M8 f7 Z2 q3 L* r0 K9 n |
|