题解 | #学英语#

学英语

https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc

看看应该就能直接看懂
全部评论
public class test { private static HashMap<integer> mapping = new HashMap<integer>(); static { mapping.put(100, "hundred"); mapping.put(90, "ninety"); mapping.put(80, "eighty"); mapping.put(70, "seventy"); mapping.put(60, "sixty"); mapping.put(50, "fifty"); mapping.put(40, "forty"); mapping.put(30, "thirty"); mapping.put(20, "twenty"); mapping.put(19, "nineteen"); mapping.put(18, "eighteen"); mapping.put(17, "seventeen"); mapping.put(16, "sixteen"); mapping.put(15, "fifteen"); mapping.put(14, "fourteen"); mapping.put(13, "thirteen"); mapping.put(12, "twelve"); mapping.put(11, "eleven"); mapping.put(10, "ten"); mapping.put(9, "nine"); mapping.put(8, "eight"); mapping.put(7, "seven"); mapping.put(6, "six"); mapping.put(5, "five"); mapping.put(4, "four"); mapping.put(3, "three"); mapping.put(2, "two"); mapping.put(1, "one"); mapping.put(0, ""); } public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String rs = new String(""); long num = Long.parseLong(in.next()); long base = num % 1000L; // under 1 thousand part rs = assemble(rs, base, ""); long caculate = num / 1000L; if (caculate == 0L) { System.out.println(rs); continue; } // has more than thousand long thou = caculate % 1000L; // thousand part rs = assemble(rs, thou, "thousand"); caculate = caculate / 1000L; if (caculate == 0L) { System.out.println(rs); continue; } // has more than million long mill = caculate % 1000L; // mill part rs = assemble(rs, mill, "million"); caculate = caculate / 1000L; if (caculate == 0L) { System.out.println(rs); continue; } // has more than billion long billion = caculate % 1000L; // billion part rs = assemble(rs, billion, "billion"); System.out.println(rs); } } private static String assemble(String rs, long l, String subfix) { if (!rs.equals("")) { rs = " " + rs; } if (l < 10L) { rs = mapping.get((int)l) + " " + subfix + rs; return rs; } if(l > 10 && l < 20){ rs = mapping.get((int)l%100) + " " + subfix + rs; return rs; } if (l < 100L) { String temp = mapping.get((int)l % 10).equals("")?"": " " + mapping.get((int)l % 10); rs = mapping.get((int)l / 10 * 10) + temp + " " + subfix + rs; return rs; } String temp = mapping.get((int)(l / 100)) + " hundred "; if(l % 100/10 == 0L && l % 100 % 10 == 0L){ return temp + subfix + rs; } if(l % 100/10 == 0L && l % 100 % 10 != 0L){ return temp + "and " + mapping.get((int)(l % 10)) + " " + subfix + rs; } if(l % 100 > 10 && l % 100 < 20){ return temp + "and " + mapping.get((int)l%100) + " " + subfix + rs; } rs = temp + "and " + mapping.get((int)(l % 100 / 10 * 10L)) + " " + mapping.get((int)(l %100 % 10)) + " " + subfix + rs; rs.trim(); return rs; } }</integer></integer>
点赞 回复 分享
发布于 2022-10-05 11:19 湖南
抱歉了 是在牛客第一次发布 以为自动提交代码区的
点赞 回复 分享
发布于 2022-10-05 11:20 湖南

相关推荐

想润的芹菜人狠话不多:把其中一个老总放中间都会得罪另一个
点赞 评论 收藏
分享
Noob1024:一笔传三代,人走笔还在
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务