|
jmz573515
银牌会员
    
积分 1212
发帖 464
注册 2006-12-13
状态 离线
|
 『楼 主』:
给5个人分10个苹果的方法
使用 LLM 解释/回答一下
给5个人分10个苹果(每人至少分到一个苹果)
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 ]
### 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 ]
此帖被 +5 点积分 点击查看详情 评分人:【 ccwan 】 | 分数: +5 | 时间:2007-1-21 03:14 |
|
|
|
2007-1-20 23:54 |
|
|
PPdos
高级用户
   
积分 783
发帖 268
注册 2006-12-26
状态 离线
|
『第 2 楼』:
要求应该再加上:
使用 LLM 解释/回答一下
每人至少分到一个苹果 不然有10与0的分法 就不止126了
Each person gets at least one apple. Otherwise, there are distributions like 10 and 0, which would make the number more than 126.
|

菩提本无树,明镜亦非台,本来无一物,何处惹尘埃. |
|
2007-1-21 00:17 |
|
|
jmz573515
银牌会员
    
积分 1212
发帖 464
注册 2006-12-13
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
谢谢2楼的提醒,是应该有“每人至少分到一个苹果”的条件。
Thanks to the reminder from user 2, there should be the condition that "each person gets at least one apple".
|
|
2007-1-21 00:21 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
  『第 4 楼』:
使用 LLM 解释/回答一下
我跟一个bat版的吧:
@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人分10个苹果^(每人至少分到一个^)总共有%count%种分法
)>>result.txt
start result.txt
还是把粗线条添上吧,免得影响美观<img src="images/smilies/face-raspberry.png" align="absmiddle" border="0">
Last edited by namejm on 2007-1-20 at 02:50 PM ]
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 :P
Last edited by namejm on 2007-1-20 at 02:50 PM ]
此帖被 +9 点积分 点击查看详情 评分人:【 ccwan 】 | 分数: +5 | 时间:2007-1-21 03:14 | 评分人:【 PPdos 】 | 分数: +2 | 时间:2007-1-21 14:40 | 评分人:【 huzixuan 】 | 分数: +2 | 时间:2007-2-1 07:24 |
|
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-21 03:07 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
百家争鸣是很好的学习方法,欣赏二位~~
Hundred schools of thought contending is a very good learning method, appreciate the two of you~~
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2007-1-21 03:15 |
|
|
tao0610
高级用户
    朦胧的世界
积分 579
发帖 218
注册 2006-10-24
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
可以用递归不过效率会很低.
It can be done recursively, but the efficiency will be very low.
|

认识自己,降伏自己,改变自己,才能改变别人! |
|
2007-1-21 03:36 |
|
|
PPdos
高级用户
   
积分 783
发帖 268
注册 2006-12-26
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
不得不服 版主排版如此清晰层次分明 使人一目了然 在下惭愧了。。
但从计算速度来vbs更胜一筹
大概因为这句 if !num! equ 10 让程序自动匹配所造成的 。。如果想增加速度又不
知道有没其他方法?
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?
|

菩提本无树,明镜亦非台,本来无一物,何处惹尘埃. |
|
2007-1-21 14:37 |
|
|
jmz573515
银牌会员
    
积分 1212
发帖 464
注册 2006-12-13
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
修改一下,可以输入苹果数就好了。
Last edited by jmz573515 on 2007-1-22 at 10:07 PM ]
Just modify it so that the number of apples can be entered.
Last edited by jmz573515 on 2007-1-22 at 10:07 PM ]
|
|
2007-1-22 00:16 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
那我也修改一下可以指定苹果数的批处理。测试了15个苹果数,批处理果然慢得很,并且排版也会有问题(虽然可以处理一下,但是代码会比较复杂,也没有多大的通用性):
@echo off
:begin
cls
set count=0
set input=
set /p input= 请输入苹果数(退出请直接回车):
if not defined input exit
if %input% lss 5 echo 苹果数太少,请重新输入&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 5人分%input%个苹果^(每人至少分到一个^)总共有%count%种分法
)>>result.txt
start result.txt
goto begin
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-22 00:38 |
|
|
tghksj01
初级用户
 
积分 23
发帖 8
注册 2007-1-20
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
这其实是一个分割法的问题,
namejm 老师及前面几位老师使用嵌套碰撞测试的方法
在模数较大时会极度浪费资源,而且效率极低
比较直接的方法是利用IF语句控制分割规律,直接按规律给出结果.
下面是一个粗劣的例子:
::分苹果问题的延伸,分割法规律控制.
::tghksj@http://www.cn-dos.net/
::tghksj@http://w.vicp.net/
::转载请注明出处.谢谢啊:) 20070122
@ECHO %DBG% OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:SETUP
SET /P RS=(回车退出)输入人数: & IF NOT DEFINED RS EXIT /B
SET /P PG=(回车退出)输入苹果数: & IF NOT DEFINED PG EXIT /B
SET /P ZS=输入每人最少分苹果数: & 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 不可能,重新输入. & 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 ]
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 ]
此帖被 +7 点积分 点击查看详情 评分人:【 PPdos 】 | 分数: +3 | 时间:2007-1-23 10:40 | 评分人:【 namejm 】 | 分数: +4 | 时间:2007-1-23 10:59 |
|
|
|
2007-1-23 08:50 |
|
|
tghksj01
初级用户
 
积分 23
发帖 8
注册 2007-1-20
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
好久没来了,代码优化了一下,顶一下,路过的评测一下,
----------------
今天去市里参加计算机技能比武,名次又是内定的....郁闷...
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....郁闷...
|
|
2007-2-1 05:22 |
|
|
huzixuan
高级用户
   
积分 537
发帖 219
注册 2006-10-31 来自 芜湖
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
一定要眼看明白!
Make sure to see clearly!
|

江湖远
碧空长
路茫茫
一个人漫无目的的奔跑,风,刺骨的冷.... |
|
2007-2-1 07:26 |
|
|
anqing
高级用户
   
积分 859
发帖 413
注册 2006-8-14
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
虽然看不全懂,佩服
Although I can't understand it all, I admire it
|
|
2007-2-1 09:51 |
|
|
gl315
新手上路

积分 10
发帖 5
注册 2007-1-24
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
真羡慕,什么时候才能到这种水平啊?
I really envy, when will I be able to reach this level?
|
|
2007-2-2 01:46 |
|
|
qjbm
初级用户
 
积分 125
发帖 44
注册 2007-1-24
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
VBS确实有先天优势...这点在此帖的代码比拼中完全体现出来了.
同样的"穷举碰撞测试算法"
VBS的:
if a+b+c+d+e=ccur(i) then
比批处理的:
set /a num=%%i+%%j+%%k+%%l+%%m
if !num! equ %input% (...)
快的叫人匪夷所思....
难道VBS在执行时会自动优化算法?
其速度简直与10楼批处理的"分割法规律控制"算法相抗衡.
10楼MAIN"函数"已经是极至算法..可是速度与VBS代码的穷举法相当...
看来批处理用来节省一下体力还不错,真正用来复杂计算就有点不能胜任了....
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....
|
|
2007-2-3 02:54 |
|
|