博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一道于心不甘的编程题
阅读量:4171 次
发布时间:2019-05-26

本文共 4000 字,大约阅读时间需要 13 分钟。

上周五例会结束了,打算做到练习题放松下,于是选择了这道(人民币大写转换),一看这道题目觉得很简单,默默告诉在半个小时一定能搞定。如果你一听这道题也觉得很简单,说明你和我是在思想认识上是在同一个高度。

       整个题目意思是: 输入阿拉伯数字,然后翻译人民币大写。例如 1 1101 1101   壹忆壹仟壹佰零壹万 壹仟壹佰零壹 

      是不是很简单? 立马我分析设计:

     1. 定义大写对应的常量字符串数组和个十百仟的单位常量。

     2.把输入的数据字符串按照4,8进行分割,然后单独处理。

     3. 处理分割后的字符串,4-8之间字符串加万,8以上的加亿 

     4. 对于连续的是零的进行单独处理

接下来就开始编程了。5分钟过去了,30分钟过去了,1个小时过去了... 

被你猜对了,花了1半小时都没有搞定,最后放弃了,就回家了。搞得周五有点小郁闷。刚刚做了一下,算是搞定了。

package com.albertshao.study;import java.util.ArrayList;import java.util.List;/** *  * 问题: 把阿拉伯数字转换为大写, * 	// 1. store the constant variables of number and unit	 //2. handle the number below the thousand.	// 3. split the number per 4 space according to the length of number	// 4. handle the special condition such as the continuous zero * @author albertshao *  */public class RMBConverter {		private static final String NUMBER_UPCASE = "零壹贰叁肆伍陆柒捌玖";		private static final String[] RMB_UNIT = {"分","角","元","拾","佰","仟","万","亿" };		private static final String NUNBER_REGEX = "^[0-9]*$";		/**	 * 第二个四位表示万	 */	private static final Integer TENTHOUSAND_INDEX = 2;		/**	 * 第三个四位表示亿	 */	private static final Integer HUNDREDMILLION_INDEX = 3;		/**	 * 第四个四位表示亿	 */	private static final Integer MILLIONMILLION_INDEX = 4;	public static void main(String args[]) {		System.out.println("10021243:" + RMBConverter.convert(10021243));		System.out.println("1110201220012:" +RMBConverter.convert(1110201220012L));				}	    /**     * Convert the input number to the expected result.     * @param number     * @return     */	private static String convert(long number) {		String result = "";		List
numList = RMBConverter.splitNumberStr(String.valueOf(number)); if(!numList.isEmpty()) { int index = 1; for(String numStr : numList) { String temp = convertUpCase(numStr); if(index == TENTHOUSAND_INDEX) { if(temp.contains(RMB_UNIT[2])) { temp = temp.replace(RMB_UNIT[2], RMB_UNIT[6]); } else { temp += RMB_UNIT[6]; } } else if(index == HUNDREDMILLION_INDEX) { if(temp.contains(RMB_UNIT[2])) { temp = temp.replace(RMB_UNIT[2], RMB_UNIT[7]); } else { temp += RMB_UNIT[7]; } } else if(index == MILLIONMILLION_INDEX) { if(temp.contains(RMB_UNIT[2])) { temp = temp.replace(RMB_UNIT[2], RMB_UNIT[6]+RMB_UNIT[7]); } else { temp += (RMB_UNIT[6]+RMB_UNIT[7]); } } result = temp + result; index ++; } } return result; } /** * Convert upcase of number below thousand. * 1101 壹仟壹佰零壹元 * @param numberStr * @return */ private static String convertUpCase(String numberStr) { StringBuilder resSb = new StringBuilder(0); if (numberStr != null && !"".equals(numberStr)) { char[] arr = numberStr.toCharArray(); int count = arr.length; for (char ch : arr) { String charUpCase = String.valueOf(NUMBER_UPCASE .charAt(Character.getNumericValue(ch))); //4. handle the special condition such as including zero if (Character.getNumericValue(ch) == 0) { if(resSb.toString().endsWith("零")) { count--; continue; } else { resSb.append(charUpCase); } } else { resSb.append(charUpCase); resSb.append(RMB_UNIT[count + 1]); } count--; } } else { resSb.append(numberStr); } return resSb.toString(); } //3. split the number according to the length of number private static List
splitNumberStr(String numberStr) { List
numList = new ArrayList
(); if (numberStr != null && !"".equals(numberStr) && numberStr.matches(NUNBER_REGEX)) { while (numberStr.length() > 4) { String numCell = numberStr.substring(numberStr.length() - 4); numList.add(numCell); numberStr = numberStr.substring(0, numberStr.length() - 4); } numList.add(numberStr); } return numList; }}
测试结果

10021243:壹仟零贰万壹仟贰佰肆拾叁元1110201220012:壹万亿壹仟壹佰零贰亿零壹佰贰拾贰万零壹拾贰元

失败总结:

1. 对于具体设计没有考虑清楚,做编程题时候一定要清楚具体实现细节,考虑实现的可行性。

2. 不要在烦躁的情况下做题,比如周五,大家都想回家,一个人在那边编写代码,心里却想着早点回家,这样的效率往往受影响。所以不要强迫自己编程。玩时玩得痛快,学时学得踏实。

 

    

 

  

 

转载地址:http://rskai.baihongyu.com/

你可能感兴趣的文章
第七章SQL数据库开发--TSQL—事务和锁
查看>>
sqlserver服务器常用的性能计数器
查看>>
SQL数据库开发—1TSQL—第八章视图
查看>>
SQL数据库管理—第二章数据库硬件性能
查看>>
SQL数据库管理—IO存储
查看>>
SQL数据库管理—第四章使用windows性能监控器监控数据库
查看>>
5.1 查找未使用的索引
查看>>
5.3 查找缺失的索引
查看>>
9.1 索引概述-1
查看>>
SQL数据库开发—TSQL—第九章索引2
查看>>
9.4.5 非聚集索引建立的原则和方法-指针
查看>>
索引3 -索引设计的最佳实践and 索引失效
查看>>
SQL数据库开发--TSQL—10第十章游标
查看>>
NSQL-MongoDB数据库简介
查看>>
linux系统管理—第一章安装redhat7进行初始化设置使用
查看>>
linux系统管理—第二章Linux帮助命令and查看Linux服务器基本情况
查看>>
linux系统管理第三章Linux文件目录管理
查看>>
嵌入式系统程序设计基础1
查看>>
MDK的安装及基本使用以及proteus的基本使用
查看>>
编译器背后的故事
查看>>