锁定老帖子 主题:几个笔试题目(2010-09-18)
精华帖 (1) :: 良好帖 (3) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-09-20
beipiaoonline 写道 第一题这样行不行:
十箱一块称,然后一箱一箱的往下拿,因为除了一箱少的,其余的都是一样重的 你这样相当于称了很多次吧 |
|
返回顶楼 | |
发表时间:2010-09-20
小弟也来献丑了,下面是编程题的代码,望各位DX赐教,谢谢!
public class ConvertToChiness { private static String[] chineseMapStr = {"零","一","二","三","四","五","六","七","八","九"}; private static String[] specailStr0 = {"千","百","十"}; private static String[] specailStr1 = {"亿","万"}; public static void main(String[] args){ System.out.println(convert("2512359321")); } private static String convert(String numberStr){ StringBuffer buffer=new StringBuffer(); List<String> list=new ArrayList<String>(); for(int i=0,j=numberStr.length();i<numberStr.length();i++,j--){ int num=Integer.parseInt(numberStr.substring(i,i+1)); String chinessNum=chineseMapStr[num]; String unit=getUnit(j); if(chinessNum.equals("零")){ if(!list.isEmpty()){ String chinessNumInList=list.get(list.size()-1); if(chinessNumInList.equals("零")){ list.remove(list.size()-1); } } } list.add(chinessNum); if(!StringUtils.isEmpty(unit)){ if(chinessNum.equals("零")){ if(ArrayUtils.contains(specailStr0, unit)){ }else if(ArrayUtils.contains(specailStr1, unit)){ list.remove(list.size()-1); list.add(unit); } }else{ list.add(unit); } } } for(String num:list){ buffer.append(num); } return buffer.toString(); } /** * 位数表示,如亿、万等 * @param digit * @return */ private static String getUnit(int digit) { String unit = ""; switch(digit) { case 12: case 8: case 4: unit = "千"; break; case 11: case 7: case 3: unit = "百"; break; case 10: case 6: case 2: unit = "十"; break; case 9: unit = "亿"; break; case 5: unit = "万"; break; } return unit; } } |
|
返回顶楼 | |
发表时间:2010-09-20
呵呵,注释加的不错。
|
|
返回顶楼 | |
发表时间:2010-09-20
public class NumberToChinese { private static final String[] genericUnit = {"","十","百","千","万","十","百","千","亿","十","百","千","兆","十兆","百兆","千兆"}; private static final String[] numberic = {"零","一","二","三","四","五","六","七","八","九"}; public static String tranform(String number){ StringBuffer buffer = new StringBuffer(); int len = number.length(); for(int i=0; i<len; i++){ int n = Integer.parseInt(number.substring(i,i+1)); if(n==0){ if(!buffer.toString().endsWith(numberic[0])){ buffer.append(numberic[n]); } }else{ buffer.append(numberic[n]); buffer.append(genericUnit[len-i-1]); } } return normalize(buffer.toString()); } private static String normalize(String s){ return s.replaceAll("^"+numberic[0]+"*(.*?)"+numberic[0]+"*$", "$1"); } public static void main(String[] args) { System.out.println(tranform("0006560000000"));; } } |
|
返回顶楼 | |
发表时间:2010-09-20
public class NumberToChinese { private static final String[] genericUnit = {"","十","百","千","万","十","百","千","亿","十","百","千","兆","十兆","百兆","千兆"}; private static final String[] numberic = {"零","一","二","三","四","五","六","七","八","九"}; public static String tranform(String number){ StringBuffer buffer = new StringBuffer(); int len = number.length(); for(int i=0; i<len; i++){ int n = Integer.parseInt(number.substring(i,i+1)); if(n==0){ if(!buffer.toString().endsWith(numberic[0])){ buffer.append(numberic[n]); } }else{ buffer.append(numberic[n]); buffer.append(genericUnit[len-i-1]); } } return normalize(buffer.toString()); } private static String normalize(String s){ return s.replaceAll("^"+numberic[0]+"*(.*?)"+numberic[0]+"*$", "$1"); } public static void main(String[] args) { System.out.println(tranform("0006560000000"));; } } |
|
返回顶楼 | |
发表时间:2010-09-20
最后修改:2010-09-20
小弟献丑了:20行代码。粗粗的写了一下。。别骂我
public class Test { public static String translateMoney(int money) { String total = "";//返回的字符串 String[] Money = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九"}; String[] Util = { " ","十", "百", "千", "万", "十", "百", "千", "亿"}; String strMoney = String.valueOf(money);//参数转字符串 String[] inMoney =strMoney.toString().trim().split("");//参数转存到数组里 for(int i=0;i<strMoney.length();i++){ total=total+Money[Integer.parseInt(inMoney[1+i])]+Util[strMoney.length()-i-1]; } return total; } public static void main(String[] args) { System.out.println(translateMoney(999999999)); } } 运行结果:九亿九千九百九十九万九千九百九十九 |
|
返回顶楼 | |
发表时间:2010-09-20
luobin23628 写道 public class NumberToChinese { private static final String[] genericUnit = {"","十","百","千","万","十","百","千","亿","十","百","千","兆","十兆","百兆","千兆"}; private static final String[] numberic = {"零","一","二","三","四","五","六","七","八","九"}; public static String tranform(String number){ StringBuffer buffer = new StringBuffer(); int len = number.length(); for(int i=0; i<len; i++){ int n = Integer.parseInt(number.substring(i,i+1)); if(n==0){ if(!buffer.toString().endsWith(numberic[0])){ buffer.append(numberic[n]); } }else{ buffer.append(numberic[n]); buffer.append(genericUnit[len-i-1]); } } return normalize(buffer.toString()); } private static String normalize(String s){ return s.replaceAll("^"+numberic[0]+"*(.*?)"+numberic[0]+"*$", "$1"); } public static void main(String[] args) { System.out.println(tranform("0006560000000"));; } } 这个还是有点问题哦,比如用800098测试,结果却是八十零九十八,我还在研究,呵呵,刚刚看到 |
|
返回顶楼 | |
发表时间:2010-09-20
夜如此的寒 写道 小弟献丑了:20行代码。粗粗的写了一下。。别骂我
public class Test { public static String translateMoney(int money) { String total = "";//返回的字符串 String[] Money = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九"}; String[] Util = { " ","十", "百", "千", "万", "十", "百", "千", "亿"}; String strMoney = String.valueOf(money);//参数转字符串 String[] inMoney =strMoney.toString().trim().split("");//参数转存到数组里 for(int i=0;i<strMoney.length();i++){ total=total+Money[Integer.parseInt(inMoney[1+i])]+Util[strMoney.length()-i-1]; } return total; } public static void main(String[] args) { System.out.println(translateMoney(999999999)); } } 运行结果:九亿九千九百九十九万九千九百九十九 呵呵。你这个太粗糙了。测试用例更粗糙。这个主要要考虑0的问题,测试数字中一个0也没有。我随便拿一个带0的就出问题了。1023201,输出:一百零十二万三千二百零十一 |
|
返回顶楼 | |
发表时间:2010-09-21
针对你提的bug,修正如下:
public String trans(int n){ StringBuffer buff = new StringBuffer(); boolean isPrePartZero = false; digit = String.valueOf(n).toCharArray(); int length = digit.length; int pos = (length - 1)/4; int headLength = (length - 1)%4 + 1; // xxxxxxxxxx --> xx|xxxx|xxxx buff.append(partTrans(0,headLength) + BIGUNIT[pos--]); for (int i = headLength;i < length ; i = i + 4) { String part = partTrans(i , i + 4); if(part.length() == 0){ isPrePartZero = true; }else{ if(isPrePartZero && !part.startsWith(ChinaDigit[0])){ buff.append(ChinaDigit[0]); } buff.append(part + BIGUNIT[pos]) ; isPrePartZero = false; } pos--; } return buff.toString(); } 你改的方案过不了: 200000000 |
|
返回顶楼 | |
发表时间:2010-09-21
你钱数转换为中文的程序有个很大的错误:
你的标记Flag用的static变量,要是程序中多次调用这个转换类的话怎么办?? 一个测试用例: public static void main(String[] args) throws Exception{ // 省略 System.out.println(translate(1234,4)); System.out.println(translate(1234,4)); } 结果: 一千二百三十四 一千二百三十四一千二百三十四 |
|
返回顶楼 | |