中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-10 16:55
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » [Joint Participation][Challenge Your Thinking] Find the sums of all different combinations in a column of numbers
Printable Version  12,616 / 21
Floor1 namejm Posted 2006-12-30 10:40
荣誉版主 Posts 1,737 Credits 5,226 From 成都
  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:

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

  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 ]
Floor2 lxmxn Posted 2006-12-30 10:47
版主 Posts 4,938 Credits 11,386

  I don't understand what this means. Can you give a simple example?
Floor3 pengfei Posted 2006-12-30 10:54
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
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



The required result after processing with a batch file is:
Floor4 a9319751 Posted 2006-12-30 19:53
中级用户 Posts 170 Credits 439


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%%=!
Floor5 Primalchaos Posted 2006-12-31 04:53
初级用户 Posts 19 Credits 41
Bump it up! I'd like to know too.
Floor6 无奈何 Posted 2006-12-31 05:38
荣誉版主 Posts 356 Credits 1,338
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:


Missing items:
Floor7 youxi01 Posted 2006-12-31 12:24
高级用户 Posts 247 Credits 846 From 湖南==》广东
I'll also post a piece of code whose efficiency isn't very high:



Explanation: the numbers in the text file test.txt need to occupy at least three non-duplicate lines!
Floor8 dbc6013 Posted 2006-12-31 21:49
初级用户 Posts 16 Credits 55
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.
Floor9 qzwqzw Posted 2007-01-01 02:31
银牌会员 Posts 636 Credits 2,343
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



[ Last edited by qzwqzw on 2006-12-31 at 01:48 PM ]
Floor10 Primalchaos Posted 2007-01-01 02:52
初级用户 Posts 19 Credits 41
Impressive! Not only does it produce the result, it can also show exactly how many combinations there are.
Floor11 ccwan Posted 2007-01-01 03:42
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
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.
Floor12 weapfe Posted 2007-01-01 04:20
初级用户 Posts 66 Credits 144
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...
Floor13 tghksj Posted 2007-01-01 04:43
社区乞丐 Posts 90 Credits -49
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 ]
Floor14 flamey Posted 2007-01-01 04:55
初级用户 Posts 74 Credits 152
I added the points to the wrong person!!!!
Floor15 tao0610 Posted 2007-01-05 08:49
高级用户 Posts 218 Credits 579
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.
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023