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-22 20:27
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Joint Participation][Challenge Your Thinking] Find the sums of all different combinations in a column of numbers DigestI View 12,293 Replies 21
Original Poster Posted 2006-12-30 10:40 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
  Given a plain numeric text file, one record per line, with no blank lines, find the sums of all different combinations of this column of numbers (combinations with the same line numbers but different value order are treated as the same combination). The required output format is n1+n2+n3=sum.

  For example, if the text content is:

1
2
3
4
5

  Then the required output format is (the order may be shuffled):

1+2=3
1+3=4
1+4=5
1+5=6
2+3=5
2+4=6
2+5=7
3+4=7
3+5=8
4+5=9
1+2+3=6
1+2+4=7
1+2+5=8
1+3+4=8
1+3+5=9
1+4+5=10
2+3+4=9
2+3+5=11
3+4+5=12
1+2+3+4=10
1+2+3+5=11
1+3+4+5=13
2+3+4+5=14
1+2+3+4+5=15

  1+2+3=6 and 2+1+3=6 are treated as the same combination.

[ Last edited by namejm on 2006-12-29 at 09:55 PM ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 2 Posted 2006-12-30 10:47 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  I don't understand what this means. Can you give a simple example?
Floor 3 Posted 2006-12-30 10:54 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
What brother namejm means is that each line in a text file has a number (about twenty numbers in all), and all possible sums of these numbers need to be calculated.

test.txt

1
2
3



The required result after processing with a batch file is:

1+2=3
1+3=4
2+3=5
1+2+3=6
业精于勤而荒于嬉,形成于思而毁于随。
Floor 4 Posted 2006-12-30 19:53 ·  中国 河南 郑州 电信
中级用户
★★
Credits 439
Posts 170
Joined 2006-01-09 20:29
20-year member
UID 48707
Status Offline

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "tokens=1,2 delims=:" %%i in ('findstr /n . 1.txt') do (
set /a n%%i=%%j
set /a nnum=%%i
)
if "nnum" == "1" echo %n1%=%n1% && goto :eof
:num
set sum=
set sumf=
set /a num+=1
set /a num_1=%num%+1
set /a nnum_1=%nnum%-1
for /l %%i in (%num% 1 %nnum%) do (
call set /a sum=!sum!+%%n%%i%%
call set sumf=!sumf!+%%n%%i%%
if "!sumf!" == "+!sum!" (set sumf=!sumf:~1!) ELSE (echo !sumf!=!sum!)
)
for /l %%i in (%num_1% 1 %nnum_1%) do (
call set /a sum=!sum!-%%n%%i%%
call set sumf=!sumf:+%%n%%i%%=!
echo !sumf!=!sum!)
)
if %num% lss %nnum% goto :num
pause


The code is not complete. I don't know how to write the replacement below, hope everyone can point it out.
call set sumf=!sumf:+%%n%%i%%=!
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
无奈何 +4 2007-01-01 03:16
ccwan +3 2007-01-01 03:27
bat c c++
Floor 5 Posted 2006-12-31 04:53 ·  中国 河北 廊坊 三河市 移动
初级用户
Credits 41
Posts 19
Joined 2006-12-15 00:10
19-year member
UID 73540
Gender Male
Status Offline
Bump it up! I'd like to know too.
Floor 6 Posted 2006-12-31 05:38 ·  中国 浙江 宁波 鹏博士宽带
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
There are quite a few errors in brother a9319751's code. The errors I found include missing combinations, duplicates, incorrect partial calculation results, and so on.

The problem raised by brother namejm is extremely complicated. I still haven't found a good method. The best I can do right now still misses 4 combinations. I know where the problem is, but I can't turn it into an effective batch statement.
I'll post the code first; I'll improve it later when I have time. Hope to see some brother finish this problem.



  1. @echo off
  2. setlocal ENABLEDELAYEDEXPANSION
  3. set n=0
  4. set file=%1
  5. set yinzi=
  6. for /f "delims=" %%a in (%file%) do (
  7. set /a n+=1
  8. call :sub %%a !n!
  9. )
  10. goto :EOF

  11. :sub
  12. for /f "tokens=1,2* delims=:" %%a in ('more +%2 %file%^|findstr /n .') do (
  13. call set yinzi%%a=%1
  14. for /l %%m in (1,1,%%a) do (
  15. call set yinzi%%m=!!yinzi%%m!! + %%b
  16. call set /p x=!!yinzi%%m!! = <nul
  17. call set /a x=!!yinzi%%m!!
  18. echo !x!
  19. )
  20. )
  21. goto :EOF
