iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 5
0
自我挑戰組

CPE 一星精選全攻略系列 第 5

Day5 # UVa 10008 & UVa 10222

UVa 10008 - What's Cryptanalysis?

題目連結

題目說明

密碼翻譯,指將密文加以分析統計。
你的任務就是寫個程式來將 input 的訊息做字母統計分析,依照出現次數由大到小輸出,如果出現次數一樣就照字典序排列。

做法

計算字母出現頻率,寫個 sort() 的第三參數排序。

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    cin >> n;
    char c;
    vector<int> text(256);
    while (cin >> c) {
        if (isalpha(c))
            text[int(toupper(c))]++;
    }
    vector<pair<int, char> > ans;
    for (int i = 0; i < 256; i++)
        if (text[i])
            ans.push_back({text[i], char(i)});
    sort(ans.rbegin(), ans.rend(), [](auto a, auto b) {
        if (a.first == b.first)
            return (a > b);
        return b > a;
    });
    for (auto [i, c] : ans)
        cout << c << " " << i << endl;
    return 0;
}

UVa 10222 - Decode the Mad man

題目說明

題目說明

又是一道解密題目,解密方法就是每個字元依照鍵盤上的排序向左移兩格,空格跟換行則直接輸出。

做法

先開一個 string 將鍵盤上的排序記錄下來,然後直接用 find() 找出位置左移兩格輸出,但要記得先換成小寫字母。

#include <bits/stdc++.h>

using namespace std;

int main() {
    string keyboard = "`1234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm,./";
    string in;
    while (getline(cin, in)) {
        for (auto i : in) {
            if (i == ' ')
                cout << i;
            else
                cout << keyboard[keyboard.find(tolower(i)) - 2];
        }
        cout << endl;
    }
    return 0;
}

上一篇
Day4 #UVa 10101 & UVa 10420
下一篇
Day6 #UVa 11332 & UVa 10252
系列文
CPE 一星精選全攻略9
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
jorsonlee
iT邦新手 4 級 ‧ 2023-05-17 19:49:35

想請問一下UVa 10008這題的sort(),在a.first==b.first的時候的return a>b跟其他狀況下return b>a ,他們的sorting值是取first還是second呢?

我要留言

立即登入留言