|
这个代码就你局的例子都没有问题
' a( |/ z. {: e- @! V" Q1 R! Y! N保存为.vbs文件& K' P5 a* S8 h, L% ]" D) l
}3 e7 R+ }2 ?9 i
Function scanfnum(hfile). m; k% l. s9 F% W$ y0 ^5 n
'从文件中读取一个数字9 L/ B9 b' n t) _
Dim s, n, n_end0 D1 ^6 _& ?4 ~4 b( U
n = "": I: }- k4 ~# k+ u: H, u
s = ""$ e6 s! L. D: ?, Y- l: \
n_end = False1 b5 E! q; `$ Q
d_end = False: i7 }. @9 m8 {1 [) H
Do While Not (hfile.AtEndOfStream)
# b/ V! @ Y9 w6 D# z; E: i n s = hfile.read(1)( l N5 A6 `- @7 Z$ d/ b
Do While (s < "0" Or s > "9") And (Not (hfile.AtEndOfStream))
) {: ]6 o4 ]% C If n_end = True Then( {) s; f, [% b; n) [
d_end = True) g1 f5 W- u$ Z, }+ y/ T
Exit Do
4 Y3 m: p: I; b* V End If/ @! ^5 R! Y. w& l1 ~& T* X
s = hfile.read(1)
% S* V" y* o: N3 K0 u) F Loop
# e: B" M% u! Z) w1 H If d_end Then Exit Do
% U, F* D+ i8 |& n& t7 P2 Y8 {6 S" o If s >= "0" And s <= "9" Then9 p9 v _5 a7 x* C
n = n + s
2 ? O- S( p5 K n_end = True
% v/ R6 l- `. F" U6 V4 w* X End If
/ g- Y3 w% Z4 d& U* o0 E7 a6 g Loop
" }( ]( u8 |5 e$ _* J0 F% M( S/ p( g# d If n = "" Then scanfnum = "error" Else scanfnum = CInt(n)+ S7 C) W; N P% ]$ g6 c( `3 ~7 l6 X
# R" b9 I. T4 ?4 e4 E, E TEnd Function
4 e: l6 d9 F& B0 a3 \) _: G$ J; H$ J& Q* Y/ e2 r. D _$ @0 a) N
Function truenum(n1, n2, vx)+ y% W2 X6 ^9 h: a% j' C
' ↑存储分数的书组2 I6 R+ X9 m- j- [3 J
'默认n1>n2: \/ i0 R% ~4 h+ W4 j
Dim p, v1, v2
9 Q9 R; A4 y b2 [- @ Q- |5 b v1 = n1
$ s; n8 ~& v5 l9 U& Y7 H v2 = n21 O$ W; L5 |- M+ L
'下面把v1拆份成为每个数字尽可能大,数量尽可能少的组合
. A a( S2 k. T9 w: _ p = 100
J/ g7 q" ~2 ~ Do While v1 > 1 And p > 1
+ X5 L8 q! T7 K' L If v1 Mod vx(p) = 0 Then
* B+ n& |; R5 T# e$ z6 G. A2 } v1 = v1 \ vx(p). ~7 n Q/ {9 g8 F' |8 K
vx(p) = 0 '用过的数字设置0
) d& A% V3 j4 |) q* H8 ? End If! r5 a% M0 W! H7 C4 L/ t. r2 H) O
p = p - 1; R$ x: v! E7 g
Loop Q1 _$ z ~/ O
'下面一样尝试把v2拆份成为每个数字尽可能大,数量尽可能少的组合,但是v1用过的数字不能再用
7 q# l* B2 `& O3 P7 W p = 100
% X1 E6 }) O+ x& r8 u6 M Do While v2 > 1 And p > 1; z0 ]3 P+ o# o! R, Z# ]
If vx(p) <> 0 Then9 n) Z7 b+ {3 o/ \; Z
If v2 Mod vx(p) = 0 Then
. I& }& P! }: u/ X" f M1 m- m v2 = v2 \ vx(p)
8 L! W2 x& O( `; R& r vx(p) = 0- }) m5 P6 c/ P
End If
. Z$ a6 Z- [+ K# V6 g End If% y) J, A: @, V; O9 f
p = p - 1
' Y8 O( O9 P& Y5 o0 t Loop7 d3 y/ t* K. ?2 k& \
'查分成功则结果v2应该是1,如果v1占用了v2必需的数字就会拆份失败" N5 v! b+ I; V2 M4 y+ v
If v2 = 1 Then truenum = 0 Else truenum = 1
* s% x4 ]& N8 q" U1 }9 B+ D8 x' EEnd Function* p" q- E/ i$ z7 S7 G/ O- c5 P5 S
% p: ^- _7 w; j$ f
Function startprc(n, inpath, outpath)
& @; @5 R$ U8 A4 T6 @ ' ↑气球个数
' C5 b2 n* M3 U6 e5 I ' ↑输入文件路径
" E- t. A7 `1 w3 L: \& R ' ↑输出文件路径- R) m! @7 j2 t
Dim nx(2), singlenum(), cnt
& A9 I8 `5 ~5 b# o5 y' _8 h9 D ReDim singlenum(n)7 @( {4 g3 g- t3 V
! v4 C. v8 W( w# C* i Set fso = CreateObject("scripting.filesystemobject") A( D# n U, ?+ ?+ w
If fso.fileexists(inpath) Then+ d( c4 \3 j, ^" [
Set inf = fso.opentextfile(inpath)+ \& X' U! J8 C6 K$ M, y
Set outf = fso.createtextfile(outpath, 1)
" r9 D/ E( l& H Do While Not (inf.AtEndOfStream)0 @! ^! C+ y. j$ l6 z( i- s+ \
For i = 1 To n
$ T+ r& J$ K$ e8 W- V8 q singlenum(i) = i '写入每个球的分数' W$ v. y9 M' |' N9 r2 A4 V# f" `
Next6 j6 E8 x- g; _; `
'读取两个数
2 \3 r, i4 z' y+ Q nx(0) = scanfnum(inf)
( e# ^" P! U* q0 i nx(1) = scanfnum(inf)6 n8 y9 X$ c$ e. s
If nx(0) <> "error" And nx(1) <> "error" Then# y, v8 m, ?, O5 v
If nx(0) < nx(1) Then
! B& s( a. z0 q/ O '大叔在前
6 Q5 q6 o: e# P nx(2) = nx(0)" s' h0 z2 z# a0 J
nx(0) = nx(1)
; B- X6 i% @7 }8 z! C5 R, d nx(1) = nx(2)
# {# D: X; k; H/ I, V6 t End If
, ]9 U( x7 v& O+ r opp = truenum(nx(0), nx(1), singlenum)- c1 b N" _- t- U& V! K
outf.writeline nx(opp)
/ K6 ?- S1 w% c @* m" R End If
9 e. R( }* F9 E, Z Loop
4 V. M7 c9 ?" ^* y! K& l End If6 r8 K' j7 w$ v( @, F" H @
End Function' W: n7 G5 t% ~' M& h) a1 D
'***************************
" v' a) B5 _8 t& f+ {'这里开始运行
( l* c* z; e2 Y7 e6 s'***************************9 Q: ^! ~) H& M/ j; k
startprc 100, "input.txt", "output.txt"
' t& C% x' g$ y4 L
/ ]3 ?; L6 [' i, f" k) t9 X3 v" M+ a" M7 K
3 s/ c9 k6 x& b
: K4 Q* G f: z$ ]9 J- d
[此贴子已经被作者于2004-5-10 14:36:26编辑过]
. m, c8 u1 ^* `7 R |
|