【NEEPU OJ】1007--This is a easy problem 4 U,3.

本文介绍了一种算法,用于统计字符串中连续元音字母组成的元音组合数量。通过两个示例,解释了如何在给定字符串中识别并计数这些元音组合,输入为整数T表示测试用例数量,每组测试用例包含字符串长度N和由N个英文字母组成的字符串。
摘要由CSDN通过智能技术生成
Description

Well…everyone know that vowels in English are a, e, i, o and u.

Let’s call all the vowels in consecutive indexes of a string as a single Gang of Vowels.

Now, you are given a string consisting only lowercase letters. Count how many Gang of Vowels in the given string exists.

Input

Input starts with an integer T (1 ≤ T ≤ 20), denoting the number of test cases.Each case contains two lines. First line contains an integer N (1 ≤ N ≤ 1000) denoting the length of the string. The next line will contain a string consists of N lowercase letters

Output

For each case, output will be in Case tc: cnt format, where tc is the test case number and cnt is the total number of Gang of Vowels in the given string.

输入样例 1

2
4
abab
3
aeb

输出样例 1

Case 1: 2
Case 2: 1

提示

string “abab” consists 2 (a, a) Gangs while string “aeb” consist 1 (ae) Gang.

来源

Dev skill


代码

1.cpp

#include <cstdio>
using namespace std;

bool gang(char g){
    if(g == 'a' || g == 'e' || g == 'i' || g == 'o' || g == 'u'){
        return true;
    }
    else{
        return false;
    }
}

int main(){
    int t, n, i, cnt;
    char w1, w2;
    scanf("%d", &t);
    for(i = 1; i <= t; ++i){
        cnt = 0;
        w1 = 'A';
        scanf("%d%*c", &n);
        while(n--){
            scanf("%c", &w2);
            if(!gang(w1) && gang(w2)){
                ++cnt;
            }
            w1 = w2;
        }
        printf("Case %d: %d\n", i, cnt);
    }
}

2.java

import java.util.Scanner;

public class Main {

    static boolean gang(char g) {
        if (g == 'a' || g == 'e' || g == 'i' || g == 'o' || g == 'u') {
            return true;
        }
        else {
            return false;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t, n, cnt, i, j;
        String inp;
        char w1, w2;
        t = sc.nextInt();
        for (i = 1; i <= t; ++i) {
            cnt = 0;
            w1 = 'A';
            n = sc.nextInt();
            inp = sc.next();
            for (j = 0; j < n; ++j) {
                w2 = inp.charAt(j);
                if (!gang(w1) && gang(w2)) {
                    ++cnt;
                }
                w1 = w2;
            }
            System.out.printf("Case %d: %d%n", i, cnt);
        }
        sc.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值