题解:UVA576 Haiku Review

· · 题解

题意

给你三个由 / 分开的句子,问你音节个数(元音的段数)是否符合要求(元音包括 aeiouy)。

分享

要想将这题代码写简短,重点在于读入。

首先,getline() 函数支持读取终止符,本题就是 /,用 stringstream 来提供输入流。

然后判断元音可以用 find(),对每段的首个元音计数即可。

实现

#include<bits/stdc++.h>
using namespace std;
string s,a[4],m = "aeiouy";
int cnt[4],res;
int main()
{
    while(getline(cin,s),s != "e/o/i")
    {
        memset(cnt,0,sizeof cnt);
        stringstream sin;
        sin<<s;
        res = 0;
        for(int i = 1; i <= 3; i ++) getline(sin,a[i],'/');
        for(int i = 1; i <= 3; i ++)
            for(int j = 0; j < a[i].size(); j ++)
                if(~m.find(a[i][j]) && (!j || m.find(a[i][j - 1]) == -1)) cnt[i] ++;
        if(cnt[1] != 5) puts("1");
        else if(cnt[2] != 7) puts("2");
        else if(cnt[3] != 5) puts("3");
        else puts("Y");
    }
    return 0;
}