China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-25 06:55
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Methods to distribute 10 apples among 5 people View 2,526 Replies 14
Original Poster Posted 2007-01-20 23:54 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
### Distribute 10 apples among 5 people (each person gets at least one apple)

aa=0
do until aa=1
i=inputbox("你想给5人分几个苹果?(每人至少一个)","请输入苹果数")
if i="" then
wscript.quit
elseif IsNumeric(i)=false then
msgbox "输入错误,请重新输入!",16+4096,"错误"
elseif i<5 then
msgbox "苹果数不能小于5,请重新输入!",16+4096,"错误"
elseif i>15 then
yesno=msgbox("苹果数大于15时速度可能会很慢,要继续吗?",32+4096+vbyesno,"提示")
if yesno=vbyes then
aa=1
end if
else
aa=1
end if
loop

set fso=createobject("scripting.filesystemobject")
set ws=createobject("wscript.shell")
set file=fso.createtextfile("OUT.txt",2)
for a=1 to ccur(i)-4
for b=1 to ccur(i)-4
for c=1 to ccur(i)-4
for d=1 to ccur(i)-4
for e=1 to ccur(i)-4
if a+b+c+d+e=ccur(i) then
h=h+1
s=s & "│" & a & vbtab & "│" & b & vbtab & "│" & c & vbtab & "│" & d & vbtab & "│" & e &vbtab &"│" &vbcrlf & "├───┼───┼───┼───┼───┤"&vbcrlf

end if
next
next
next
next
next
s=Left(s,Len(s)-Len("├───┼───┼───┼───┼───┤"&vbcrlf))
file.write "给5个人分"&i&"个苹果(每人至少分到一个苹果)一共有" & h &"种方法" & vbcrlf & vbcrlf
file.write "┌───┬───┬───┬───┬───┐"& VBcrlf
file.write "│A" & vbtab &"│B" & vbtab &"│C" & vbtab &"│D" & vbtab &"│E" & vbtab &"│" & VBcrlf
file.write "┝━━━┿━━━┿━━━┿━━━┿━━━┥"& VBcrlf
file.write s
file.write "└───┴───┴───┴───┴───┘"
file.close
ws.run "notepad.exe OUT.txt"


