a205簡單陣列、排序與區間掃描

TOI 2019/11 新手組:降雨量統計

同時計算每日與各時段的累計雨量。

前往題目練習 →回報教學錯誤固定 7×4 筆:時間 O(1),空間 O(1)。

這題在學什麼

每筆雨量只有一位小數,移除小數點即得到十分之一毫米整數,避免浮點數比較誤差。

對七列各加總四筆得到每日雨量,並按四個欄位累加得到各時段週雨量。

分別找兩個陣列最大值的位置,日索引加 1,時段名稱由固定表查出;最大值保證唯一。

Step-by-step animation

降雨量統計・列和與欄和

回報動畫錯誤
步驟 1 / 3

轉成整數

星期三下午 12.3 毫米轉為 123,其餘皆為 0。

週三下午=123
單位
0.1 毫米

Reference solution

回報程式錯誤

完整程式

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    vector<int> days(7), slots(4);
    for (int i = 0; i < 7; i++)
        for (int j = 0; j < 4; j++) {
            string s;
            cin >> s;
            s.erase(remove(s.begin(), s.end(), '.'), s.end());
            int x = stoi(s);
            days[i] += x;
            slots[j] += x;
        }
    vector<string> names = {"morning", "afternoon", "night", "early morning"};
    cout << max_element(days.begin(), days.end()) - days.begin() + 1 << '\n'
         << names[max_element(slots.begin(), slots.end()) - slots.begin()] << '\n';
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多