筆記、[ALG101] 虛擬碼


Posted by s103071049 on 2021-04-21

|學好程式,先從不要寫程式開始

如何撰寫 pseudo code

  • 從 1~100 印出 i
for(i from 1 t0 100) do
    print i 
end for
  • 印出 1~100 的奇數
for(i from 1 t0 100) do
    if( n mode 2 ===1) then
        print i 
    end if
end for
  • FizzBuzz

for(i from 1 to 100) do
  if (i%15===0) print "FizzBuzz "
  else if(i%3===0) print "Fizz"
  else if(i%5===0) print "Buzz"
  else print(i)

end for  //先求有再求好,不是最有效率地解法
  • 找最小值
let 最小的牌 = 第一張牌
for(i form 1 to n) do 
  open the i-th card
  if(第i張牌比最小的牌還小) do
     最小的牌 = 第i張牌
  end if
end for 

-----
let min =arr[0]
for (i from 0 to n-1) do
  if(arr[i]< min) do
     min=arr[i]
  end if
end for
print min
  • 字串反轉
//先想著如何把字串一個字一個字印出來
let result=""
for(i from n-1 to 0) do
   result+=str[i]
end for
print result
  • 陣列總和
//結構跟反轉字串極為類似
let sum =0

for(i from 0 to n-1)do
   sum+=arr[i]

end for
print sum
  • 找最大值
let max=arr[0]

for(i from 0 to n-1) do 
   if(arr[i]>max) do 
       max=arr[i]
   end if 

end for
print max

#pseudo code







Related Posts

[Release Notes] 20200926_v1 - Refined main style to Dark Mode

[Release Notes] 20200926_v1 - Refined main style to Dark Mode

[ week 1 ] Markdown 語法筆記

[ week 1 ] Markdown 語法筆記

pure function

pure function


Comments