[ Last edited by jmz573515 on 2007-2-1 at 02:27 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
ccwan +5 2007-01-21 03:14
Floor 2 Posted 2007-01-21 00:17 ·  美国 弗吉尼亚州 华盛顿县 Microsoft
高级用户
★★
Credits 783
Posts 268
Joined 2006-12-26 17:18
19-year member
UID 74627
Gender Male
Status Offline
Each person gets at least one apple. Otherwise, there are distributions like 10 and 0, which would make the number more than 126.
菩提本无树,明镜亦非台,本来无一物,何处惹尘埃.
Floor 3 Posted 2007-01-21 00:21 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
Thanks to the reminder from user 2, there should be the condition that "each person gets at least one apple".
Floor 4 Posted 2007-01-21 03:07 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
I'm following a bat version:

@echo off
(
echo ┌───┬───┬───┬───┬───┐
echo │ A │ B │ C │ D │ E │
)>result.txt
setlocal enabledelayedexpansion
for /l %%i in (1,1,6) do (
for /l %%j in (1,1,6) do (
for /l %%k in (1,1,6) do (
for /l %%l in (1,1,6) do (
for /l %%m in (6,-1,1) do (
set /a num=%%i+%%j+%%k+%%l+%%m
if !num! equ 10 (
if !count! equ 0 (echo ┝━━━┿━━━┿━━━┿━━━┿━━━┥
) else echo ├───┼───┼───┼───┼───┤
echo │ %%i │ %%j │ %%k │ %%l │ %%m │
)>>result.txt&set /a count+=1
)
)
)
)
)
(
echo └───┴───┴───┴───┴───┘
echo 5 people divide 10 apples ^(each person gets at least one^) there are %count% ways of distribution
)>>result.txt
start result.txt

Still add the thick lines, so as not to affect the appearance

[ Last edited by namejm on 2007-1-20 at 02:50 PM ]
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
ccwan +5 2007-01-21 03:14
PPdos +2 2007-01-21 14:40
huzixuan +2 2007-02-01 07:24
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 5 Posted 2007-01-21 03:15 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Hundred schools of thought contending is a very good learning method, appreciate the two of you~~
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 6 Posted 2007-01-21 03:36 ·  中国 北京 朝阳区 联通
高级用户
★★
朦胧的世界
Credits 579
Posts 218
Joined 2006-10-24 04:29
19-year member
UID 67972
Status Offline
It can be done recursively, but the efficiency will be very low.

认识自己,降伏自己,改变自己
,才能改变别人!
Floor 7 Posted 2007-01-21 14:37 ·  美国 弗吉尼亚州 华盛顿县 Microsoft
高级用户
★★
Credits 783
Posts 268
Joined 2006-12-26 17:18
19-year member
UID 74627
Gender Male
Status Offline
I have to admit, the moderator's layout is so clear and well-structured that it's easy to understand at a glance. I feel ashamed.

But in terms of calculation speed, VBS is a bit better.

Probably because of this line "if !num! equ 10" that makes the program match automatically. .. If you want to increase the speed and don't know if there are other methods?
菩提本无树,明镜亦非台,本来无一物,何处惹尘埃.
Floor 8 Posted 2007-01-22 00:16 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
Just modify it so that the number of apples can be entered.

[ Last edited by jmz573515 on 2007-1-22 at 10:07 PM ]
Floor 9 Posted 2007-01-22 00:38 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Then I also modified the batch script that can specify the number of apples. I tested with 15 apples, and the batch script was indeed very slow, and the layout would also have problems (although it can be dealt with, but the code would be more complicated and not very universal):

@echo off

:begin
cls
set count=0
set input=
set /p input= Please enter the number of apples (press Enter directly to exit):
if not defined input exit
if %input% lss 5 echo The number of apples is too small, please enter again&pause&goto begin
set /a max=%input%-4
(
echo ┌───┬───┬───┬───┬───┐
echo │ A │ B │ C │ D │ E │
)>result.txt
setlocal enabledelayedexpansion
for /l %%i in (1,1,%max%) do (
for /l %%j in (1,1,%max%) do (
for /l %%k in (1,1,%max%) do (
for /l %%l in (1,1,%max%) do (
for /l %%m in (%max%,-1,1) do (
set /a num=%%i+%%j+%%k+%%l+%%m
if !num! equ %input% (
if !count! equ 0 (echo ┝━━━┿━━━┿━━━┿━━━┿━━━┥
) else echo ├───┼───┼───┼───┼───┤
echo │ %%i │ %%j │ %%k │ %%l │ %%m │
)>>result.txt&set /a count+=1
)
)
)
)
)
(
echo └───┴───┴───┴───┴───┘
echo There are %count% ways to divide %input% apples among 5 people (each person gets at least one)
)>>result.txt
start result.txt
goto begin
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 10 Posted 2007-01-23 08:50 ·  中国 山东 济南 电信
初级用户
Credits 23
Posts 8
Joined 2007-01-20 11:37
19-year member
UID 77140
Gender Male
Status Offline
This is actually a problem of the division method.
Teacher namejm and the previous几位 teachers used the method of nested collision testing, which will extremely waste resources and have extremely low efficiency when the modulus is relatively large. A more direct method is to use the IF statement to control the division rule and directly give the result according to the rule.
The following is a crude example:

::Extension of the apple - dividing problem, division rule control.
::tghksj@http://www.cn-dos.net/
::tghksj@http://w.vicp.net/
::Please indicate the source when reprinting. Thank you :) 20070122
@ECHO %DBG% OFF
SETLOCAL ENABLEDELAYEDEXPANSION

