iT邦幫忙

2023 iThome 鐵人賽

DAY 8
0
AI & Data

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

(DAY 8)CPE一星題庫UVA12019&UVA10038(C/C++)

  • 分享至 

  • xImage
  •  

UVA12019

ChatGPT 問題輸入:

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

題目描述:
中文:Doom是一個天文學家,他發現每年的某些日期都有特殊的星期幾。他稱之為“Doom's Day”。Doom的日常生活中充滿了數字,他想要知道每年的特殊日期是星期幾。請幫助Doom編寫一個程式,根據輸入的年份和月份,找出特殊日期是星期幾。
英文:Doom, a renowned astronomer, has discovered that certain dates in each year fall on particular days of the week. He refers to them as "Doom's Day." Doom's life revolves around numbers, and he wants to know what day of the week these special dates fall on each year. Help Doom write a program that, given the year and month, determines the day of the week for Doom's Day.

範例程式碼:

#include <iostream>
using namespace std;

int main() {
    int t; // 測試案例數
    cin >> t;

    // 一年中每個月的Doom's Day日期
    int doomDays[] = {3, 0, 0, 4, 9, 6, 11, 8, 5, 10, 7, 12};

    while (t--) {
        int month, day;
        cin >> month >> day;

        // 計算當年的Doom's Day日期
        int yearDoomDay = (doomDays[month - 1] + day) % 7;

        // 根據星期幾輸出對應的文字
        string daysOfWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        cout << daysOfWeek[yearDoomDay] << endl;
    }

    return 0;
}

這個程式會根據給定的年份和月份,計算出該年的Doom's Day日期,並根據星期幾輸出結果。

解題思路:

  1. 我們使用一個陣列 doomDays 來存儲每個月的Doom's Day日期,注意到2月不在這個陣列中,所以我們設置為0。
  2. 對於每個測試案例,我們讀取輸入的年份和月份。
  3. 我們計算出該年的Doom's Day日期,即 (doomDays[month - 1] + day) % 7,其中 month - 1 用於匹配陣列的索引。
  4. 最後,我們根據星期幾的索引,在 daysOfWeek 陣列中找到對應的文字,並輸出結果。

UVA10038

ChatGPT 問題輸入:

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

題目描述:

中文:有一個整數序列,我們想確定這個序列是否是Jolly跳者。對於一個整數序列,如果相鄰兩個數字的差的絕對值恰好是1到n-1(n是序列中的整數個數)之間的所有數字,則稱其為Jolly跳者。請編寫一個程式,判斷給定的整數序列是否是Jolly跳者。
英文:Given an integer sequence, we want to determine if it is a Jolly Jumper sequence. For an integer sequence, if the absolute difference between adjacent two numbers is exactly 1 to n-1 (n is the number of integers in the sequence), then it is called a Jolly Jumper sequence. Write a program to determine if the given integer sequence is a Jolly Jumper sequence.

範例程式碼:

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

int main() {
    int n; // 整數個數
    while (cin >> n) {
        vector<int> sequence(n);
        vector<bool> jolly(n - 1, false);

        // 讀取整數序列
        for (int i = 0; i < n; i++) {
            cin >> sequence[i];
        }

        // 檢查相鄰數字的差的絕對值
        for (int i = 1; i < n; i++) {
            int diff = abs(sequence[i] - sequence[i - 1]);
            if (diff >= 1 && diff <= n - 1) {
                jolly[diff - 1] = true;
            }
        }

        // 檢查是否所有差的絕對值都出現過
        bool isJolly = all_of(jolly.begin(), jolly.end(), [](bool val) { return val; });

        if (isJolly) {
            cout << "Jolly" << endl;
        } else {
            cout << "Not jolly" << endl;
        }
    }

    return 0;
}

這個程式會判斷給定的整數序列是否是Jolly跳者。它首先讀取整數序列,然後計算相鄰數字的差的絕對值,並檢查這些差是否恰好是1到n-1之間的數字。最後,它檢查是否所有差的絕對值都出現過,如果是,則輸出"Jolly",否則輸出"Not jolly"。

解題思路:

  1. 我們使用一個vector來存儲整數序列,另一個vector用來表示差的絕對值是否出現過。
  2. 我們遍歷整數序列,計算相鄰數字的差的絕對值,如果差的絕對值在1到n-1之間,則將對應的位置標記為true。
  3. 最後,我們檢查是否所有差的絕對值都出現過,如果是,則判斷為Jolly跳者,否則不是。

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

尚未有邦友留言

立即登入留言