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-28 06:46
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Symmetric output bonus question (challenging) View 4,186 Replies 64
Floor 16 Posted 2008-07-11 01:46 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Wow. . .
ps: Brother's code is not universal. What if both sides are letters or Chinese characters?

[ Last edited by zw19750516 on 2008-7-11 at 01:49 AM ]
批处理之家新域名:www.bathome.net
Floor 17 Posted 2008-07-11 01:52 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
Originally posted by zw19750516 at 2008-7-11 01:46 AM:
Dizzy...
ps: Brother's code is not universal. What if both sides are letters or Chinese characters?

[ Last edited by zw19750516 on 2008-7-11 at 01:49 AM ]

Please give more advice!~
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 18 Posted 2008-07-11 01:55 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Tips: intercept characters from both sides at the same time with the middle as the boundary
批处理之家新域名:www.bathome.net
Floor 19 Posted 2008-07-11 01:57 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Originally posted by zw19750516 at 2008-7-11 01:46:
Dizzy...
ps: Brother's code is not universal. What if both sides are letters or Chinese characters?

[ Last edited by zw19750516 on 2008-7-11 at 01:49 AM ]

Both sides are letters?
This can only be blamed on your carelessness in setting the question.
致精致简!
Floor 20 Posted 2008-07-11 02:01 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
It really is that I thought too, too complicated... I wanted to give up halfway through this problem, FAITH... Now if I do it again, I'm afraid I'll really give up. Such a problem makes my spine hurt when I think about it~
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 21 Posted 2008-07-11 02:02 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Originally posted by 26933062 at 2008-7-11 01:57:

Both sides are letters?
This can only blame you for not being careful in setting the question.

I just explained that the code should improve generality, and there's no other meaning. For this question, he has already solved it, and I don't deny that.
批处理之家新域名:www.bathome.net
Floor 22 Posted 2008-07-11 02:04 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Originally posted by pusofalse at 2008-7-11 02:01:
It's really that I thought too, too complicated... I wanted to give up halfway through this problem, FAITH...
Now if I do it again, I'm afraid I will really give up. Such a problem makes my spine hurt thinking about it~

Victory often follows right after the moment when you feel you can't hold on any longer.
ps: Brothers, I'm going to sleep first. I have to go to work tomorrow. Looking forward to someone writing efficient and general-purpose code (mine won't be posted first)...

[ Last edited by zw19750516 on 2008-7-11 at 02:08 AM ]
批处理之家新域名:www.bathome.net
Floor 23 Posted 2008-07-11 02:46 ·  中国 江苏 常州 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
I think it's not that long. Sign in
简单!简单!再简单!
Floor 24 Posted 2008-07-11 10:02 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
But the code for solving the problem must not be short. I think it should not be solvable within 10 lines.
批处理之家新域名:www.bathome.net
Floor 25 Posted 2008-07-11 11:49 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
It seems that this problem is indeed a bit difficult. I'll first公布 my thought - out relatively complex method:

@echo off&setlocal enabledelayedexpansion
set str=0123456789abcdefghij
:lp
set a=%str:~,1%&set /a n+=1
set .%n%=%a%&set _%n%=!str:~-1,1!&set str=%str:~1,-1%
if defined str goto lp
set /p code=please enter the code:
:loop
set a=%code:~,1%&set var=%var% !a!&set code=%code:~1%
if defined code goto loop
for /l %%a in (1,1,%n%) do (
for %%i in (%var%) do (
if "!.%%a!" equ "%%i" set flag=a
if "!_%%a!" equ "%%i" set flag=a
)
if defined flag (
set str1=!str1!!.%%a!&set str2=!_%%a!!str2!&set "flag="
) else (
set str1=!str1!#&set str2=#!str2!
))
cls&echo %str1% ^| %str2%&pause>nul
批处理之家新域名:www.bathome.net
Floor 26 Posted 2008-07-11 12:11 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
It's really confusing. It took me quite a while...
There are three methods thought out in total. The first one is not universal, and the latter two are relatively universal.
Generally speaking, I feel that the idea is not as clear as zw19750516's... I'm confused. Got bamboozled...
Code One:
The left side must be a number. (Not universal, only for the topic in the first post)

@echo off&setlocal enabledelayedexpansion
echo.&set /p num= Please enter &cls
set "str=a b c d e f g h i j"
set "var1=##########"
set /a n=10
for %%a in (%str%) do (
set /a n-=1&set .%%a=!n!&set .!n!=!n!&set "_!n!=%%a"
)
for /l %%a in (0 1 9) do (
if not "!num:~%%a,1!"=="" set num2=!num2! !num:~%%a,1!
)
for %%a in (!num2!) do (
set /a n=!.%%a!+1
call set "var1=%%var1:~0,!.%%a!%%!.%%a!%%var1:~!n!%%"
)
for /l %%a in (0 1 9) do (
if "!var1:~%%a,1!"=="#" (set var2=#!var2!) else set "var2=!_%%a!!var2!"
)
echo.&echo !var1! !var2!
pause>nul



Code Two:
More universal, not limited to numbers, both left and right sides can be any characters but no duplicates.

@echo off&setlocal enabledelayedexpansion
set "str1=abcdefghij"&set "str2=tsrqponmlk"
for /l %%a in (0 1 9) do (
set ".!str1:~%%a,1!=!str1:~%%a,1!"
set ".!str2:~%%a,1!=!str1:~%%a,1!"
set "_!str1:~%%a,1!=#"
set "_!str2:~%%a,1!=#"
)
echo !str1! klmnopqrst
echo.&set /p num= Please enter &cls&echo !num!
echo.&echo !str1! klmnopqrst
set num=!num: =!
for /l %%a in (0 1 9)do if not "!num:~%%a,1!"=="" set num2=!num2! !num:~%%a,1!
for %%a in (!num2!) do set _%%a=!.%%a!&set "_!.%%a!=!.%%a!"
for /l %%a in (0 1 9) do call set "var1=!var1! %%_!str1:~%%a,1!%%"
set /a nnn=-1
for %%a in (!var1!) do (
set /a nnn+=1
if "%%a"=="#" (set var2=#!var2!) else call set "var2=%%str2:~!nnn!,1%%!var2!"
)
echo.&echo !var1: =! !var2!
pause>nul



Code Three:

@echo off&setlocal enabledelayedexpansion
set "str1=abcdefghij"&set "str2=tsrqponmlk"
set var1=##########&set var2=!var1!
echo !str1! klmnopqrst
echo.&set /p num= Please enter &cls&echo You entered is !num!
echo.&echo !str1! klmnopqrst
set num=!num: =!
for /l %%a in (0 1 9)do if not "!num:~%%a,1!"=="" set num2=!num2! !num:~%%a,1!
for /l %%a in (0 1 9) do (
set /a mmm=%%a+1
set ".!str1:~%%a,1!=!mmm!"
set ".!str2:~%%a,1!=!mmm!"
)
for %%a in (!num2!) do (
set /a hhh=!.%%a!-1
call set var1=%%var1:~0,!hhh!%%%%str1:~!hhh!,1%%%%var1:~!.%%a!%%
call set var2=%%var2:~0,!hhh!%%%%str2:~!hhh!,1%%%%var2:~!.%%a!%%
)
for /l %%a in (0 1 9) do set var3=!var2:~%%a,1!!var3!
echo.&echo !var1! !var3!
pause>nul
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
bat-zw +15 2008-07-11 12:24
pusofalse +15 2008-07-11 12:51
致精致简!
Floor 27 Posted 2008-07-11 12:15 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
To be honest, I'm confused by your code... The expert actually came up with three methods.
The idea highlights that this lies in the clever use of variable assignment.
The flaw I think is still in the complexity of the code.

[ Last edited by zw19750516 on 2008-7-11 at 01:02 PM ]
批处理之家新域名:www.bathome.net
Floor 28 Posted 2008-07-11 12:21 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Here is another code I used a bit cleverly (when inputting, it must be in the form like 1 a f 4):


@echo off&setlocal enabledelayedexpansion
set str=0j 1i 2h 3g 4f 5e 6d 7c 8b 9a
set /p code=Please enter characters, separated by spaces in between:
for %%a in (%str%) do (set a=%%a
for %%i in (%code%) do (
if "%%i" equ "!a:~,1!" set flag=a
if "%%i" equ "!a:~1!" set flag=a
)
if defined flag (
set str1=!str1!!a:~,1!&set str2=!a:~1!!str2!&set "flag="
) else (
set str1=!str1!#&set str2=#!str2!
))
cls&echo %str1% ^| %str2%&pause>nul
批处理之家新域名:www.bathome.net
Floor 29 Posted 2008-07-11 13:09 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
After seeing the codes of seniors, I feel extremely ashamed...
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 30 Posted 2008-07-11 15:18 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Another one, with a clear train of thought.

@echo off&setlocal enabledelayedexpansion
set "str1= 0 1 2 3 4 5 6 7 8 9"
set "str2= j i h g f e d c b a"
echo.&set /p num= Please enter &cls&set num=!num: =!
for %%a in (!str1! !str2!) do set num=!num:%%a= %%a!
for %%a in (%str1%) do set /a wei+=1,weis=wei*2

for /l %%a in (1 2 !weis!) do (
set ".!str1:~%%a,1!=!str2:~%%a,1!"
set ".!str2:~%%a,1!=!str1:~%%a,1!"
)
for %%a in (!num!)do set _%%a=a&set _!.%%a!=a
for %%a in (!str1!)do if defined _%%a (set var1=!var1!%%a) else set "var1=!var1!#"
for %%a in (!str2!)do if defined _%%a (set var2=%%a!var2!) else set "var2=#!var2!"
echo.&echo !var1! !var2!
pause>nul



[ Last edited by 26933062 on 2008-7-11 at 04:14 PM ]
致精致简!
Forum Jump: