Blue Bridge Cup VIP Time Report Assistant

Problem Description

Given the current time, please read it out in English.
  Time is expressed in hours h and minutes m. In English reading, the way to read a time is:
  if m is 0, then read the time, then add "o'clock", such as 3:00 read "three" o'clock ".
  If m is not 0, then read out the time, and then read out the points, such as 5:30 read "five thirty"
  Hours and minutes are pronounced in English numerals, 0-20 is pronounced as:
  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 is pronounced thirty, 40 is forty, and 50 is fifty.
  For numbers greater than 20 and less than 60, first read the whole ten, then add the single digits. For example, 31 first reads 30 and then adds 1, which is read as "thirty one".
  According to the above rule, 21:54 reads "twenty one fifty four", 9:07 reads "nine seven", and 0:15 reads "zero fifteen".

Input format

The input contains two non-negative integers h and m, which represent the hours and minutes of time. Non-zero numbers have no leading zeros. h is less than 24, m is less than 60.

Output format

English output time and time.

Sample input

0 15

Sample output

zero fifteen

Problem-solving code

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

public class Main {

	static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] args) {
		
		Map<Integer, String> maps = new HashMap<Integer, String>();
		maps.put(1, "");
		String str = "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";
		
		String [] strArr = str.split(",");
		
		for (int i = 0; i < strArr.length; i++) {
			String str2Arr[] = strArr[i].split(":");
			maps.put(Integer.valueOf(str2Arr[0]),str2Arr[1]);
		}
		
		//输入日期
		int h = sc.nextInt();//小于24(小时)
		int m = sc.nextInt();//小于60(分)
		
		//判断是否符合规则
		if(h<=24 && m<=60) {
			
			String strH="";
			String strM="";
			
			if(h<=20) {
				strH = maps.get(h);
			}else if(h>20) {
				strH = maps.get(20) +" "+ maps.get(h-20);
			}
			
			//如果是整数就直接返回
			if(m==20 || m==30 || m==40 || m==50) {
				strM = maps.get(m);
			}else if(m<20) {
				//m=19?
				strM = maps.get(m);
			}else if(m>20 && m<30) {
				//m=29?
				//29-20=9
				strM = maps.get(20)+" " + maps.get(m-20);
			}else if(m>30 && m<40) {
				//m=329?
				//39-30=9
				strM = maps.get(30)+" " + maps.get(m-30);
			}else if(m>40 && m<50) {
				//m=49?
				//49-40=9
				strM = maps.get(40)+" " + maps.get(m-40);
			}else if(m>50 && m<60) {
				//m=59?
				//59-50=9
				strM = maps.get(50) +" "+ maps.get(m-50);
			}
			
			System.out.println(strH+" "+strM);
		}
		
		
		
		
//		for (Integer key : maps.keySet()) {
//			System.out.println(key+"--->"+maps.get(key));
//		}
		
	}

	
}

发布了186 篇原创文章 · 获赞 3233 · 访问量 24万+

Guess you like

Origin blog.csdn.net/qq_17623363/article/details/105626333