1、题目概述
该题目是一道简单的字符串处理类型题目,只需要根据题目要求用一般算法编程解决问题即可。
2、题目分析
输入:
The input contains one or more lines, each of which contains a single haiku. (输入包含多行,每行一首Haiku体诗)
A haiku contain at least three words, and words will be separated by either a single space or a slash('/'). Slashes also separate the
three lines of a haiku.(一首Haiku诗至少有三个单词,每两个单词间用一个空格或者‘/’分隔开。'/'也分隔开了一首Haiku诗的三句诗。)
A haiku will contain only lowercase letters('a' - 'z'), forward slashes('/'), and spaces,and will be more than 200 characters long.
(一首Haiku体诗只包含小写字母,反斜杠,空格,并且不会超过200个字符)
题目条件:
each line has the correct number of syllables (5/7/5).(每行的正确音节数量(5/7/5))
every contiguous sequence of one or more vowels counts as one syllable.(连续的元音字母算作一个音节)
the vowels are a, e, i, o, u,and y.(元音字母包括a,e,i,o,u, y)
Every word will contain at least one syllable.(每个单词至少包括一个音节)
输出:
if all three lines have the correct number of syllables, output 'Y'(如果符合Haiku诗体的要求,输出‘Y’)
if the haiku is not correct, you must output the number of the first line that has the wrong number of syllables.(如果不符合诗体要求,输出第一行不对的诗句的行号)
3、算法设计
1、从一首Haiku中找出三条诗句的开始位置和结束位置
2、统计一条诗句中的音节个数,该问题可转化为统计非元音向元音的过渡点的个数,即某个位置,其本身是元音,左边相邻的是非元音。
3、根据统计结果,输出相应的答案
4、编程方式
数据结构和变量:
String haiku 用来存储一首Haiku诗
start1, end1 第一条诗句的开始位置下标和结束位置下标
start2, end2
start3, end3
操作:
int getSyllablesNum() // 统计一条诗句中音节的个数
5、代码(Java 描述)
import java.util.Arrays; import java.util.Scanner; public class Main { public static String vowels = "aeiouy"; public static boolean isVowel(char ch) { return vowels.indexOf(ch) != -1; } public static int getSyllablesNum(String haiku, int start, int end) { int syllables_num = 0; for(int i = start; i <= end; i ++) { if(isVowel(haiku.charAt(i)) == true && (i == start || isVowel(haiku.charAt(i - 1)) == false)) { syllables_num ++; } } return syllables_num; } public static void main(String[] args) { String haiku; Scanner cin = new Scanner(System.in); while(true) { haiku = cin.nextLine(); if(haiku.equals("e/o/i")) { break; } int start1, end1, start2, end2, start3, end3; start1 = 0; end1 = haiku.indexOf('/') - 1; start2 = end1 + 2; end2 = haiku.indexOf('/', start2) - 1; start3 = end2 + 2; end3 = haiku.length() - 1; if(getSyllablesNum(haiku, start1, end1) != 5) { System.out.println(1); }else if(getSyllablesNum(haiku, start2, end2) != 7) { System.out.println(2); }else if(getSyllablesNum(haiku, start3, end3) != 5) { System.out.println(3); }else { System.out.println("Y"); } } } }
6、友情推荐