闲着无聊就更新(
上次更新时间: 2023.10.16 2023.10.16 2023.10.16
Computer Science
Algorithm
Introduction
计算机:p r o g r a m → i n s t r u c t i o n → p r o b l e m program→instruction→problem p r o g r am → in s t r u c t i o n → p r o b l e m
A l g o r i t h m { I n p u t O u t p u t U n a m b i g u i t y F i n i t e n e s s E f f e c t i v e n e s s L a n g u a g e I n d e p e n d e n t Algorithm\begin{cases}Input\\Output\\Unambiguity\\Finiteness\\Effectiveness\\Language\ Independent\end{cases} A l g or i t hm ⎩ ⎨ ⎧ I n p u t O u tp u t U nambi g u i t y F ini t e n ess E f f ec t i v e n ess L an g u a g e I n d e p e n d e n t
Program Development Life Cycle
Abstracted things I \mathrm{I} I
D e c o m p o s i t i o n \mathbf{Decomposition} Decomposition i s u s e d t o s p l i t t h e p r o g r a m i n t o s u b p r o g r a m s is\ used\ to\ split\ the\ program\ into\ subprograms i s u se d t o s pl i t t h e p r o g r am in t o s u b p r o g r am s
Structure Diagram
C a l c u l a t o r I n p u t P r o c e s s O u t p u t ⏞ I n p u t a , b f i n d m a x i m u m i n a , b O u t p u t a n s \begin{matrix}Calculator\\\overbrace{Input\qquad\qquad\qquad\qquad Process\qquad\qquad\qquad\qquad Output}\\Input\ a,b\qquad\qquad find\ maximum\ in\ a,b\qquad\qquad Output\ ans\end{matrix} C a l c u l a t or I n p u t P r ocess O u tp u t I n p u t a , b f in d ma x im u m in a , b O u tp u t an s
I n p u t : Input: I n p u t : T h e n u m b e r y o u e n t e r i n t h e c o m p u t e r . The\ number\ you\ enter\ in\ the\ computer. T h e n u mb er y o u e n t er in t h e co m p u t er .
P r o c e s s : Process: P r ocess : U s e a l g o r i t h m t o a c h i e v e s o m e g o a l s . Use\ algorithm\ to\ achieve\ some\ goals. U se a l g or i t hm t o a c hi e v e so m e g o a l s .
O u t p u t Output O u tp u t T h e n u m b e r c o m p u t e r t e l l y o u a f t e r t h e a l g o r i t h m . The\ number\ computer\ tell\ you\ after\ the\ algorithm. T h e n u mb er co m p u t er t e l l y o u a f t er t h e a l g or i t hm .
3 main aspects of Algorithm
A l g o r i t h m { S e q u e n c i n g − o r d e r S e l e c t i o n − c h o i c e I t e r a t i o n − r e p e c t Algorithm\begin{cases}Sequencing-order\\Selection-choice\\Iteration-repect\end{cases} A l g or i t hm ⎩ ⎨ ⎧ S e q u e n c in g − or d er S e l ec t i o n − c h o i ce I t er a t i o n − r e p ec t
Flow chart
Abstracted things I I \mathrm{II} II
F o r e x a m p l e : J a v a For\ example: Java F or e x am pl e : J a v a
代码 复制 Math mathObject=new Math (...)
A l g o r i t h m { D e c l a r e : M a t h m a t h O b j e c t I n s t a n t i a t e : n e w M a t h ( . . . ) I n i t i a l i z e : M a t h m a t h O b j e c t = n e w M a t h ( . . . ) Algorithm\begin{cases}Declare:Math\ mathObject\\Instantiate:new\ Math(...)\\Initialize:Math\ mathObject=new\ Math(...)\end{cases} A l g or i t hm ⎩ ⎨ ⎧ D ec l a r e : M a t h ma t h O bj ec t I n s t an t ia t e : n e w M a t h ( ... ) I ni t ia l i z e : M a t h ma t h O bj ec t = n e w M a t h ( ... )
Pseudocode solution
P s e u d o c o d e : A m e t h o d o f r e p r e s e n t i n g a n a l g o r i t h m u s i n g s i m p l e w o r k s a n d m a t h e m a t i c a l o p e r a t o r s . Pseudocode: A\ method\ of\ representing\ an\ algorithm\ using\ simple\ works\ and\ mathematical\ operators. P se u d oco d e : A m e t h o d o f r e p r ese n t in g an a l g or i t hm u s in g s im pl e w or k s an d ma t h e ma t i c a l o p er a t or s .
W h e n y o u w a n t t o u s e a v a r i a b l e , y o u n e e d t o d e c l a r e i t f i r s t . When\ you\ want\ to\ use\ a\ variable,\ you\ need\ to\ declare\ it\ first. W h e n y o u w an t t o u se a v a r iab l e , y o u n ee d t o d ec l a r e i t f i r s t .
代码 复制 DECLARE n:INTEGER // int
DECLARE n:STRING // string
DECLARE n:BOOLEAN // bool
DECLARE n:REAL // float
I N P U T & O U T P U T INPUT\ \&\ OUTPUT I N P U T & O U T P U T
I N P U T i s u s e d f o r d a t a e n t r y . INPUT\ is\ used\ for\ data\ entry. I N P U T i s u se d f or d a t a e n t r y .
O U T P U T i s u s e d t o d i s p l a y a s t r i n g o r a v a r i a b l e . OUTPUT\ is\ used\ to\ display\ a\ string\ or\ a\ variable. O U T P U T i s u se d t o d i s pl a y a s t r in g or a v a r iab l e .
代码 复制 INPUT n
OUTPUT n
OUTPUT "I AK IOI"
A s s i g n i n g a V a l u e Assigning\ a\ Value A ss i g nin g a V a l u e
V a l u e s a r e a s s i g n e d t o a v a r i a b l e u s i n g t h e ← o p e r a t o r . Values\ are\ assigned\ to\ a\ variable\ using\ the\ ← operator. V a l u es a r e a ss i g n e d t o a v a r iab l e u s in g t h e ← o p er a t or .
T h e v a l u e o n t h e r i g h t o f t h e ← o p e r a t o r i s a s s i g n e d t o t h e l e f t . The\ value\ on\ the\ right\ of\ the\ ← operator\ is\ assigned\ to\ the\ left. T h e v a l u e o n t h e r i g h t o f t h e ← o p er a t or i s a ss i g n e d t o t h e l e f t .
M a t h e m a t i c a l e x p r e s s i o n s c a n b e u s e d o n t h e r i g h t s i d e o f t h e ← o p e r a t o r . Mathematical\ expressions\ can\ be\ used\ on\ the\ right\ side\ of\ the\ ← operator. M a t h e ma t i c a l e x p r ess i o n s c an b e u se d o n t h e r i g h t s i d e o f t h e ← o p er a t or .
代码 复制 Name <- "Mike"
Age <- 32
Gender <- "M"
Salary <- 6000
Expense <- 1145
Saving <- Salary- Expense
I F . . . T H E N . . . E L S E . . . E N D I F IF...THEN...ELSE...ENDIF I F ... T H E N ... E L S E ... E N D I F
S t a r t w i t h Start\ with S t a r t w i t h IF f o l l o w e d b y a c o n d i t i o n a l s t a t e m e n t . followed\ by\ a\ conditional\ statement. f o l l o w e d b y a co n d i t i o na l s t a t e m e n t .
I n d e n t 2 s p a c e s f o r Indent\ 2\ spaces\ for I n d e n t 2 s p a ces f or THEN.
I n d e n t 2 m o r e s p a c e s t o s h o w w h i c h c o m m a n d s a r e p a r t o f t h e Indent\ 2\ more\ spaces\ to\ show\ which\ commands\ are\ part\ of\ the I n d e n t 2 m or e s p a ces t o s h o w w hi c h co mman d s a r e p a r t o f t h e THEN b l o c k . block. b l oc k .
R e t u r n t o t h e i n d e n t a t i o n o f t h e Return\ to\ the\ indentation\ of\ the R e t u r n t o t h e in d e n t a t i o n o f t h e THEN k e y w o r d a n d u s e keyword\ and\ use k ey w or d an d u se ELSE i f n e e d if\ need i f n ee d
I n t h e In\ the I n t h e ELSE c a s e , i n d e n t 2 s p a c e s t o s h o w w h i c h c o m m a n d s a r e p a r t o f t h e case,\ indent\ 2\ spaces\ to\ show\ which\ commands\ are\ part\ of\ the c a se , in d e n t 2 s p a ces t o s h o w w hi c h co mman d s a r e p a r t o f t h e THEN b l o c k . block. b l oc k .
R e t u r n t o t h e i n d e n t a t i o n f o r t h e Return\ to\ the\ indentation\ for\ the R e t u r n t o t h e in d e n t a t i o n f or t h e IF k e y w o r d a n d t h e keyword\ and\ the k ey w or d an d t h e ENDIF a t t h e e n d o f t h e c o d e . at\ the\ end\ of\ the\ code. a t t h e e n d o f t h e co d e .
代码 复制 IF Age>= 10
THEN
OUTPUT "Eligible"
ELSE
OUTPUT "Ineligibe"
ENDIF
C A S E O F . . . E N D C A S E CASE\ OF...ENDCASE C A S E O F ... E N D C A S E
S t a r t w i t h Start\ with S t a r t w i t h CASE OF f o l l o w e d b y t h e n a m e o f a v a r i a b l e p r e v i o u s l y d e c l a r e d . followed\ by\ the\ name\ of\ a\ variable\ previously\ declared. f o l l o w e d b y t h e nam e o f a v a r iab l e p r e v i o u s l y d ec l a r e d .
I n d e n t 3 s p a c e s a n d g i v e o n e p o t e n t i a l v a l u e f o r t h e v a r i a b l e f o l l o w e d b y a c o l o n a n d t h e n t h e f i r s t s t a t e m e n t Indent\ 3\ spaces\ and\ give\ one\ potential\ value\ for\ the\ variable\ followed\ by\ a\ colon\ and\ then\ the\ first\ statement I n d e n t 3 s p a ces an d g i v e o n e p o t e n t ia l v a l u e f or t h e v a r iab l e f o l l o w e d b y a co l o n an d t h e n t h e f i r s t s t a t e m e n t
f o r t h a t c o n d i t i o n . for\ that\ condition. f or t ha t co n d i t i o n .
S u b s e q u e n t s t a t e m e n t s f o r t h a t c o n d i t i o n s h o u l d f o l l o w t h e i n d e n t a t i o n o f t h e f i r s t s t a t e m e n t . Subsequent\ statements\ for\ that\ condition\ should\ follow\ the\ indentation\ of\ the\ first\ statement. S u b se q u e n t s t a t e m e n t s f or t ha t co n d i t i o n s h o u l d f o l l o w t h e in d e n t a t i o n o f t h e f i r s t s t a t e m e n t .
R e t u r n t o t h e i n d e n t a t i o n f o r t h e f i r s t p o t e n t i a l v a l u e a n d l i s t a n o t h e r p o t e n t i a l v a l u e f o l l o w e d b y a c o l o n Return\ to\ the\ indentation\ for\ the\ first\ potential\ value\ and\ list\ another\ potential\ value\ followed\ by\ a\ colon R e t u r n t o t h e in d e n t a t i o n f or t h e f i r s t p o t e n t ia l v a l u e an d l i s t an o t h er p o t e n t ia l v a l u e f o l l o w e d b y a co l o n
a n d t h e f i r s t s t a t e m e n t f o r t h a t c o n d i t i o n . and\ the\ first\ statement\ for\ that\ condition. an d t h e f i r s t s t a t e m e n t f or t ha t co n d i t i o n .
R e p e a t u n t i l a l l p o t e n t i a l v a l u e s h a v e b e e n l i s t e d . Repeat\ until\ all\ potential\ values\ have\ been\ listed. R e p e a t u n t i l a l l p o t e n t ia l v a l u es ha v e b ee n l i s t e d .
F o r a l l o t h e r v a l u e s t h e For\ all\ other\ values\ the F or a l l o t h er v a l u es t h e OTHERWISE k e y w o r d c a n b e u s e d i n t h e s a m e w a y a s t h e p o t e n t i a l v a l u e . keyword\ can\ be\ used\ in\ the\ same\ way\ as\ the\ potential\ value. k ey w or d c an b e u se d in t h e s am e w a y a s t h e p o t e n t ia l v a l u e .
R e t u r n t o t h e i n d e n t a t i o n o f t h e Return\ to\ the\ indentation\ of\ the R e t u r n t o t h e in d e n t a t i o n o f t h e CASE OF k e y w o r d a n d p u t keyword\ and\ put k ey w or d an d p u t ENDCASE t o s h o w t h e e n d o f t h e s e l e c t i o n . to\ show\ the\ end\ of\ the\ selection. t o s h o w t h e e n d o f t h e se l ec t i o n .
代码 复制 CASE OF Symbol
'+' : OUTPUT a + b
'-' : OUTPUT a - b
'*' : OUTPUT a * b
'/' : OUTPUT a / b
OTHERWISE : OUTPUT "No Valid Symbol..."
ENDCASE
然后我不想写了
接着存储代码吧
T1
写一个伪代码,记录50个骰子投两边的总和的数量。
代码 复制 DECLARE rand1:INTEGER
DECLARE arr:ARRAY[1 :12 ] OF INTEGER
FOR i<- 1 TO 50
rand1<- INT (RAND (1 )* 6 + 1 )+ INT (RAND (1 )* 6 + 1 )
arr[rand1]<- arr[rand1]+ 1
NEXT i
DECLARE str:STRING
FOR i<- 2 TO 12
str<- ""
str<- str& i& ":"
FOR j<- 1 TO arr[i]
str<- str& "X"
NEXT j
PRINT str
NEXT i
上课学习array随便写的代码
代码 复制 DECLARE name:ARRAY[1 :13 ] OF STRING
DECLARE exams:ARRAY[1 :13 ,1 :7 ] OF REAL
DECLARE average:ARRAY[1 :13 ] OF REAL
FUNCTION getnumber () RETURNS REAL
DECLARE randomnumber:REAL
randomnumber<- RAND (10000 )
randomnumber<- INT (randomnumber)
randomnumber<- randomnumber/ 100
RETURN randomnumber
ENDFUNCTION
PROCEDURE findaverage ()
DECLARE sum:REAL
FOR i<- 1 TO 13
sum<- 0.00
FOR j<- 1 TO 7
sum<- sum+ exams[i,j]
NEXT j
sum<- sum/ 7
average[i]<- INT (sum* 100 )/ 100.0
NEXT i
ENDPROCEDURE
PROCEDURE print ()
DECLARE string1:STRING
FOR i<- 1 TO 13
string1<- name[i]
FOR j<- LENGTH (string1) TO 6
string1<- string1& " "
NEXT j
FOR j<- 1 TO 7
string1<- string1& exams[i,j]& " "
FOR k<- LENGTH (string1) TO (6 * (j+ 1 ))
string1<- string1& " "
NEXT k
NEXT j
string1<- string1& " average score:" & average[i]
OUTPUT string1
NEXT i
ENDPROCEDURE
FOR i<- 1 TO 13
INPUT name[i]
FOR j<- 1 TO 7
exams[i,j]<- getnumber ()
NEXT j
NEXT i
CALL findaverage ()
CALL print ()