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 06:28
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Set out a question, all picked from C language questions by me View 2,611 Replies 38
Original Poster Posted 2008-04-10 19:35 ·  中国 广东 东莞 电信
初级用户
Credits 54
Posts 19
Joined 2008-02-24 13:37
18-year member
UID 111315
Gender Male
Status Offline
I, from the "C Language Data Compilation," the questions are for novice learning and expert practice. Hehe. I also don't have time to do it, leaving it for everyone to do.
=================================================================
----------QQ:327828994

【Program 1】
Title: There are 4 numbers 1, 2, 3, 4. How many three-digit numbers with distinct and non-repeating digits can be formed? And what are they?
1. Program analysis: The digits that can be filled in the hundreds, tens, and units places are all 1, 2, 3, 4. Form all permutations and then remove the ones that don't meet the conditions.

【Program 4】
Title: Input a certain year, month, and day, and judge which day of the year it is?
1. Program analysis: Take March 5th as an example. You should first add up the first two months, then add 5 days to get the day of the year. In special cases, if it's a leap year and the input month is greater than 3, an additional day needs to be considered.

【Program 8】
Title: Output the 9*9 multiplication table.
1. Program analysis: Consider by rows and columns. There are 9 rows and 9 columns. i controls the row and j controls the column.
【Program 9】
Title: Require to output a chessboard.
1. Program analysis: Use i to control the row and j to control the column. According to the change of the sum of i + j, control whether to output a black square or a white square.

【Program 11】
Title: Classical problem: There is a pair of rabbits. Starting from the 3rd month after birth, it gives birth to a pair of rabbits every month. After the little rabbits grow to the 3rd month, they give birth to a pair of rabbits every month. If the rabbits don't die, ask what is the total number of rabbits each month?
1. Program analysis: The law of rabbits is the sequence 1, 1, 2, 3, 5, 8, 13, 21....
【Program 12】
Title: Judge how many prime numbers are between 101 and 200, and output all prime numbers.
1. Program analysis: The method to judge a prime number: Use a number to divide by 2 to sqrt(this number) respectively. If it can be divided evenly, it means this number is not a prime number, otherwise it is a prime number.
【Program 13】
Title: Print out all "Narcissistic numbers". 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. For example: 153 is a "Narcissistic number" because 153 = 1³ + 5³ + 3³.
1. Program analysis: Use a for loop to control numbers from 100 to 999. Decompose each number into the units digit, tens digit, and hundreds digit.
【Program 14】
Title: Decompose a positive integer into prime factors. For example: input 90, print out 90 = 2*3*3*5.
Program analysis: Decompose n into prime factors. First find the smallest prime number k, then complete the following steps:
(1) If this prime number is exactly equal to n, it means the process of decomposing prime factors has ended, and just print it out.
(2) If n<>k, but n can be divided evenly by k, then print out the value of k, and use the quotient of n divided by k as the new positive integer n, and repeat step 1.
(3) If n cannot be divided evenly by k, then use k+1 as the value of k, and repeat step 1.
【Program 16】
Title: Input two positive integers m and n, and find their greatest common divisor and least common multiple.
1. Program analysis: Use the Euclidean algorithm.
: : Author's prompt: The data does not explain what the Euclidean algorithm is. According to the C source code, the Euclidean algorithm (if a>b, let number a take the modulus of number b to get c, then let number b take the modulus of number c to get d, then let c take the modulus of d, and keep looping until the final modulus remainder n=0)

【Program 19】
Title: A number is called a "perfect number" if it is exactly equal to the sum of its factors. For example, 6 = 1 + 2 + 3. Program to find all perfect numbers within 1000.
【Program 20】
Title: A ball is freely dropped from a height of 100 meters. Each time it lands, it rebounds to half of the original height; then falls again. Find out how many meters it has passed in total when it lands for the 10th time? How high does it rebound for the 10th time?
【Program 21】
Title: Monkey peach-eating problem: The monkey picked several peaches on the first day, ate half of them immediately, and was not addicted, so ate one more. The next morning, it ate half of the remaining peaches again, and ate one more. In the following mornings, it ate half of the remaining peaches from the previous day plus one. On the morning of the 10th day, when it wanted to eat again, it saw that there was only one peach left. Find out how many peaches were picked on the first day.
1. Program analysis: Adopt the reverse thinking method and infer from the back to the front.
【Program 23】
Title: Print out the following pattern (diamond)
*
***
******
********
******
***
*
1. Program analysis: First treat the figure as two parts. The first four rows follow one pattern, and the last three rows follow another pattern. Use a double for loop. The first layer controls the row and the second layer controls the column.

【Program 24】
Title: There is a fraction sequence: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13... Find the sum of the first 20 terms of this sequence.

1. Program analysis: Please grasp the changing law of the numerator and the denominator.

【Program 25】
Title: Find the sum of 1 + 2! + 3! +... + 20!
1. Program analysis: This program just changes accumulation to cumulative multiplication.
【Program 28】
Title: There are 5 people sitting together. Ask how old the fifth person is? He said he is 2 years older than the fourth person. Ask how old the fourth person is, he said he is 2 years older than the third person. Ask the third person, he said he is 2 years older than the second person. Ask the second person, he said he is 2 years older than the first person. Finally ask the first person, he said he is 10 years old. Ask how old the fifth person is?
1. Program analysis: Use the recursive method. Recursion is divided into the backtracking and recursion stages. To know the age of the fifth person, need to know the age of the fourth person, and so on, push back to the first person (10 years old), then backtrack.
【Program 30】
Title: A 5-digit number, judge whether it is a palindrome number. That is, 12321 is a palindrome number, the units digit is the same as the ten-thousands digit, and the tens digit is the same as the thousands digit.
【Program 37】
Title: Sort 10 numbers
1. Program analysis: You can use the selection method. That is, in the comparison process of the last 9, select the smallest one to exchange with the first element, and so on, that is, use the second element to compare with the last 8 and exchange.
【Program 61】
Title: Print out Pascal's triangle (要求 print out 10 rows as shown below)   
1. Program analysis:
       1
      1  1
      1  2  1
      1  3  3  1
      1  4  6  4  1
      1  5  10 10 5  1 
【Program 69】
Title: There are n people standing in a circle, numbered in order. Starting from the first person to report numbers (report numbers from 1 to 3), those who report 3 exit the circle. Ask who is the original number of the last remaining one.
【Program 80】
Title: There is a pile of peaches on the beach, and five monkeys come to divide them. The first monkey divides this pile of peaches into five equal parts, there is one more, this monkey throws the extra one into the sea and takes one part. The second monkey divides the remaining peaches into five equal parts again, there is one more, it also throws the extra one into the sea and takes one part. The third, fourth, and fifth monkeys all do this. Ask what is the minimum number of peaches originally on the beach?
【Program 81】
Title: 809*??=800*??+9*??+1 where?? represents a two-digit number, the result of 8*?? is a two-digit number, and the result of 9*?? is a three-digit number. Find the two-digit number represented by??, and the result after 809*??.
【Program 83】
Title: Find the number of odd numbers that can be composed of 0 - 7.
【Program 84】
Title: An even number can always be expressed as the sum of two prime numbers.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
bat-zw +4 2008-04-10 20:16
Floor 2 Posted 2008-04-10 19:58 ·  中国 北京 华为云
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
Floor 3 Posted 2008-04-10 20:02 ·  中国 广东 东莞 电信
初级用户
Credits 54
Posts 19
Joined 2008-02-24 13:37
18-year member
UID 111315
Gender Male
Status Offline
Floor 4 Posted 2008-04-10 20:15 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
```@echo off
for /l %%i in (1,1,4) do set a=%%i&call :lp %%a%%
pause&goto :eof
:lp
for /l %%j in (1,1,4) do if not %%j equ %a% set b=%%j&call :loop %%a%% %%b%%
goto :eof
:loop
for /l %%k in (1,1,4) do (
if not %%k equ %a% set d=%%k
setlocal enabledelayedexpansion
if not !d! equ %b% set c=!d!
)
echo %a%%b%%c%
endlocal
goto :eof
```