Posted by 无奈何 2006-12-30 16:25

Output result:

1 + 2 = 3
1 + 2 + 3 = 6
1 + 3 = 4
1 + 2 + 3 + 4 = 10
1 + 3 + 4 = 8
1 + 4 = 5
1 + 2 + 3 + 4 + 5 = 15
1 + 3 + 4 + 5 = 13
1 + 4 + 5 = 10
1 + 5 = 6
2 + 3 = 5
2 + 3 + 4 = 9
2 + 4 = 6
2 + 3 + 4 + 5 = 14
2 + 4 + 5 = 11
2 + 5 = 7
3 + 4 = 7
3 + 4 + 5 = 12
3 + 5 = 8
4 + 5 = 9


Missing items:

1+2+4=7
1+2+5=8
1+3+5=9
2+3+5=11
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
ccwan +9 2007-01-01 03:27
redtek +5 2007-01-01 04:43
yuhaomiao +1 2007-01-02 04:57
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 7 Posted 2006-12-31 12:24 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
I'll also post a piece of code whose efficiency isn't very high:


@echo off
setlocal enabledelayedexpansion

for /f %%i in (test.txt) do (
set flag=%%i

REM ===========================
REM Delete duplicate lines;
Rem ===========================
if not defined !flag! (
set %%i=A

REM ===========================
REM Get the number of file lines;
Rem ===========================

set /a CYC_num_+=1
set str=%%i !str!))

REM ===========================
REM Set the loop count for the next for loop;
Rem ===========================
set /a CYC_num=%CYC_num_%-3



call :test str
for /l %%i in (1 1 %CYC_num%) do call :test Res
pause>nul
:test
for /f "delims== tokens=2" %%i in ('set %1') do (
set tem=%%i
for %%a in (%%i) do (
set flag=!tem:%%a =!
if not defined !flag! (
set var=
set num=
set /a a+=1
set Res!a!=!tem:%%a =!
call :Get_SUM %%Res!a!%%
set !flag!=A
)
)
)
goto :eof

:Get_SUM
if not "%1"=="" (
set var=%1+!var!
set /a num+=%1
shift
goto :Get_SUM)
echo !var:~0,-1!=!num!


Explanation: the numbers in the text file test.txt need to occupy at least three non-duplicate lines!
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
无奈何 +8 2007-01-01 03:15
ccwan +7 2007-01-01 03:27
redtek +7 2007-01-01 04:42
Floor 8 Posted 2006-12-31 21:49 ·  中国 河北 廊坊 三河市 移动
初级用户
Credits 55
Posts 16
Joined 2006-10-27 21:01
19-year member
UID 68519
Gender Male
Status Offline
Originally posted by 无奈何 at 2006-12-31 05:38 AM:
There are quite a few errors in brother a9319751's code. The errors I found include missing combinations, duplicates, incorrect partial calculation results, and so on.

The problem raised by brother namejm is extremely complicated. I still haven't found a good method..


I'm very interested in this problem. Could you explain the train of thought?
Other experts can explain it too,
thanks in advance here.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
flamey +2 2007-01-01 04:53
Floor 9 Posted 2007-01-01 02:31 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
For this kind of problem, using a recursive algorithm makes the idea much clearer

Although multi-level loops can also do it, they are very hard to write and read

Below is an example of recursion


@echo off
setlocal enabledelayedexpansion

for /f %%n in (test.txt) do (
set /a i+=1
set gn!i!=%%n
)
set gn
pause

for /l %%j in (1,1,%i%) do call :rec %%j
pause
goto :eof

:rec
setlocal

call set tmp=%%gn%1%%
set /a sum+=%tmp%
set /a lvl+=1
if %lvl% gtr 1 (
set exp=%exp%+%tmp%
set /a idx+=1
echo !idx!:!exp!=%sum%
) else (set exp=%tmp%)

set /a nxt=%1+1
for /l %%j in (%nxt%,1,%i%) do call :rec %%j

endlocal & set idx=%idx%
goto :eof


[ Last edited by qzwqzw on 2006-12-31 at 01:48 PM ]
Recent Ratings for This Post ( 9 in total) Click for details
RaterScoreTime
无奈何 +12 2007-01-01 03:15
ccwan +11 2007-01-01 03:26
namejm +12 2007-01-01 04:09
tghksj +2 2007-01-01 04:39
redtek +11 2007-01-01 04:41
flamey +2 2007-01-01 04:55
qasa +2 2007-01-05 15:16
lxmxn +10 2007-01-05 23:48
pengfei +15 2007-01-07 13:15
Floor 10 Posted 2007-01-01 02:52 ·  中国 河北 廊坊 三河市 移动
初级用户
Credits 41
Posts 19
Joined 2006-12-15 00:10
19-year member
UID 73540
Gender Male
Status Offline
Impressive! Not only does it produce the result, it can also show exactly how many combinations there are.
Floor 11 Posted 2007-01-01 03:42 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
The code from several of you is truly brilliant, containing wisdom and a solid accumulation of knowledge. It's an example for me to learn from.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 12 Posted 2007-01-01 04:20 ·  中国 江苏 苏州 中移铁通
初级用户
★★
Credits 144
Posts 66
Joined 2006-11-30 00:37
19-year member
UID 72121
Gender Male
Status Offline
As the title says. The contents of my test.txt are:



The result is:
gn1=1
gn2=2
gn3=3
Press any key to continue. . .
1:1+2=3
2:1+2+3=6
3:1+3=4
4:2+3=5
Press any key to continue. . .
Not sure if it's...
Floor 13 Posted 2007-01-01 04:43 ·  中国 山东 济南 电信
社区乞丐
★★
此图片另存后死机
Credits -49
Posts 90
Joined 2006-12-02 13:00
19-year member
UID 72412
Gender Male
Status Offline
9th floor qzwqzw

I can only add two points, I'll take your code home and study it carefully...

I can't see where the duplicate combinations are excluded?

--------------------
I'll look at it slowly first,
for now I completely understand everything before the first pause.....
---------------------
But I get stuck at :rec.............

Can someone tell me what that %1 is about????
------------------------------------
Now I know what += means........ turns out it's similar to C....
------------------------------------
......
After turning ECHO ON
I went through the code line by line against the result.....
I still don't quite understand :rec....
-------------------------
Looking again, I must understand it this time~ I didn't learn recursion well when I studied C, this time I have to really work at it.......
----------------------
Got it...... I understand it now!

[ Last edited by tghksj on 2007-1-1 at 11:58 AM ]
我的网络笔记本.非联系本人请勿访问!http://w.vicp.net
Floor 14 Posted 2007-01-01 04:55 ·  中国 广东 肇庆 电信
初级用户
★★
Credits 152
Posts 74
Joined 2005-12-01 23:06
20-year member
UID 46314
Status Offline
I added the points to the wrong person!!!!
Floor 15 Posted 2007-01-05 08:49 ·  中国 北京 朝阳区 联通
高级用户
★★
朦胧的世界
Credits 579
Posts 218
Joined 2006-10-24 04:29
19-year member
UID 67972
Status Offline
After a few days of holiday, quite a few good things turned up....

The recursion in the 9th-floor post is indeed ingenious, but considering efficiency it's best to avoid FOR nesting and CALL recursion.

Does not support duplicate numbers.

@echo off&setlocal enabledelayedexpansion
set n=1
for /f %%n in (test.txt) do (
set /a i+=1
set str!i!=%%n
)
set str
pause&echo.

:loop
set/a n+=1
if not defined str%n% goto end
for /f "tokens=1* delims==" %%a in ('set result 2^>nul') do (
set/a x+=1
set result!x!=%%b+!str%n%!
)
set/a f=n-1
if not defined flag!str%n%! for /l %%j in (1,1,%f%) do (
set/a x+=1
set result!x!=!str%%j!+!str%n%!
)
set/a flag!str%n%!+=1
goto :loop

:end
for /f "tokens=1* delims==" %%a in ('set result 2^>nul') do (
set/a id+=1
set/a %%a=%%b
echo !id!:%%b=!%%a!
)
pause&goto :eof
Recent Ratings for This Post ( 4 in total) Click for details
RaterScoreTime
lxmxn +10 2007-01-05 23:54
redtek +9 2007-01-07 00:56
pengfei +5 2007-01-07 13:17
Hanyeguxing +4 2009-07-05 09:00

认识自己,降伏自己,改变自己
,才能改变别人!
Forum Jump: