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-07-08 04:00
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Set out a question, all picked from C language questions by me View 2,606 Replies 38
Floor 16 Posted 2008-04-11 21:10 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Narc narcissistic number

It is suggested to use three nested loops...
Floor 17 Posted 2008-04-11 21:42 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Because cmd is limited to 10-digit numbers, using this method can only accurately calculate the height of the ball bouncing up on the 10th landing, and the distance traveled in 10 passes cannot be accurately calculated (depressed):
@echo off
set /a n=100*10000000&set m=0
:begin
if %m% gtr 0 set str=%str%+2*%n%
if %m% gtr 0 set /a str=%str%/10000000
set /a n/=2&set /a m+=1
if %m% lss 10 goto begin
set /a str=%str%+100/2
echo The total distance after the 10th landing of the ball is %str% meters
:lp
if "%n:~,-6%"=="" set n=0%n%
if "%n:~,-6%"=="" goto lp
echo The 10th bounce is 0.%n% meters
pause>nul


[ Last edited by zw19750516 on 2008-4-11 at 10:05 PM ]
批处理之家新域名:www.bathome.net
Floor 18 Posted 2008-04-11 22:26 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
```@echo off&setlocal enabledelayedexpansion
set n=10
for %%i in (2,2,2,2) do set /a n+=%%i&cls&echo The fifth person is!n! years old
pause>nul
```
批处理之家新域名:www.bathome.net
Floor 19 Posted 2008-04-11 22:34 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
The Narcissistic number algorithm is indeed better with three nested loops. The following one was forgotten who the moderator wrote, but I remember it was written by moderator namejm:

@echo off
echo.
echo Three-digit narcissistic numbers are:
echo.
setlocal enabledelayedexpansion
for /l %%i in (1,1,9) do (
for /l %%j in (0,1,9) do (
for /l %%k in (0,1,9) do (
set a=%%i
set b=%%j
set c=%%k
set num1=!a!!b!!c!
set /a num2=!a!*!a!*!a!+!b!*!b!*!b!+!c!*!c!*!c!
if !num1! equ !num2! echo !num1!
)
)
)
pause
goto :eof
简单!简单!再简单!
Floor 20 Posted 2008-04-11 22:35 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
@echo off
set /a str=6*7*7*7*7*7*4
echo There can be %str% odd numbers formed
pause>nul
批处理之家新域名:www.bathome.net
Floor 21 Posted 2008-04-11 22:41 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Originally posted by terse at 2008-4-11 22:34:
The narcissistic number algorithm is indeed better with three nested loops. The following one I forgot who the moderator wrote, I remember it was written by moderator namejm:

@echo off
echo.
echo Three-digit narcissistic numbers are:
echo.
setlocal ...

I don't think so. My code can implement it very well (I initially missed a *), why write such complicated code.
批处理之家新域名:www.bathome.net
Floor 22 Posted 2008-04-11 23:47 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
In terms of efficiency, a 3-layer loop is better than looping from 111 to 999.

For program code, efficiency is the top priority.
Floor 23 Posted 2008-04-11 23:54 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
If it's simple, then directly SET/a var
var=!str:~,1!!str:~,1!*!str:~,1!+!str:~1,1!*!str:~1,1!*!str:~1,1!.....
简单!简单!再简单!
Floor 24 Posted 2008-04-12 00:16 ·  中国 湖南 长沙 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Personally, there is no difference in efficiency between floor 15 and floor 19.
However, it is unnecessary for floor 15 to put the delay variable inside the for.
If you want to prove which one has higher efficiency, it is best to rely on facts.
To find the narcissistic numbers from 1 to 10000 and record the batch processing running time, isn't that a result?
致精致简!
Floor 25 Posted 2008-04-12 00:43 ·  中国 陕西 西安 电信
银牌会员
★★★★
钻石会员
Credits 2,278
Posts 1,020
Joined 2007-11-19 13:34
18-year member
UID 103127
Gender Male
Status Offline
The so-called "narcissistic number" refers to a three-digit number whose sum of the cubes of its digits is equal to the number itself.

15th floor:
@echo off
setlocal enabledelayedexpansion
for /l %%i in (100,1,999) do (

set str=%%i
set/a a=!str:~,1!, b=!str:~1,1!, c=!str:~2,1!
set /a var=!a!*!a!*!a!+!b!*!b!*!b!+!c!*!c!*!c!
if !var! equ !str! echo !str!

)
pause



19th floor:
@echo off
setlocal enabledelayedexpansion
for /l %%i in (1,1,9) do for /l %%j in (0,1,9) do for /l %%k in (0,1,9) do (

set/a a=%%i,b=%%j,c=%%k
set num1=!a!!b!!c!
set /a num2=!a!*!a!*!a!+!b!*!b!*!b!+!c!*!c!*!c!
if !num1! equ !num2! echo !num1!

)
pause


[ Last edited by plp626 on 2008-4-12 at 12:51 AM ]
山外有山,人外有人;低调做人,努力做事。

进入网盘(各种工具)~~ 空间~~cmd学习
Floor 26 Posted 2008-04-12 07:12 ·  中国 四川 成都 双流区 电信
新手上路
Credits 18
Posts 8
Joined 2008-04-04 10:40
18-year member
UID 114923
Gender Male
Status Offline
Floor 27 Posted 2008-04-12 14:45 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Are the number of loops the same?
Which one is less?
Which is faster, taking each digit or combining several digits?
Floor 28 Posted 2008-04-12 15:00 ·  中国 湖南 长沙 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Originally posted by slore at 2008-4-12 14:45:
Are the number of loops the same?
Which one is less?
Which is faster, taking each digit or combining several digits?

To find the Armstrong numbers from 1 to 10000 and record the batch processing time, wouldn't that give the result?
致精致简!
Floor 29 Posted 2008-04-12 16:09 ·  中国 陕西 西安 电信
银牌会员
★★★★
钻石会员
Credits 2,278
Posts 1,020
Joined 2007-11-19 13:34
18-year member
UID 103127
Gender Male
Status Offline
Originally posted by 26933062 at 2008-4-12 03:00 PM:

Find the narcissistic numbers from 1 to 10000, and record the batch processing running time, then you will get the result?




The two times are the same, with an error within 0.01 seconds, which is less than the "system" error of %time%.
山外有山,人外有人;低调做人,努力做事。

进入网盘(各种工具)~~ 空间~~cmd学习
Floor 30 Posted 2008-04-12 16:31 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
If you can test up to 99999999999999999999999999999999999...
Forum Jump: