|
这个代码就你局的例子都没有问题1 ~2 N) g6 F. W6 q
保存为.vbs文件
' r. P0 E- a- d& j1 V5 z. F- t- b" x3 i8 B
Function scanfnum(hfile)4 }4 L; J1 F& i/ B9 Z, c
'从文件中读取一个数字2 V" k0 I8 j8 m% V* V" g0 ]* J
Dim s, n, n_end4 G* l. }( f; `, O6 I( t3 l% j
n = ""
! {0 K2 ^3 s( B8 N' } s = ""
L4 W4 d* I" @0 \8 k1 M$ g n_end = False
6 J- c* H+ w% v. ^% D; L3 q d_end = False4 f; R* V H2 R$ m
Do While Not (hfile.AtEndOfStream)
% y8 y- }) e, c s = hfile.read(1)
' a! `- ]! j, L" J( } Do While (s < "0" Or s > "9") And (Not (hfile.AtEndOfStream))( e: F R' c6 p# O9 C) r
If n_end = True Then
2 {9 C+ x+ z. H. ^ d_end = True
0 g7 Q0 T$ k* j1 _( ]7 }% Y Exit Do
+ G/ |) {4 P" w End If% h$ A' W7 _& P* X% o [, z
s = hfile.read(1)
% B) F* s/ r" d: y- C& e Loop! Y+ [2 v U% j& u. U- X6 [8 o
If d_end Then Exit Do" K6 n) `( S7 B: D- E
If s >= "0" And s <= "9" Then9 l- |3 J& K/ W2 M
n = n + s( A( h' a# G* }4 c% J4 I
n_end = True
8 ], p' B9 }/ L End If
, n1 A0 U+ A! e! ? Loop
# W7 H; I& d9 E8 F' M If n = "" Then scanfnum = "error" Else scanfnum = CInt(n)
( y H* ~* [, A7 o
Z5 K6 N$ j, g% ]+ d# xEnd Function9 ]+ i; l# b0 g" q
, e2 E1 ? g3 f7 O( D; LFunction truenum(n1, n2, vx)
w0 c. q; k2 ^+ ?4 r ' ↑存储分数的书组* d e0 d, S5 b% x4 M3 J
'默认n1>n26 p w. f4 ]2 D% l0 C
Dim p, v1, v2
* C A3 U* m9 T& P8 q8 o2 ^5 y3 I v1 = n1
3 _8 @1 J# ?6 x2 N( N! L v2 = n25 I0 q7 C# w3 M: Y
'下面把v1拆份成为每个数字尽可能大,数量尽可能少的组合
9 X2 [- v2 K% `% k p = 100# B! l( h5 Z% H8 T' c; L) G8 U4 j) b
Do While v1 > 1 And p > 1; g0 I3 m& d+ z
If v1 Mod vx(p) = 0 Then+ `! ~4 \. T2 }9 {- z* L5 U
v1 = v1 \ vx(p)
- ?1 g# h/ w4 H! E& ~7 |$ h vx(p) = 0 '用过的数字设置0
+ M/ L7 \( j* O* e1 k, U! ^5 W End If
, m' U3 Q7 s4 D. I% W( j- m p = p - 19 K0 ]4 d/ u8 d5 m" e- L
Loop8 ^4 ^ A* \7 n4 O1 c" t
'下面一样尝试把v2拆份成为每个数字尽可能大,数量尽可能少的组合,但是v1用过的数字不能再用5 G; e( y* B g+ M- b9 s
p = 100 Y5 Q' M0 M# D5 G8 {
Do While v2 > 1 And p > 1
6 L/ x, y6 m( a9 z" h If vx(p) <> 0 Then
6 h, {* a5 b. S2 j* \1 G& V; g- d; j$ o If v2 Mod vx(p) = 0 Then! P0 b- }2 t# O" N
v2 = v2 \ vx(p)$ ~/ P- V# l: w+ k) C
vx(p) = 0
/ c. q2 f( j. n End If
3 U$ i8 w3 a( H" T2 @ End If s" H) U% l7 {' \/ J
p = p - 13 d. ?! Z$ w2 }2 _1 P4 e3 ^
Loop+ D6 Q* k5 }1 V
'查分成功则结果v2应该是1,如果v1占用了v2必需的数字就会拆份失败1 R! v& |5 d+ B) o/ P3 M/ d
If v2 = 1 Then truenum = 0 Else truenum = 1! ?: G) U3 ]0 {
End Function
! J% _/ J |' x% f* n3 a; d
3 }8 l0 G( f: z( w8 yFunction startprc(n, inpath, outpath)+ P" j: b3 E8 v0 I
' ↑气球个数
/ b- H& N/ m6 O3 z$ F* w ' ↑输入文件路径
3 R+ ~3 O6 p M4 o ' ↑输出文件路径3 @# \8 ~1 C/ F# X: I
Dim nx(2), singlenum(), cnt
, i9 V5 ~! q! S( L4 }" a% w ReDim singlenum(n)
% J9 ~, i; L+ z8 M
# E# i* x& O: x' B1 t; T# w- m Set fso = CreateObject("scripting.filesystemobject")( J) S: L4 U, q( C
If fso.fileexists(inpath) Then( }0 \4 n5 _6 c' K( `4 w# p
Set inf = fso.opentextfile(inpath)/ R3 c; T6 r! Z) C- E7 @3 ~
Set outf = fso.createtextfile(outpath, 1)
) ?) q6 E% |/ H* [& N Do While Not (inf.AtEndOfStream)
% E) }/ W% z' Z3 A For i = 1 To n
, |7 ~8 W' a1 o) R7 h singlenum(i) = i '写入每个球的分数
* ^ e8 F6 _/ A5 Q/ N; m Next4 }, w m5 b8 s I5 x: X/ M! \
'读取两个数! o0 M! e5 ~3 x' E$ k1 f( a6 K
nx(0) = scanfnum(inf)
h# r- {- C/ L nx(1) = scanfnum(inf)4 ^2 r7 b+ a0 i
If nx(0) <> "error" And nx(1) <> "error" Then+ p" x" ~! {4 u# m
If nx(0) < nx(1) Then A4 Q' a) X l: V; o
'大叔在前. P: |* A% W5 S$ c- K
nx(2) = nx(0)& C% q6 ~- _! I! C2 W
nx(0) = nx(1)- a+ {/ Q# X3 d4 K3 M
nx(1) = nx(2)
4 q; Q6 I: |- v% u* m End If' G6 A9 u5 s6 I( O3 b9 Y
opp = truenum(nx(0), nx(1), singlenum)9 G3 o' U2 h* r
outf.writeline nx(opp)' {& z% X. b- W+ X, N
End If5 K. ^. L; z1 N# e K; q# |
Loop# T/ s7 Q& R6 [! D
End If) z; S7 ]6 P- p6 J- H
End Function
5 ]# u* d, m5 U7 U1 f, V; E+ v6 ?'***************************
+ f# Z" O4 t# w- z+ t. y'这里开始运行
; J2 j' ?% w$ W9 Y'***************************3 n6 n8 |$ w6 v- f! X% V
startprc 100, "input.txt", "output.txt"* b+ d$ @/ b9 h- s5 B) k
; N5 g" ?( f. R' Z* H, H
( a! O+ W2 u9 _/ [3 m
/ |; N; H& x4 r- @" A
: G+ n" k) g: r4 \$ K [此贴子已经被作者于2004-5-10 14:36:26编辑过] / L- i/ m5 T- |7 ^& t$ f
|
|