蓝桥杯基础练习【报时助手】

资源限制

时间限制:1.0s   内存限制:512.0MB

问题描述

给定当前的时间,请用英文的读法将它读出来。
时间用时h和分m表示,在英文的读法中,读一个时间的方法是:
如果m为0,则将时读出来,然后加上“o'clock”,如3:00读作“three o'clock”。
如果m不为0,则将时读出来,然后将分读出来,如5:30读作“five thirty”。
时和分的读法使用的是英文数字的读法,其中0~20读作:
0:zero, 1: one, 2:two, 3:three, 4:four, 5:five, 6:six, 7:seven, 8:eight, 9:nine, 10:ten, 11:eleven, 12:twelve, 13:thirteen, 14:fourteen, 15:fifteen, 16:sixteen, 17:seventeen, 18:eighteen, 19:nineteen, 20:twenty。
30读作thirty,40读作forty,50读作fifty。
对于大于20小于60的数字,首先读整十的数,然后再加上个位数。如31首先读30再加1的读法,读作“thirty one”。
按上面的规则21:54读作“twenty one fifty four”,9:07读作“nine seven”,0:15读作“zero fifteen”。

输入格式

输入包含两个非负整数h和m,表示时间的时和分。非零的数字前没有前导0。h小于24,m小于60。

输出格式

输出时间时刻的英文。

样例输入

0 15

样例输出

zero fifteen

提示:
1.先分好m为0或不为0
2.如果m为0,则将时读出来,然后加上“o'clock”,如3:00读作“three o'clock”,不需要读取分,分用“o'clock”替代。
3.如果m不为0,则将时读出来,然后将分读出来,如5:30读作“five thirty”,m不为0时分两部分:(1):求时 (2):求分  (3):注意空格
同时注意:对于大于20的数字,首先读整十的数,然后再加上个位数 

(1)求时【0-24】: 分为小于10,和大于等于10。  

大于等于10又分为小于等于20【20-24】和小于20【10-20】

(2)求分【0-20】:分为小于等于20【0-20】,大于20【20-60】。

小于等于20时又分为:小于等于10【0-10】,大于10(10-20】

 (3)空格:

                当m==0时:

                                1.在每次拼接好的字符串后面添加空格

                当m!=0时:

                                1.首先在求时拼接字符串的后面添加一个空格。因为:(时【空格】分)

                                2.求时或分对于大于20的数字中间也要添加空格。因为:(先读十位数在读个位数)

                                3.求时除了2加空格,其他都不用加 因为:(时【空格】分【此次无空格】)

import java.util.Scanner;
 
public class Main{
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 /**
		    * 试题 基础练习 报时助手
		  */
		Scanner sc = new Scanner(System.in);
//		时h 和 分m
		int h = sc.nextInt();
		int m = sc.nextInt();
		
		//0-20: 一共21个数【下标为0-20】
		String[] arrh = {"zero","one","two","three","four","five","six","seven","eight","nine","ten",
				         "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"};
		//0 10 20 30 40 50 【下标为0-5】
		String[] arrs = {"zero","ten","twenty","thirty","forty","fifty"};
		
		//定义结果字符串
		String time = "";
		
		 // 如果m为0,则将时读出来,然后加上“o'clock”,如3:00读作“three o'clock”。
		if(m == 0) {
			//时单位的数
			if(h > 20) {
				time += arrh[h / 10] + arrh[h % 10]+" ";
			}else {
				time += arrh[h]+" ";
			}
			//分单位的数
			time += "o'clock";
				
//	如果m不为0,则将时读出来,然后将分读出来,如5:30读作“five thirty”。		
		}else if(m != 0) {
			//这个if是求时单位的
			//小于十的数字
			if(h < 10) {
			    time += arrh[h]+" ";
			}else {
				// 大于等于10的数字
				//小于等于20的数字
				if(h <= 20) { 
					time += arrh[h]+ " ";
				}else {
//					对于大于20的数字,首先读整十的数,然后再加上个位数
					time += arrs[h / 10] +" "+ arrh[h % 10]+" ";
				}
			}
			//这个if是求分单位的
			//当分小于等于20时
			if(m <= 20) {
				//当分大于10时
				if(m > 10) {
					time += arrh[m];
				}else {
				//当分小于10时
					time += arrh[m];
				}
			}else {
				//当分大于20时
//				对于大于20的数字,首先读整十的数,然后再加上个位数
				time += arrs[m / 10] +" " + arrh[m % 10];
			}
		}
			
		//输出结果
		System.out.println(time);
	}
 
}


#推荐算法实习##蓝桥杯#
全部评论

相关推荐

1 1 评论
分享
牛客网
牛客企业服务