iT邦幫忙

2023 iThome 鐵人賽

DAY 5
0
AI & Data

(C/C++)利用ChatGPT整理CPE(大學程式能力檢定)一星題庫系列 第 5

(DAY 5)CPE一星題庫UVA10008&UVA10222(C/C++)

  • 分享至 

  • xImage
  •  

UVA10008

ChatGPT 問題輸入:

大學程式能力檢定 UVA10008 What's Cryptanalysis? 問題的中文與英文描述,包含錯誤處理和邊界檢查的完整C++範例程式碼,用於解決UVA10008問題,以及解題思路的解釋

題目描述:
中文:Cryptanalysis是一個用於分析和解讀密碼的技術。這項工作對於破解消息的加密至關重要。現在,你的任務是編寫一個程式,用於統計一段文本中每個大寫字母出現的次數,並按字母順序列印它們,每個字母的出現次數都要附帶在旁邊。
英文:Cryptanalysis is the science of decrypting coded messages without possessing the key. It is a basic tool in modern computer security. Your task is to write a program which decrypts simple messages.

Write a program that decrypts simple messages for "Aliens from Outer Space, Inc."

範例程式碼:
以下是完整的C++程式碼示例:

#include <iostream>
#include <map>
#include <vector>
#include <cctype>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;
    cin.ignore();  // Ignore the newline character after n.

    map<char, int> letterCount;

    // Read and process each line of text.
    for (int i = 0; i < n; i++) {
        string line;
        getline(cin, line);

        // Iterate through each character in the line.
        for (char c : line) {
            if (isalpha(c)) {
                // Convert the character to uppercase and update the count.
                letterCount[toupper(c)]++;
            }
        }
    }

    // Create a vector of pairs to sort the letters by count and then by character.
    vector<pair<int, char>> sortedLetters;
    for (auto entry : letterCount) {
        sortedLetters.push_back(make_pair(entry.second, entry.first));
    }

    // Sort the letters first by count (in descending order) and then by character (in ascending order).
    sort(sortedLetters.begin(), sortedLetters.end(), greater<pair<int, char>>());

    // Print the sorted letters and their counts.
    for (auto entry : sortedLetters) {
        cout << entry.second << " " << entry.first << endl;
    }

    return 0;
}

解題思路
解決這個問題的思路相對簡單。您需要首先統計輸入文本中每個大寫字母出現的次數,然後按字母順序對它們進行排序,最後列印出每個字母和它們的出現次數。在這個過程中,您可以使用C++的STL容器(如map)來幫助統計和排序。

UVA10222

大學程式能力檢定 UVA10222 Decode the Mad man 問題的中文與英文描述,包含錯誤處理和邊界檢查的完整C++範例程式碼,用於解決UVA10222問題,以及解題思路的解釋

題目描述:

中文:Mad man想要知道你是否聰明到足以破解他的密碼。他寫了一段密文,你需要幫助他解碼。

你知道一個字母可以對應到另一個字母,例如:'a'對應到'c','b'對應到'd',以此類推。你需要編寫一個程式,將給定的密文解碼成明文。

英文:Mad man wants to know if you are smart enough to decrypt his coded message. He has written a piece of text and you need to help him to decode it.

You know that one letter corresponds to another letter. For example, 'a' corresponds to 'c', 'b' corresponds to 'd', and so on. You need to write a program that deciphers the given text into plain text.

範例程式碼:
以下是完整的C++程式碼示例,包括錯誤處理和邊界檢查:

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main() {
    string substitution = "abcdefghijklmnopqrstuvwxyz"; // 明文字母對應表

    string cipherText;
    while (getline(cin, cipherText)) {
        for (char& c : cipherText) {
            if (isalpha(c)) {
                // 將密文字母轉換為小寫,並使用對應表替換
                c = tolower(c);
                cout << substitution[c - 'a'];
            }
            else {
                // 如果不是字母,直接輸出原字符
                cout << c;
            }
        }
        cout << endl;
    }

    return 0;
}

解題思路:

這個問題是一個簡單的字母替換問題,即將一個字母替換為另一個字母。解決這個問題的思路非常簡單,只需要建立一個字母替換表,然後根據這個表將密文中的每個字母替換為對應的明文字母。


上一篇
(DAY 4)CPE一星題庫UVA10101&UVA10420(C/C++)
下一篇
(DAY 6)CPE一星題庫UVA11332&UVA10252(C/C++)
系列文
(C/C++)利用ChatGPT整理CPE(大學程式能力檢定)一星題庫11
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言