UVa12195 - Jingle Composing(strtok的使用)

本文介绍了一位音乐爱好者 Marcos 在尝试创作铃声时遇到的问题。通过将音符的持续时间转换为特定数值,文章提供了一个计算每段旋律是否符合预设标准的方法,并附带了实现这一功能的 C++ 代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. C. Marcos is taking his first steps in the direction of jingle composition. He is having some troubles, but at least he is achieving pleasant melodies and attractive rhythms.

In music, a note has a pitch (its frequency, resulting in how high or low is the sound) and a duration (for how long the note should sound). In this problem we are interested only in the duration of the notes.

A jingle is divided into a sequence of measures, and a measure is formed by a series of notes.

The duration of a note is indicated by its shape. In this problem, we will use uppercase letters to indicate a note's duration. The following table lists all the available notes:

\epsfbox{p4481.eps}

The duration of a measure is the sum of the durations of its notes. In Marcos' jingles, each measure has the same duration. As Marcos is just a beginner, his famous teacher Johann Sebastian III taught him that the duration of a measure must always be 1.

For example, Marcos wrote a composition containing five measures, of which the first four have the correct duration and the last one is wrong. In the example below, each measure is surrounded with slashes and each note is represented as in the table above.


/HH/QQQQ/XXXTXTEQH/W/HW/


Marcos likes computers as much as music. He wants you to write a program that determines, for each one of his compositions, how many measures have the right duration.

Input 

The input contains several test cases. Each test case is described in a single line containing a string whose length is between 3 and 200 characters, inclusive, representing a composition. A composition begins and ends with a slash `/'. Measures in a composition are separated by a slash `/'. Each note in a measure is represented by the corresponding uppercase letter, as described above. You may assume that each composition contains at least one measure and that each measure contains at least one note. All characters in the input will be either slashes or one of the seven uppercase letters used to represent notes, as described above.

The last test case is followed by a line containing a single asterisk.

Output 

For each test case your program must output a single line, containing a single integer, the number of measures that have the right duration.

Sample Input 

/HH/QQQQ/XXXTXTEQH/W/HW/ 
/W/W/SQHES/ 
/WE/TEX/THES/ 
*

Sample Output 

4 
3 
0
题意:给出一行字符串,求出两个斜杠之间的字母之和是否等于1,统计个数

思路:注意用浮点数精确度的问题,在这里用W=64,H=32, Q=16, E=8, S=4, T=2, X=1,计算两斜杠之间的数字之和,看是否等于64

#include <cstdio>
#include <cstring>
#include <map>

using namespace std;

const int MAXN = 210;

char str[MAXN];

int cal(char ch)
{
    switch (ch) {
        case 'W': return 64;
        case 'H': return 32;
        case 'Q': return 16;
        case 'E': return 8;
        case 'S': return 4;
        case 'T': return 2;
        case 'X': return 1;
    }
}

bool input() 
{
    scanf("%s", str);
    if (strcmp(str, "*") == 0) return false;
    
    return true;
}

void solve() 
{
    char *p = strtok(str, "/");
    int ans = 0;
    
    while (p) {
        int sum = 0;
        for (int i = 0, len = strlen(p); i < len; i++) {
            sum += cal(p[i]);
        }
        //printf("sum=%lf\n", sum);
        if (sum == 64) ans++;
        p = strtok(NULL, "/");
    }
    
    printf("%d\n", ans);
}

int main() 
{
#ifndef ONLINE_JUDGE
    freopen("/cygdrive/d/OJ/uva_in.txt", "r", stdin);
#endif

    while (input()) {
        solve();
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值