:SETUP
SET /P RS=(Press Enter to exit)Enter the number of people: & IF NOT DEFINED RS EXIT /B
SET /P PG=(Press Enter to exit)Enter the number of apples: & IF NOT DEFINED PG EXIT /B
SET /P ZS=Enter the minimum number of apples each person gets: & IF NOT DEFINED ZS EXIT /B
SET /A ZD=%PG% - (%RS% - 1)*%ZS%,f=0,g=%RS% - 1,j=%RS% - 2
IF %ZD% LSS %ZS% (ECHO Impossible, re-enter. & GOTO :SETUP)
FOR /L %%i IN (1,1,%RS%) DO (SET /A R%%i=!ZS! & IF!RS!==%%i SET /A R%%i=!ZD!)

:MAIN
CALL :PRI & IF %R1%==%ZD% GOTO :EOF
IF!R%RS%!==%ZS% (
CALL :GUI & CALL :JIN & CALL :ADD
SET /A R%RS%=%PG% -!h!,j=%RS% - 2
GOTO :MAIN
)
SET /A R%RS% -=1,R%g% +=1
GOTO :MAIN

:PRI
FOR /L %%i IN (1,1,%RS%) DO SET /P=!R%%i! <NUL
SET /A f +=1 & ECHO :!f!
GOTO :EOF

:GUI
FOR /L %%i IN (%j%,1,%g%) DO (IF %%i NEQ %j% SET /A R%%i=%ZS%)
GOTO :EOF

:JIN
SET /A R%j% +=1
CALL :ADD
SET /A h=%h% +(%ZS% - 1)
IF %h%==%PG% (SET /A j -=1 & CALL :GUI & CALL :JIN)
GOTO :EOF

:ADD
SET /A h=0
FOR /L %%i IN (1,1,%g%) DO (SET /A h +=!R%%i!)
GOTO :EOF


[ Last edited by tghksj01 on 2007-1-31 at 04:20 PM ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
PPdos +3 2007-01-23 10:40
namejm +4 2007-01-23 10:59
Floor 11 Posted 2007-02-01 05:22 ·  中国 山东 济南 电信
初级用户
Credits 23
Posts 8
Joined 2007-01-20 11:37
19-year member
UID 77140
Gender Male
Status Offline
Long time no see. I optimized the code a bit. Give it a thumbs up. Passers - by, please evaluate.

----------------
Went to the city today to participate in the computer skills competition, but the ranking was fixed again....郁闷...
Floor 12 Posted 2007-02-01 07:26 ·  中国 安徽 芜湖 电信
高级用户
★★
Credits 537
Posts 219
Joined 2006-10-31 21:08
19-year member
UID 69036
Gender Male
From 芜湖
Status Offline
Make sure to see clearly!
江湖远
碧空长
路茫茫

一个人漫无目的的奔跑,风,刺骨的冷....
Floor 13 Posted 2007-02-01 09:51 ·  中国 吉林 四平 联通
高级用户
★★★
Credits 859
Posts 413
Joined 2006-08-14 21:55
19-year member
UID 60532
Status Offline
Although I can't understand it all, I admire it
Floor 14 Posted 2007-02-02 01:46 ·  中国 广东 惠州 电信
新手上路
Credits 10
Posts 5
Joined 2007-01-24 06:41
19-year member
UID 77513
Gender Male
Status Offline
I really envy, when will I be able to reach this level?
Floor 15 Posted 2007-02-03 02:54 ·  中国 山东 济南 电信
初级用户
Credits 125
Posts 44
Joined 2007-01-24 15:31
19-year member
UID 77555
Gender Female
Status Offline
VBS indeed has inherent advantages... This is fully reflected in the code competition in this post.

The same "exhaustive collision test algorithm"

VBS's:

if a+b+c+d+e=ccur(i) then

is much faster than the batch processing's:

set /a num=%%i+%%j+%%k+%%l+%%m
if !num! equ %input% (...)

to an astonishing degree....

Does VBS automatically optimize the algorithm during execution?

Its speed is almost comparable to the "segmentation method rule control" algorithm on floor 10.

The "function" on floor 10 is already the ultimate algorithm.. but the speed is comparable to the exhaustive method of VBS code...

It seems that batch processing is okay for saving some effort, but it's a bit incompetent when it comes to complex calculations....
Forum Jump: