![]() |
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-10-02 21:46 |
47,813 topics / 349,918 posts / today 0 new / 48,279 members |
| DOS批处理 & 脚本技术(批处理室) » Numerical comparison |
| Printable Version 3,824 / 19 |
| Floor1 huahua0919 | Posted 2010-11-11 16:15 |
| 银牌会员 Posts 780 Credits 1,608 | |
|
Today I saw a problem, put it on the forum for everyone to write, hehe!
Input two lines of strings from the command line, the strings conform to the following format 1 1 3 5 5 3 2 1 All are composed of ten digits from 0-9, each digit is odd, and separated by spaces in the middle. We represent the first group of input as A, the second group as B, and determine how many are the same in each corresponding bit of A and B. The following numbers: ============== 1 2 3 5 1 1 5 5 ============== Among them, there are two groups of correspondences that are the same, namely (1,1) and (5,5). Expressed as 2A, if there is only one group of the same, it is expressed as 1A, three groups of the same are expressed as 3A, and so on. After comparing the A type (corresponding to the same), then determine whether there are the same numbers in the remaining A and B. One group counts as 1B, two groups count as 2B, and do not compare repeatedly, and so on. The above result is 2A0B. The following are examples: ================== 1 1 2 2 2 2 1 1 0A4B ================== 1 3 3 3 3 1 1 1 0A2B ================== 1 2 3 4 4 3 2 1 0A4B ================== 1 1 1 1 1 1 1 1 4A0B ================== 1 3 5 6 2 3 5 9 2A0B ================== Just output the result nAmB |
|
| Floor2 huahua0919 | Posted 2010-11-13 12:15 |
| 银牌会员 Posts 780 Credits 1,608 | |
|
Why is there no one writing it? If it were before, a group of people would stack code. I would post my garbage code.
|
|
| Floor3 zhoupeng243 | Posted 2010-11-13 12:59 |
| 新手上路 Posts 15 Credits 13 | |
|
@echo off
setlocal enabledelayedexpansion color 0b set /p aa=Enter line A characters: set /p bb=Enter line B characters: set n=0 set m=0 for /f "tokens=1,2,3,4* delims= " %%a in ("%aa%") do ( for /f "tokens=1,2,3,4* delims= " %%i in ("%bb%") do ( set a=%%a & set b=%%b & set c=%%c & set d=%%d set i=%%i & set j=%%j & set k=%%k & set l=%%l if "!a!"=="!i!" (set /a n+=1)&(set a=a)&(set i=i) if "!b!"=="!j!" (set /a n+=1)&(set b=b)&(set j=j) if "!c!"=="!k!" (set /a n+=1)&(set c=c)&(set k=k) if "!d!"=="!l!" (set /a n+=1)&(set d=d)&(set l=l) if "!a!"=="!j!" (set /a m+=1)&(set a=a)&(set j=j) if "!a!"=="!k!" (set /a m+=1)&(set a=a)&(set k=k) if "!a!"=="!l!" (set /a m+=1)&(set a=a)&(set l=l) if "!b!"=="!i!" (set /a m+=1)&(set b=b)&(set i=i) if "!b!"=="!k!" (set /a m+=1)&(set b=b)&(set k=k) if "!b!"=="!l!" (set /a m+=1)&(set b=b)&(set l=l) if "!c!"=="!i!" (set /a m+=1)&(set c=c)&(set i=i) if "!c!"=="!j!" (set /a m+=1)&(set c=c)&(set j=j) if "!c!"=="!l!" (set /a m+=1)&(set c=c)&(set l=l) if "!d!"=="!i!" (set /a m+=1)&(set d=d)&(set i=i) if "!d!"=="!j!" (set /a m+=1)&(set d=d)&(set j=j) if "!d!"=="!k!" (set /a m+=1)&(set d=d)&(set k=k) ) ) echo. echo. echo. echo.Final result statistics echo.%n%A%m%B pause |
|
| Floor4 huahua0919 | Posted 2010-11-13 13:08 |
| 银牌会员 Posts 780 Credits 1,608 | |
|
The answer upstairs is incorrect. If A:1 1 2 2 and B:2 2 1 1, the answer should be 0A4B, but yours is 0A2B
|
|
| Floor5 523066680 | Posted 2010-11-13 22:02 |
| 银牌会员 Posts 1,133 Credits 2,361 | |
|
I saw it and said I didn't understand.
Is it like this: 1 1 2 2 2 2 1 1 0A4B First, compare vertically, there are 4 columns, all different, so get 0A Then compare between the two lines, and 4 groups of the same can be matched (non - repeated matching). So 0A4B 1 2 3 4 4 3 2 1 0A4B This is this situation, and then the following is different 1 1 1 1 1 1 1 1 4A0B Why is it 0B? Oh, I get it. That is, A is compared by columns. The remaining numbers can be judged interlaced. For 1111, this is all swallowed by A |
|
| Floor6 huahua0919 | Posted 2010-11-13 22:15 |
| 银牌会员 Posts 780 Credits 1,608 | |
|
Yes, that's it.
|
|
| Floor7 523066680 | Posted 2010-11-13 23:17 |
| 银牌会员 Posts 1,133 Credits 2,361 | |
|
Fat and there is no part where the user input is written
[ Last edited by 523066680 on 2010-11-13 at 23:54 ] |
|
| Floor8 523066680 | Posted 2010-11-13 23:26 |
| 银牌会员 Posts 1,133 Credits 2,361 | |
|
I still won't mess around...
Why in the example 1 3 3 3 3 1 1 1 0A2B =========== Can't be divided into 11 11 33 33, right? When removing, is it that all the same are removed? [ Last edited by 523066680 on 2010-11-13 at 23:27 ] |
|
| Floor9 huahua0919 | Posted 2010-11-13 23:47 |
| 银牌会员 Posts 780 Credits 1,608 | |
|
1 3 3 3
3 1 1 1 It's 0A2B, and the final remaining ones are 3 3 1 1 |
|
| Floor10 523066680 | Posted 2010-11-13 23:56 |
| 银牌会员 Posts 1,133 Credits 2,361 | |
|
It should be okay
1 - 3 3 - 1 3 - 1 3 - 1 0A2B |
|
| Floor11 huahua0919 | Posted 2010-11-14 00:05 |
| 银牌会员 Posts 780 Credits 1,608 | |
|
It seems that it doesn't work if it's changed to 1122 or 2211
|
|
| Floor12 523066680 | Posted 2010-11-14 12:08 |
| 银牌会员 Posts 1,133 Credits 2,361 | |
|
1113 3331 only gets 2B 2211 1122 can get 4B I really don't understand
===================================== If I make another mistake, I won't do it anymore, the down-to-earth route: [ Last edited by 523066680 on 2010-11-14 at 14:31 ] |
|
| Floor13 jarry0932 | Posted 2010-11-30 21:56 |
| 初级用户 Posts 122 Credits 128 | |
|
It seems that the number guessing game I played when I was young was like this...
|
|
| Floor14 Hanyeguxing | Posted 2010-11-30 23:38 |
| 银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂 | |
|
First, define the variable groups:
for %%a in (%Han1%) do set/a Ye1+=1&set #!Ye1!=%%a for %%a in (%Han2%) do set/a Ye2+=1&set @!Ye2!=%%a The acquisition of value A: According to the requirement, just compare the nth one of the two groups of characters in sequence. for /l %%a in (1,1,%Ye1%) do if "!#%%a!"=="!@%%a!" set/a Xing1+=1 Because the numbers that are the same are no longer used, so I first define a variable to combine each number of each group into a sequence: set "Gu1=!Gu1! !Ye2! ". Before comparison, the sequence numbers of the two groups of numbers should be the same, for example: 1 2 3 4 5 6 7... Each time a match occurs, this sequence number is also deleted at the same time: set Gu1=!Gu1:%%a =!. Because it is a comparison of corresponding columns, the remaining sequence numbers of the two groups of numbers always remain synchronously the same. The acquisition of value B: Then, after nested for, combine and compare the two groups of numbers. There should be no matches in the corresponding columns. Because the sequence numbers have been deleted in the previous step, so here the for loop is for the sequence numbers of the two groups. When a match occurs, delete this sequence number: if "!#%%a!"=="!@%%b!" set Gu1=!Gu1:%%b =!&set/a Xing2+=Gu2 Because the change of this Gu1 is inside the second for, so it cannot be synchronized with Gu1 in for %%b in (!Gu1!) do immediately. There are two methods to solve this problem: 1. Use goto to jump out of this for, so remove the outer for and use goto loop 2. Do not jump out of the for, but need to make the counting run empty after a match occurs. Here, set/a Xing2+=Gu2,Gu2=0 is used. Because Xing2+=Gu2 is first, Gu2=0 is later. The first time, it is added by 1, and later it still continues to loop, but each time it adds 0. Requirement: The number of pairs of the two groups of numbers is the same, each number has any number of digits, and is separated by spaces. Method 1: [ Last edited by Hanyeguxing on 2010-12-2 at 00:39 ] |
|
| Floor15 Hanyeguxing | Posted 2010-12-01 10:14 |
| 银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂 | |
|
Method 2:
```batch @echo off&setlocal enabledelayedexpansion for %%a in (1 2) do set Xing%%a=0&set/p Han%%a=输入第 %%a 行字符: for %%a in (%Han1%) do set/a Ye1+=1&set #!Ye1!=%%a for %%a in (%Han2%) do set/a Ye2+=1&set @!Ye2!=%%a&set "Gu1=!Gu1! !Ye2! " for /l %%a in (1,1,%Ye1%) do if "!#%%a!"=="!@%%a!" set/a Xing1+=1&set Gu1=!Gu1:%%a =! for %%a in (%Gu1%) do ( set Gu2=1 for %%b in (!Gu1!) do if "!#%%a!"=="!@%%b!" set Gu1=!Gu1:%%b =!&set/a Xing2+=Gu2,Gu2=0 ) echo %Xing1%A%Xing2%B&pause&exit ``` [ Last edited by Hanyeguxing on 2010-12-2 at 00:40 ] |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |