论坛首页 Java企业应用论坛

几个笔试题目(2010-09-18)

浏览 23221 次
精华帖 (1) :: 良好帖 (3) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-09-21   最后修改:2010-09-21
根据楼主的需求,加了个判断:

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("");//参数转存到数组里
		boolean  flg = true;
		for(int i=0;i<strMoney.length();i++){
			if(Integer.parseInt(inMoney[1+i])==0){
				if(flg){
				total=total+Money[Integer.parseInt(inMoney[1+i])];
				flg = false;
				}
			}else{
				total=total+Money[Integer.parseInt(inMoney[1+i])]+Util[strMoney.length()-i-1];
				flg = true;
			}
		}
		total = total.toString().trim();
		return total;
	}

	public static void main(String[] args) {	
		System.out.println(translateMoney(100023201));
	}		

}


输出结果:一亿零二万三千二百零一

继续修改

0 请登录后投票
   发表时间:2010-09-21  
夜如此的寒 写道
根据楼主的需求,加了个判断:

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++){
			if(Integer.parseInt(inMoney[1+i])==0){
				total=total+Money[Integer.parseInt(inMoney[1+i])];
			}else{
				total=total+Money[Integer.parseInt(inMoney[1+i])]+Util[strMoney.length()-i-1];
			}
		}
                total = total.toString().trim();
		return total;
	}

	public static void main(String[] args) {	
		System.out.println(translateMoney(1023201));
	}		

}


输出结果:一百零二万三千二百零一


兄弟,你这个不行啊。考虑问题要周到些。呵呵。多个0在一起呢?比如10023201,测试又有问题了,结果输出:一千零零二万三千二百零一
0 请登录后投票
   发表时间:2010-09-21  
yangguo 写道
针对你提的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


呵呵。看样子还是我那种最古老的实现方式比较可靠。呵呵。
200000000这么多0还真没有考虑到。呵呵。
0 请登录后投票
   发表时间:2010-09-21  
a881127b 写道
你钱数转换为中文的程序有个很大的错误:
你的标记Flag用的static变量,要是程序中多次调用这个转换类的话怎么办??

一个测试用例:
public static void main(String[] args) throws Exception{  
        // 省略  
    	System.out.println(translate(1234,4));
    	System.out.println(translate(1234,4));
    }  


结果:
一千二百三十四
一千二百三十四一千二百三十四

哦,谢谢提醒,写的时候没有考虑这种问题,还有多线程问题也没有考虑,当时只考虑实现功能。
0 请登录后投票
   发表时间:2010-09-21  
polaris1119 写道


呵呵。看样子还是我那种最古老的实现方式比较可靠。呵呵。
200000000这么多0还真没有考虑到。呵呵。


你的比较低效,也不好扩展的。

扩展到更高位就体现出优势了。

package com.test;

public class MoneyTrans {
	
	private static String[] ChinaDigit = {"零","一","二","三","四","五","六","七","八","九"};
	private static String[] UNIT = {"","","十","百","千"};
	private static String[] BIGUNIT = {"","万","亿","兆"};
	private static long MAX = 10000000000000000L - 1;
	private char[] digit;
	public String trans(long n) throws Exception{
		if(n > MAX){
			throw new Exception("数字过大,最多可处理到千兆位");
		}
		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();
	}
	
	private  String partTrans(int start, int end) {
		StringBuffer buff = new StringBuffer();
		boolean isPreDigitZero = false;
		for (int i = start; i < end; i++) {
			int cur = digit[i] - '0';
			if(cur != 0 ){
				if(isPreDigitZero){
					buff.append(ChinaDigit[0]);
				}
				buff.append(ChinaDigit[cur] + UNIT[end - i]);
				isPreDigitZero = false;
			}
			else {			
				isPreDigitZero = true;
			}			
		}		
		return buff.toString();		
	}

	public static void main(String[] args) throws Exception {		
		MoneyTrans transtor = new MoneyTrans();    
		String test1 = transtor.trans(9000000000000000L);
		String test2 = transtor.trans(9000000000000001L);
		System.out.println(test1);
		System.out.println(test2);
	}

}




0 请登录后投票
   发表时间:2010-09-21   最后修改:2010-09-21
利用中午的时间写了一下,主要使用了正则表达式和BigDecimal中一些方法,效率没有考虑,但自认为代码还是比较易懂的. 大家帮忙检查下,看有没有问题
import java.math.BigDecimal;

public class TestMoney {
	public static final String[] ms = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
	public static final String[] ds = {"圆","拾", "佰", "仟", "万", "拾", "佰", "仟", "亿","拾", "佰", "仟", "兆","拾", "佰", "仟"};
	public static final String[] xs = {"角","分"};	