[ Last edited by zw19750516 on 2008-4-11 at 04:02 PM ]
批处理之家新域名:www.bathome.net
Floor 5 Posted 2008-04-10 20:25 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
```batch
@echo off
for /l %%i in (1,1,9) do (
setlocal enabledelayedexpansion
for /l %%j in (1,1,%%i) do (
set a=%%i&set b=%%j
set /a c=!a!*!b!&set /a m+=1
if !c! lss 10 set c= !c!
set "var=!a!*!b!=!c!"
set str=!str! !var!
if !m! equ !a! echo !str!&set m=0&set str=
)
endlocal
)
pause>nul
```

[ Last edited by zw19750516 on 2008-4-11 at 04:03 PM ]
批处理之家新域名:www.bathome.net
Floor 6 Posted 2008-04-10 20:45 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
```
@echo off
:begin
cls
set /p var=Enter the date to calculate (format: 2000-01-01):
if "%var:~4,1%%var:~7,1%"=="--" goto year
cls
echo Incorrect input format! Please return to correct input.&ping /n 2 127.1>nul&goto begin
:year
set year=%var:~,4%
set /a str=%year%-%year%/4*4
:month
if %var:~5,1% equ 0 (set month=%var:~6,1%) else (set month=%var:~5,2%)
set /a month=%month%-1
if %month% equ 0 set /a number=0
if %month% equ 1 set /a number=31
if %month% equ 2 set /a number=31+28
if %month% equ 3 set /a number=31*2+28
if %month% equ 4 set /a number=31*2+30+28
if %month% equ 5 set /a number=31*3+30+28
if %month% equ 6 set /a number=31*3+30*2+28
if %month% equ 7 set /a number=31*4+30*2+28
if %month% equ 8 set /a number=31*5+30*2+28
if %month% equ 9 set /a number=31*5+30*3+28
if %month% equ 10 set /a number=31*6+30*3+28
if %month% equ 11 set /a number=31*6+30*4+28
if %str% equ 0 if %month% gtr 2 set /a number=%number%+1
:day
if %var:~8,1% equ 0 (set /a number=%number%+%var:~9,1%) else (set /a number=%

number%+%var:~8,2%)
cls
echo %var% is the %number%th day of %year%
pause>nul
```

[ Last edited by zw19750516 on 2008-4-11 at 04:03 PM ]
批处理之家新域名:www.bathome.net
Floor 7 Posted 2008-04-10 20:49 ·  中国 甘肃 兰州 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Floor 8 Posted 2008-04-11 01:24 ·  中国 江苏 常州 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
### Determine the Number of Days
```
@echo off
set/p p1=Enter the year:
set/p p2=Enter the month:
set/p p3=Enter the day:
set/a pn="!(p1%%4) & !(!(p1%%100)) | !(p1%%400)"
for /f "tokens=%p2%" %%i in ("0 31 59 90 120 151 181 212 243 273 304 334") do set/a n=%%i+p3
if %P2% gtr 2 set/a n+=pn
if %P2% equ 2 if %P3% equ 29 if %pn% equ 0 echo %p1% is not a leap year, so there is no %P3% day in %P2% month&pause&exit
echo %p1% year %p2% month %p3% day is the %n%th day of %p1% year
pause
```

### Number Permutations
```
@echo off&setlocal enabledelayedexpansion
set str=1 2 3 4
for %%a in (!str!) do (
set str1=!str:%%a=!
for %%b in (!str1!) do (
set str2=!str1:%%b=!
for %%c in (!str2!) do (
set str3=!str2:%%c=!
echo %%b%%c!str3: =!
set /a n+=1
)
)
)
echo There are %n% permutations
pause
```

### Find Two-Digit Number
```
@echo off&setlocal enabledelayedexpansion
for /l %%i in (99,-1,10) do (
set/a m=%%i*8,n=%%i*9
if !m! lss 100 if !n! gtr 100 set/a mn=809*%%i&set str=%%i
)
echo 809 × %str% = %mn%
pause
```

### How Many Peaches Were Picked on the First Day
```
@echo off
set m=1
set n=10
:lp
set/a m+=1
set/a m=%m%*2,n-=1
if %n% gtr 1 goto lp
echo %m% peaches were picked on the first day
pause
```

