a569簡單以64分音符為整數單位

CPE:Jingle Composing (UVa 12195)

把分數加法改成整數累加,遇分隔符就結算。

前往題目練習 →回報教學錯誤每筆時間 O(L),空間 O(1)。

解題思路

所有音符時值分母都是64的因數,因此把一小節長度1換成64。W、H、Q、E、S、T、X分別對應64、32、16、8、4、2、1,完全不需要浮點數。

掃描音符時把權重加到current;碰到/時,若current恰為64便把合法數加1,再把current歸零。開頭的/只會結算0,不影響答案;結尾的/會結算最後一節,不能漏掉。單獨*終止,每段只輸出一個整數。

Step-by-step animation

示意:/HH/QQQ/W/

回報動畫錯誤
步驟 1 / 3

第一小節

H+H=32+32=64,合法。

HH:64
合法數
1

Reference solution

回報程式錯誤

完整程式

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <array>
#include <tuple>
#include <functional>
#include <limits>
#include <cstdint>
#include <cctype>
#include <utility>
using namespace std;

int main() {
    map<char, int> value{{'W', 64}, {'H', 32}, {'Q', 16}, {'E', 8}, {'S', 4}, {'T', 2}, {'X', 1}};
    string s;
    while (cin >> s && s != "*") {
        int sum = 0, ans = 0;
        for (char c : s) {
            if (c == '/') {
                ans += sum == 64;
                sum = 0;
            } else
                sum += value[c];
        }
        cout << ans << '\n';
    }
}

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

更多