	/**
	 * 
	 * @param money 考虑到整数太小,使用了BigDecimal,本来字符串也可以,但懒的再改了
	 * @param useZhao 是否以兆为单位,true 表示单位为'兆' false表示'兆'将以'万亿'表示
	 * @return
	 */
	public static String convert(BigDecimal money, boolean useZhao) {
		// 转换小数部分
		BigDecimal xiao = money.subtract(money.setScale(0,BigDecimal.ROUND_DOWN));
		boolean isXiao = !xiao.equals(BigDecimal.ZERO);
		StringBuilder sb3 = new StringBuilder();
		String xiaoStr = "";
		if(isXiao) {
			int i = 0;
			do {
				xiao = xiao.multiply(BigDecimal.TEN);				
				sb3.append(ms[xiao.setScale(0, BigDecimal.ROUND_DOWN).intValue()]);
				sb3.append(xs[i]);
				i++;
				xiao = xiao.subtract(xiao.setScale(0,BigDecimal.ROUND_DOWN));
			} while(i < xs.length);
			xiaoStr = sb3.toString()
				.replaceAll("(零[^角分])+", "零")
				.replaceAll("(零[角分])+$", "");
		}
		
		// 转换整数部分
		StringBuilder sb1 = new StringBuilder();		
		do {
			BigDecimal[] s = money.divideAndRemainder(BigDecimal.TEN);
			sb1.append(ms[s[1].intValue()]);
			money = s[0];
		} while(money.compareTo(BigDecimal.ZERO) > 0);
		StringBuilder sb2 = new StringBuilder();
		for (int i = 0; i < sb1.length(); i++) {
			sb2.append(ds[i]).append(sb1.charAt(i));
		}
		String tmp = sb2.reverse()
			.append((xiaoStr.length()>0)?xiaoStr:"整")
			//中间有'零佰','零仟'的都替换为'零'
			.toString().replaceAll("(零[^亿万圆])+", "零") 	
			//中间有'零亿', '零零亿','零零万','零零元'的都替换掉零,不包括兆|亿|万|圆
			//等价于下面注释的四句
			.replaceAll("(零+)(?=兆|亿|万|圆)", "")	
//			.replaceAll("零+兆", "兆")
//			.replaceAll("零+亿", "亿")
//			.replaceAll("零+万", "万")
//			.replaceAll("零+圆", "圆")
			.replaceAll("亿万", "亿")
			.replaceAll("兆亿", "兆")
			.replaceAll("壹拾", "拾") ; // 按楼主说的,更符合习惯
		tmp = (useZhao)?tmp:tmp.replaceAll("兆", "万亿");
		System.out.println(tmp);
		return tmp;
	}
	
	public static void main(String[] args) {

		TestMoney.convert(new BigDecimal("1020.02"),true);
		TestMoney.convert(new BigDecimal("1020.00"),true);
		TestMoney.convert(new BigDecimal("1020.30"),true);
		TestMoney.convert(new BigDecimal("1020.44"),true);
		TestMoney.convert(new BigDecimal("109000"),true);
		TestMoney.convert(new BigDecimal("1234123456789"),true);
		TestMoney.convert(new BigDecimal("1234123456789"),false);
		TestMoney.convert(new BigDecimal("1010000100010"),true);
		TestMoney.convert(new BigDecimal("1010000100010"),false);
		TestMoney.convert(new BigDecimal("1000000000000"),true);
		TestMoney.convert(new BigDecimal("1000000000000"),false);
		TestMoney.convert(new BigDecimal("903003000000000"),false);
		TestMoney.convert(new BigDecimal("903003000000000"),true);
		
		
		
	}

}

0 请登录后投票
   发表时间:2010-09-23  
那个编程题没看明白 有人帮忙解释下么?
0 请登录后投票
   发表时间:2010-09-24   最后修改:2010-09-24
用了递归解也行
public class Test {
	
	private static String[] digit = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
	private static String[] unit = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", 
			"千", "万", "兆", "十", "百", "千", "万", "亿" };

	private static String convertInner(String money) {
		int length = money.length();
		int curNum = money.charAt(0) - '0';
		String result = "";
		if (length > 1) {
			String curUnit = unit[length - 2];
			result = convertInner(money.substring(1, length));
			if (curNum == 0) {
				if (money.charAt(1) != '0' && "万亿兆".indexOf(curUnit) == -1)
					result = "零" + result;
				else if ("万亿兆".indexOf(curUnit) != -1)
					result = curUnit + result;
			} else
				result = digit[curNum - 1] + curUnit + result;
		} else if (curNum != 0)
			result = digit[curNum - 1];
		return result;
	}

	public static String convert(String money) {
		String result = convertInner(money).replaceAll("亿万", "亿").replaceAll("兆万", "兆")
				.replaceAll("兆亿", "兆").replaceAll("一十", "十");
		return result.equals("") ? "零" : result;
	}

	public static void main(String[] args) {
		String result = "";
		String[] money = { "900000000020300" };
		String[] rightResult = { "九十兆零二万零三百" };
		for (int i = 0; i < money.length; i++) {
			result = convert(money[i]);
			System.out.println("[" + result.equals(rightResult[i]) + "] money = " + money[i]
					+ ", result = " + result);
		}
	}
}


0 请登录后投票
   发表时间:2010-09-24  
有难度呀!!!!!!!!
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics