題目: UVa - 12195 - Jingle Composing

題目說明

每筆測資會有代表音符的字母及 / ,每個音符分別代表不同的持續時間,兩個 / 包起來的字母代表一次的測資。題目要求輸出持續時間為 1 的測資數量。

解題思路

使用 String 讀取測資,從 Size = 1 時開始,利用 Switch 判斷字符並加入與其相對應的時間。當 Switch 執行到 Default 時代表目前字符為 / ( 每一行資料只有代表音符的字母及 / ),此時若時間為 64 ( 將每個音符持續時間都乘以 64,方便運算 ) 則將 ans + 1

參考解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>

using namespace std;

int main()
{
string input;

while ((cin >> input) && input != "*")
{
int ans = 0, time = 0;

for (size_t i = 1; i < input.size(); ++i)
{
switch (input[i])
{
case 'W':
time += 64;
break;
case 'H':
time += 32;
break;
case 'Q':
time += 16;
break;
case 'E':
time += 8;
break;
case 'S':
time += 4;
break;
case 'T':
time += 2;
break;
case 'X':
time += 1;
break;
default:
if (time == 64)
++ans;
time = 0;
break;
}
}
cout << ans << endl;
}
}