ZOJ 1713 (Haiku Review)解题报告

来源:互联网 发布:ubuntu查看硬盘总大小 编辑:程序博客网 时间:2024/06/04 08:48

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、友情推荐


==========================================友情推荐=========================================================
给大家推荐一个很吊的网站(钱宝网),个人感觉和阿里巴巴的余额宝类似,不过收益大约是余额宝的10~20倍。收益用来充话费
是足够了。并且注册就送6.6元,可立马提现。本人其身体验,保证网站真实可信。点击此处,查看详情
===========================================================================================================






0 0
原创粉丝点击