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-29 14:00
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Numerical comparison View 3,187 Replies 19
Original Poster Posted 2010-11-11 16:15 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,608
Posts 780
Joined 2007-10-07 10:19
18-year member
UID 99089
Gender Male
Status Offline
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
Floor 2 Posted 2010-11-13 12:15 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,608
Posts 780
Joined 2007-10-07 10:19
18-year member
UID 99089
Gender Male
Status Offline
Why is there no one writing it? If it were before, a group of people would stack code. I would post my garbage code.

@echo off&setlocal enabledelayedexpansion&color 3e

set /p aa=Enter a line of characters:
set /p bb=Enter a line of characters:
set n=0
set m=0


:loopa
set i=0&set j=0&set k=0&set l=0&set ii=0
for %%i in (!aa!) do (
set /a i+=1
set /a k=ii+2
for %%h in (!bb!) do (
set /a j+=1
if "!i!"=="!j!" (if "%%i"=="%%h" (
call,set aa=%%aa:~,!ii!%%%%aa:~!k!%%
call,set bb=%%bb:~,!ii!%%%%bb:~!k!%%
set /a n+=1
goto :loopa
)
)
)
set /a ii+=2
set j=0
)
echo.
echo.The value after removing type A:%aa%============%bb%
echo.
echo.
echo.
echo.The process of changing the value of type B is as follows:
:loopb
set i=0&set j=0&set k=0&set l=0
for %%i in (!aa!) do (
set /a k=i+2
for %%h in (!bb!) do (
set /a l=j+2
if /i "%%i"=="%%h" (
call,set aa=%%aa:~,!i!%%%%aa:~!k!%%
call,set bb=%%bb:~,!j!%%%%bb:~!l!%%
set /a m+=1
echo.The result of the !m!th time !aa!==================!bb!
goto:loopb
)
set /a j+=2
)
set /a i+=2
set /a j=0
)
echo.
echo.
echo.
echo.Final result statistics
echo.%n%A%m%B
pause
Floor 3 Posted 2010-11-13 12:59 ·  中国 江苏 苏州 吴江区 电信
新手上路
Credits 13
Posts 15
Joined 2010-09-18 21:51
15-year member
UID 174432
Gender Male
Status Offline
@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
Floor 4 Posted 2010-11-13 13:08 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,608
Posts 780
Joined 2007-10-07 10:19
18-year member
UID 99089
Gender Male
Status Offline
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
Floor 5 Posted 2010-11-13 22:02 ·  中国 广东 广州 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
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
Floor 6 Posted 2010-11-13 22:15 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,608
Posts 780
Joined 2007-10-07 10:19
18-year member
UID 99089
Gender Male
Status Offline
Yes, that's it.
Floor 7 Posted 2010-11-13 23:17 ·  中国 广东 广州 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
Fat and there is no part where the user input is written

@echo off
setlocal enabledelayedexpansion

set numa=1134
set numb=3364
set /a sameA=0,sameB=0
for /l %%a in (0,1,3) do (
echo !numa:~%%a,1! - !numb:~%%a,1!
if "!numa:~%%a,1!"=="!numb:~%%a,1!" (
set /a sameA+=1,cut=%%a+1
for %%n in (!cut!) do (
set numa=!numa:~0,%%a!x!numa:~%%n!
set numb=!numb:~0,%%a!x!numb:~%%n!
)
)
)
set /a num_n=8-sameA*2-1,x=0
set next=%numa:x=%%numb:x=%

:lp
set /a xb=x+1
for /l %%a in (%xb%,1,%num_n%) do (
if "!next:~%%a,1!"=="!next:~%x%,1!" (
set /a sameB+=1
set next=!next:~0,%x%!x!next:~%xb%!
set tmpstr=!next:~%%a!
set next=!next:~0,%%a!x!tmpstr:~1!
goto :break
)
)
:break
if %x% lss %num_n% (set /a x+=1 &goto :lp)
echo %sameA%A%sameB%B
pause



1 - 3
1 - 3
3 - 6
4 - 4
1A2B


[ Last edited by 523066680 on 2010-11-13 at 23:54 ]
Floor 8 Posted 2010-11-13 23:26 ·  中国 广东 广州 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
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 ]
Floor 9 Posted 2010-11-13 23:47 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,608
Posts 780
Joined 2007-10-07 10:19
18-year member
UID 99089
Gender Male
Status Offline
1 3 3 3
3 1 1 1
It's 0A2B, and the final remaining ones are
3 3
1 1
Floor 10 Posted 2010-11-13 23:56 ·  中国 广东 广州 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
It should be okay

@echo off
setlocal enabledelayedexpansion

set numa=1333
set numb=3111
set /a sameA=0,sameB=0
for /l %%a in (0,1,3) do (
echo !numa:~%%a,1! - !numb:~%%a,1!
if "!numa:~%%a,1!"=="!numb:~%%a,1!" (
set /a sameA+=1,cut=%%a+1
for %%n in (!cut!) do (
set numa=!numa:~0,%%a!x!numa:~%%n!
set numb=!numb:~0,%%a!x!numb:~%%n!
)
)
)
set /a num_n=8-sameA*2,x=0
set next=%numa:x=%%numb:x=%AAA

for /l %%a in (0,1,9) do (
set tmpstr=!next:%%a=!
set tmpstr=!tmpstr:~0,%num_n%!
if not "!tmpstr:AA=!"=="!tmpstr!" (set /a sameB+=1)
)

echo %sameA%A%sameB%B
pause


1 - 3
3 - 1
3 - 1
3 - 1
0A2B
Floor 11 Posted 2010-11-14 00:05 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,608
Posts 780
Joined 2007-10-07 10:19
18-year member
UID 99089
Gender Male
Status Offline
It seems that it doesn't work if it's changed to 1122 or 2211
Floor 12 Posted 2010-11-14 12:08 ·  中国 天津 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
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:

@echo off
setlocal enabledelayedexpansion

set numa=1122
set numb=2211
set /a sameA=0,sameB=0
for /l %%a in (0,1,3) do (
echo !numa:~%%a,1! - !numb:~%%a,1!
if "!numa:~%%a,1!"=="!numb:~%%a,1!" (
set /a sameA+=1,cut=%%a+1
for %%n in (!cut!) do (
set numa=!numa:~0,%%a!x!numa:~%%n!
set numb=!numb:~0,%%a!x!numb:~%%n!
)
)
)
set /a num_n=4-sameA-1,x=0
set numa=%numa:x=%
set numb=%numb:x=%

:lp
for /l %%a in (0,1,%num_n%) do (
if "!numa:~%x%,1!"=="!numb:~%%a,1!" (
set /a sameB+=1
set tmpstr=!numb:~%%a!
set numb=!numb:~0,%%a!x!tmpstr:~1!
goto :break
)
)
:break
if %x% lss %num_n% (set /a x+=1 &goto :lp)
echo %sameA%A%sameB%B
pause


[ Last edited by 523066680 on 2010-11-14 at 14:31 ]
Floor 13 Posted 2010-11-30 21:56 ·  中国 广东 东莞 联通
初级用户
★★
Credits 128
Posts 122
Joined 2009-09-21 06:24
16-year member
UID 152378
Gender Male
Status Offline
It seems that the number guessing game I played when I was young was like this...
Floor 14 Posted 2010-11-30 23:38 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
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:
@echo off&setlocal enabledelayedexpansion
for %%a in (1 2) do set Xing%%a=0&set/p Han%%a=Enter the characters of line %%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 =!
set Gu2=%Gu1%&call:Gu %Gu1%
echo %Xing1%A%Xing2%B&pause&exit
:Gu
for %%a in (%Gu2%) do if "!#%1!"=="!@%%a!" set/a Xing2+=1&set Gu2=!Gu2:%%a =!&goto:Ye
:Ye
shift
if not "%1"=="" goto:Gu


[ Last edited by Hanyeguxing on 2010-12-2 at 00:39 ]
Floor 15 Posted 2010-12-01 10:14 ·  中国 吉林 延边朝鲜族自治州 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
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 ]
Forum Jump: