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();
}
}