|
这个代码就你局的例子都没有问题
/ G2 a9 G1 B. l O j2 x5 D1 M3 O保存为.vbs文件
' T6 U3 P9 f1 q1 V/ y
5 K+ F r$ w" h) V# M) NFunction scanfnum(hfile)% [) E- Q1 k. Z' a. Y
'从文件中读取一个数字/ u$ R9 J: B% J: C
Dim s, n, n_end
" V: ^8 k4 T$ w* Y n = ""+ K* Y* H- ~) I6 Z) v- w
s = ""# i, p- J& ^1 P! d4 a! J7 L
n_end = False+ Q3 a4 Y( {* w; W4 b( U0 i, \
d_end = False
! h% m/ A. K4 P- ]2 v Do While Not (hfile.AtEndOfStream)( w; t/ T5 ?9 B
s = hfile.read(1)
; d+ h7 e% _5 F2 S Do While (s < "0" Or s > "9") And (Not (hfile.AtEndOfStream))
/ x. k" W# p- p' L* ` If n_end = True Then
4 G& N. ~. B0 W1 _0 l% l d_end = True
6 @- w; B, g" O; O! M Exit Do
) a% K- b7 o: C$ C2 O2 K, c( O( | End If
0 d# z' C# }5 c+ X s = hfile.read(1)
& R$ ^- [$ Q G4 n Loop
6 j- x* S0 C+ j. j$ _ y( ~5 p P" M If d_end Then Exit Do
) O2 ^* W/ O" x* A If s >= "0" And s <= "9" Then
. a5 C( ?. s) Y7 } n = n + s
% D- R1 h" l) B n_end = True2 |0 c3 H" G- v9 P) N/ F. `
End If
. I% W6 B, |! w( u& e/ l! L: [ Loop, r$ s; A: u; x# y5 k3 f3 M
If n = "" Then scanfnum = "error" Else scanfnum = CInt(n)' }% Q! X% e) ?9 O J! j0 z
; {0 V1 Q& \4 ^8 {( \/ e- D3 FEnd Function
% ? @& g( Z( Z- p; x
2 o6 f# R: u/ X4 }. N9 ~: C; A! ?Function truenum(n1, n2, vx), r" E Y q1 R; R
' ↑存储分数的书组7 s8 @6 I! k8 T
'默认n1>n2: ^. a) _7 ?. x' `( }* G, k; \4 ^
Dim p, v1, v2, T; I+ |# w( W; o) S+ w+ Q
v1 = n12 p( X) M9 Z/ M5 V4 e- s% y b, [
v2 = n2
: O, Y8 p, c7 ?3 o '下面把v1拆份成为每个数字尽可能大,数量尽可能少的组合
1 f: p& l5 _& a7 a p = 100, [8 [3 B' }4 S+ s& H: x2 {
Do While v1 > 1 And p > 14 q5 U' d3 S' ]7 O/ m# d
If v1 Mod vx(p) = 0 Then$ z: Z% @: J5 w0 e2 N+ ?
v1 = v1 \ vx(p)/ U5 B" u/ a |+ G
vx(p) = 0 '用过的数字设置0
6 c" v+ J$ v% N! H* I* w2 e* t End If
, P6 N) Y5 Z( @ p = p - 1
0 E2 B5 b* m/ ?! y Loop
% S8 v9 b; D, k '下面一样尝试把v2拆份成为每个数字尽可能大,数量尽可能少的组合,但是v1用过的数字不能再用( U% b8 Z+ t( V4 m# k! }
p = 100
7 `$ \# z2 n% D, M Do While v2 > 1 And p > 1
3 ?( E8 F' ?" W4 }$ t If vx(p) <> 0 Then7 T4 Z7 h4 I9 p/ A( u2 X
If v2 Mod vx(p) = 0 Then& }7 ]4 D) [8 H6 a
v2 = v2 \ vx(p)9 m q/ L2 B& W- j
vx(p) = 0+ l" V3 r5 N; ?0 o; ~
End If+ W. @$ J# Z1 @$ W$ S9 w% D; D; q
End If9 V4 n7 k6 `0 B% |7 A3 D
p = p - 1
$ i, j& E) p/ }9 Z5 y5 P. s$ ~ Loop
3 s) K" g2 n7 T" Q '查分成功则结果v2应该是1,如果v1占用了v2必需的数字就会拆份失败' V- d. s2 n. ?8 d3 a, B T
If v2 = 1 Then truenum = 0 Else truenum = 1
- P7 L% \$ b- k5 E, q% `% eEnd Function
* U/ [( \. m4 W8 P
/ O# \. A6 C( Y9 rFunction startprc(n, inpath, outpath)
7 Z+ F8 h Y0 H6 V; g' {# H7 O ' ↑气球个数. \; C0 v$ v5 T8 a
' ↑输入文件路径$ M: M( n9 e6 t6 K2 P# x# M2 f x3 q
' ↑输出文件路径
6 C% A* |) V. z7 I Dim nx(2), singlenum(), cnt8 J+ u) m+ \7 X3 y0 J
ReDim singlenum(n)
! W2 f, R- d; K8 |" ?
9 y& I- U8 c/ O0 L; q& l( y Set fso = CreateObject("scripting.filesystemobject")
h7 a, m0 [. X) N8 g. S If fso.fileexists(inpath) Then& f- T. t+ k5 s: e8 y
Set inf = fso.opentextfile(inpath)
! q9 K- f. V0 l! T0 O+ e, j Set outf = fso.createtextfile(outpath, 1)- q! I/ \! E; c6 \9 ]
Do While Not (inf.AtEndOfStream)- z6 g9 ?% Z$ C, \$ M! L
For i = 1 To n. d1 x/ t0 j( X% o8 l; B% e
singlenum(i) = i '写入每个球的分数" j' e O) [: N: f, A
Next* b/ M$ G" [, k% c
'读取两个数
T% y% D; m, a H7 o5 b nx(0) = scanfnum(inf)' o8 l O$ Z2 k% _
nx(1) = scanfnum(inf)
; K- R* ?3 ~ l( j% ~ H) l If nx(0) <> "error" And nx(1) <> "error" Then
; k; p0 g3 v! I5 W! l. B If nx(0) < nx(1) Then
4 A% C, [8 g1 y. v1 L '大叔在前5 @ L3 O" F) a( o5 O t) W' d
nx(2) = nx(0)
- ]. L! K. G3 _: n7 X nx(0) = nx(1)( K4 F# Q( r7 ]8 _! T
nx(1) = nx(2)0 c. k9 u+ Y; Y$ _- d+ Q
End If
' U7 c5 b u9 `7 t& R% a& n9 C opp = truenum(nx(0), nx(1), singlenum)! F$ k6 u Z/ A7 q) `' N
outf.writeline nx(opp)
) A6 P; ]. P8 m* j! X End If1 b1 G0 @" N! y
Loop( Z1 w- B! k2 ?* Z" O8 k4 n) F
End If8 d. x( ?3 @3 f' I
End Function' ^. J: j+ K2 y- ^ A) a% E
'***************************
6 m) B2 n- k% C0 R5 j'这里开始运行9 |- ~9 w. t2 f; g, A/ D! g
'**************************** J1 c T8 B0 O
startprc 100, "input.txt", "output.txt"
2 H3 k: S! S, ~$ R" w [; H+ u4 O7 g8 _7 c# _
% C: b" y/ x: c$ y3 A( P
: m6 u5 s8 ]7 k" V" @7 y" C4 \0 [9 K
[此贴子已经被作者于2004-5-10 14:36:26编辑过] & X ]$ r, R5 s# `7 U
|
|