Try   HackMD

2024 Sprout W3 作業題解

No. 294 我愛零分

題目要求:

  • 給你
    n
    個成績,要對他們進行一些修改。
  • 把大於
    0
    的數字改成
    0
    ,其他數字不用動。
  • 把更改過後的成績倒著輸出。

注意事項:

  • 注意陣列的長度要開多大,還有索引值是從
    0
    開始數。
  • 注意輸出每個成績之後都要換行。

Pseudo Code

declare scores[], n

read input to n

for i from 0 to n - 1 :
    
    read input to scores[i]
    
    if scores[i] > 0 :
        set scores[i] to 0

for i from n - 1 to 0 :
    
    output scores[i]

AC Code

#include <iostream> using namespace std ; int main() { int n, scores[10005] ; cin >> n ; for (int i = 0; i < n; i++) { cin >> scores[i] ; if (scores[i] > 0) { scores[i] = 0 ; } } for (int i = n - 1; i >= 0; i--) { cout << scores[i] << "\n" ; } }

No. 953 維吉尼亞的統計學

題目要求:

  • 給定兩個只包含英文小寫字母的字串,分別為明文和金鑰。
  • 將明文根據給定的加密對照表和密鑰加密。
  • 統計加密過後的密文中,小寫字母 a - z 個別出現了幾次。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

plaintext: sprout
key: abcdef
cyphertext: sqtryy

根據觀察會發現,在同一行中從上往下看,字母會是正常的英文字母順序,直到遇到 z 之後再接 a。

所以其實不用把整個表記錄下來,只需要用推算的就好!

比如說現在是明文的字母是 s,而金鑰的字母是 j。

我們知道 j 是從

0 開始數的第
9
個字母,所以我們只要從 s 往後偏移
9
個字母,t、u、v、w、x、y、z、a、b,就會得到加密過後的字母是 b,也就是從
0
開始數的第
1
個字母。

而為了方便我們做紀錄,我們可能會想要開一個陣列,每個索引值代表不同的字母,比如說 a 的資訊就存在索引值為

0 的位置,j 的資訊就存在索引值為
9
的位置。

所以知道加密過後的字母是第幾個字母是至關重要的。

實際上寫成 code 的話,大概會是類似:

(int)(('s' - 'a' + 'j' - 'a') % 26) // integer 形式,也就是從 0 開始數的第幾個字母

注意事項:

  • 注意陣列的長度要開多大,還有索引值是從
    0
    開始數。
  • 注意輸出每個成績之後都要換行。
  • 記得要模
    26

Pseudo Code

declare text[], key[], count[]

initialize count[] to 0

read input to text and key

for i from 0 to strlen(text) :

    declare cypher_num
    
    set cypher_num to get_cypher_num(text[i], key[i])
    
    count[cypher_num] += 1
    
for i from 0 to 25 :

    output count[i]

AC Code

#include <iostream> using namespace std ; int main() { char text[1005], key[1005] ; int count[26] = {0} ; cin >> text >> key ; int text_len = strlen(text) ; for (int i = 0; i < text_len; i++) { count[(int)(text[i] - 'a' + key[i] - 'a') % 26]++ ; } for (int i = 0; i < 26; i++) { cout << count[i] ; if (i < 25) cout << " " ; } return 0 ; }