### Sort Numbers
```
@echo off
set/p var=Please enter numbers
set var=%var: =%
:lp
set .%var:~0,1% %random%=a
set var=%var:~1%
if not "%var%" == "" goto :lp
set/p =Sorted as:<nul
for /f "delims=.= " %%i in ('set .') do set/p =%%i<nul
echo.
pause
```

### Increasing Sequence
This is what I misunderstood. Let's modify it
```
@echo off
for /l %%i in (1,1,20) do set/a n+=%%i
echo %n%
pause
```

[ Last edited by terse on 2008-4-11 at 09:32 AM ]
简单!简单!再简单!
Floor 9 Posted 2008-04-11 14:02 ·  中国 广东 东莞 电信
初级用户
Credits 54
Posts 19
Joined 2008-02-24 13:37
18-year member
UID 111315
Gender Male
Status Offline
If there are problems that can't be solved, I'll post the C source code, haha
Floor 10 Posted 2008-04-11 16:04 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
```@echo off
set /p month=Month:
if %month% gtr 0 if %month% leq 2 echo 2 only&pause&goto :eof
set /a month=%month%-2
set n=2&set m=2
:begin
set /a n=%n%+%m%
set /a w+=1
if %w% equ %month% echo %n% only&pause&goto :eof
set /a m=%m%+%n%
set /a v+=1
if %v% equ %month% echo %m% only&pause&goto :eof
goto begin
```
批处理之家新域名:www.bathome.net
Floor 11 Posted 2008-04-11 19:05 ·  中国 北京 鹏博士BGP
银牌会员
★★★
Credits 2,098
Posts 566
Joined 2007-09-11 07:27
18-year member
UID 97070
Gender Male
Status Offline
```
::Determine Prime Number
@echo off
for /l %%a in (101,1,200) do call:j %%a
exit/b

:j
setlocal enabledelayedexpansion
set/a half=%1/2
for /l %%a in (2,1,!half!) do set/a r=%1%%%%a&if !r! equ 0 goto:eof
set/p=%1 <nul
```
Floor 12 Posted 2008-04-11 19:45 ·  中国 北京 鹏博士BGP
银牌会员
★★★
Credits 2,098
Posts 566
Joined 2007-09-11 07:27
18-year member
UID 97070
Gender Male
Status Offline
```
::【Program 25】Title: Find the sum of 1 + 2! + 3! +... + 20!
@echo off&setlocal
for /l %%a in (1,1,20) do call:j %%a&set/a sum+=j
echo %sum%
pause
exit/b

:j
set/a j=1
for /l %%a in (1,1,%1) do set/a j*=%%a
```
Floor 13 Posted 2008-04-11 20:02 ·  中国 陕西 西安 电信
银牌会员
★★★★
钻石会员
Credits 2,278
Posts 1,020
Joined 2007-11-19 13:34
18-year member
UID 103127
Gender Male
Status Offline
If there are any that you don't know, refer to:

http://www.cn-dos.net/forum/viewthread.php?tid=38256&page=2&sid=GpG7Fd#pid270829

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

进入网盘(各种工具)~~ 空间~~cmd学习
Floor 14 Posted 2008-04-11 20:24 ·  中国 北京 电信
银牌会员
★★★
Credits 2,098
Posts 566
Joined 2007-09-11 07:27
18-year member
UID 97070
Gender Male
Status Offline

::【Program 19】Find "Perfect Numbers"
@echo off
echo Calculating...
for /l %%a in (1,1,1000) do (
setlocal enabledelayedexpansion
set/a sum=0
set/a a'=%%a-1
for /l %%b in (1,1,!a'!) do (
set/a t=%%a%%%%b
if !t! equ 0 set/a sum+=%%b
)
if !sum! equ %%a set/p=%%a <nul
endlocal
)
pause
Floor 15 Posted 2008-04-11 20:41 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
```@echo off
for /l %%i in (100,1,999) do (
set str=%%i
setlocal enabledelayedexpansion
set a=!str:~,1!&set b=!str:~1,1!&set c=!str:~2,1!
set /a var=!a!*!a!*!a!+!b!*!b!*!b!+!c!*!c!*!c!
if !var! equ !str! echo !str!
endlocal
)
pause```

[ Last edited by zw19750516 on 2008-4-11 at 10:40 PM ]
批处理之家新域名:www.bathome.net
Forum Jump: