iT邦幫忙

2023 iThome 鐵人賽

DAY 4
0
AI & Data

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

(DAY 4)CPE一星題庫UVA10101&UVA10420(C/C++)

  • 分享至 

  • xImage
  •  

UVA10101

ChatGPT 問題輸入:

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

問題描述

中文:UVA10101問題的描述是要求您將給定的數字轉換為孟加拉數字的文字表示形式。孟加拉數字是一種特殊的數字系統,其數字單詞是由"shata"(百)、"hajar"(千)、"lakh"(十萬)、"koti"(千萬)組成,並且使用特殊的數字字母表示,例如"1"對應到"ek","2"對應到"dhoi",等等。如果有多個"0"連續出現,則只需輸出一次"0"。
英文:The problem UVA10101 asks you to convert a given number into its textual representation in Bangla numbers. Bangla numbers are a special number system with words like "shata" (hundred), "hajar" (thousand), "lakh" (hundred thousand), and "koti" (ten million), and they use special number words like "ek" for "1," "dhoi" for "2," and so on. If there are multiple consecutive "0"s, you should print only one "0".

程式碼示例

以下是一個示例的C++程式碼,用於解決UVA10101問題。這個程式碼包括基本的功能,但您可以根據需要添加錯誤處理和邊界檢查。

#include <iostream>
#include <string>
using namespace std;

// Function to convert a number less than 100 into Bangla text
string convertLessThan100(int num) {
    const string below20[] = {"", "ek", "dhoi", "teen", "char", "panch", "chhoy", "shat", "at", "noi", "dosh"};
    const string tens[] = {"", "", "bish", "trish", "chollish", "pachaas", "chaabbis", "shat", "aatt", "nobbay"};
    
    if (num < 11) {
        return below20[num];
    } else if (num < 20) {
        return "baro " + below20[num - 10];
    } else {
        int tensDigit = num / 10;
        int onesDigit = num % 10;
        return tens[tensDigit] + " " + below20[onesDigit];
    }
}

// Function to convert a number into Bangla text
string convertToBangla(int num) {
    if (num == 0) {
        return "shunno"; // Zero
    }
    
    string result = "";
    
    int crore = num / 10000000;
    num %= 10000000;
    int lakh = num / 100000;
    num %= 100000;
    int thousand = num / 1000;
    num %= 1000;
    int hundred = num / 100;
    num %= 100;
    
    if (crore > 0) {
        result += convertLessThan100(crore) + " koti ";
    }
    
    if (lakh > 0) {
        result += convertLessThan100(lakh) + " lakh ";
    }
    
    if (thousand > 0) {
        result += convertLessThan100(thousand) + " hajar ";
    }
    
    if (hundred > 0) {
        result += convertLessThan100(hundred) + " shat ";
    }
    
    if (num > 0) {
        result += convertLessThan100(num);
    }
    
    return result;
}

int main() {
    int num;
    int caseNum = 1;
    
    while (cin >> num) {
        cout << caseNum << ". " << convertToBangla(num) << endl;
        caseNum++;
    }
    
    return 0;
}

解題思路

  1. 定義了兩個函數convertLessThan100convertToBangla來執行轉換工作。convertLessThan100用於將小於100的數字轉換為孟加拉文字表示,convertToBangla用於將大數字轉換為孟加拉文字表示。

  2. convertLessThan100中,我們使用陣列存儲1到10和10到20的數字的文字表示,然後根據輸入的數字將它們組合起來。

  3. convertToBangla中,我們首先將輸入數字拆分成億、萬、千和百位數字,然後根據這些數字依次轉換為孟加拉文字表示,並用"koti"、"lakh"、"hajar"和"shat"來分隔它們。

  4. 最後,將轉換後的文本輸出到標準輸出。

UVA10420

ChatGPT 問題輸入:
大學程式能力檢定 UVA10420 List of Conquests 問題的中文與英文描述,包含錯誤處理和邊界檢查的完整C++範例程式碼,用於解決UVA10420問題,以及解題思路的解釋

問題描述

中文:UVA10420問題的描述是要求您維護一個國家的征服清單。每當一個國家被征服,您都需要記錄征服日期以及征服者的名字。然後,您需要列印出每個不同的征服者的名字以及他們的征服次數,按字母順序排列。
英文:The problem UVA10420 requires you to maintain a list of conquests for a country. Whenever a country is conquered, you need to record the date of conquest along with the name of the conqueror. Then, you need to print the names of each distinct conqueror and the number of times they have conquered, sorted alphabetically.

程式碼示例

這是一個簡單的C++程式碼示例,用於解決UVA10420問題。請注意,這個示例僅包括基本的功能,您可能需要添加錯誤處理和邊界檢查。

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
    int n;
    cin >> n;
    cin.ignore(); // Consume the newline character
    
    map<string, int> conquerors; // To store conqueror names and their conquest count
    
    for (int i = 0; i < n; ++i) {
        string line;
        getline(cin, line);
        size_t space_pos = line.find(' '); // Find the first space
        string conqueror = line.substr(0, space_pos); // Extract conqueror's name
        
        // Increment the conquest count for this conqueror
        conquerors[conqueror]++;
    }
    
    // Print conquerors and their conquest count in alphabetical order
    for (const auto& entry : conquerors) {
        cout << entry.first << " " << entry.second << endl;
    }
    
    return 0;
}

解題思路

  1. 首先,讀取整數n,表示有n筆征服記錄。

  2. 使用map來儲存征服者的名字以及對應的征服次數。map保證了名字的字母順序排列,並且它會自動處理重複的名字。

  3. 使用getline函數來讀取每一筆征服記錄。找到第一個空格,將名字提取出來,然後將對應的map條目的征服次數增加1。

  4. 最後,遍歷map,按字母順序輸出每個不同的征服者的名字以及他們的征服次數。


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

尚未有邦友留言

立即登入留言