题解 | #学英语#

学英语

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 湖南

相关推荐

不愿透露姓名的神秘牛友
11-20 19:57
已编辑
某大厂 golang工程师 23.0k*16.0, 2k房补,年终大概率能拿到
点赞 评论 收藏
分享
10-25 12:05
已编辑
湖南科技大学 Java
若梦难了:我有你这简历,已经大厂乱杀了
点赞 评论 收藏
分享
10-09 00:50
已编辑
长江大学 算法工程师
不期而遇的夏天:1.同学你面试评价不错,概率很大,请耐心等待;2.你的排名比较靠前,不要担心,耐心等待;3.问题不大,正在审批,不要着急签其他公司,等等我们!4.预计9月中下旬,安心过节;5.下周会有结果,请耐心等待下;6.可能国庆节前后,一有结果我马上通知你;7.预计10月中旬,再坚持一下;8.正在走流程,就这两天了;9.同学,结果我也不知道,你如果查到了也告诉我一声;10.同学你出线不明朗,建议签其他公司保底!11.同学你找了哪些公司,我也在找工作。
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务