当前位置: 代码迷 >> J2EE >> 关于Java的算法(经典面试题)解决思路
  详细解决方案

关于Java的算法(经典面试题)解决思路

热度:84   发布时间:2016-04-22 00:23:22.0
关于Java的算法(经典面试题)
在控制台输入数字(0、1、2、3、4、5、6、7、8、9、10……)
在控制台输出中文大写(零、壹、贰、叁、肆、伍、陆、柒、捌、玖、拾……)
eg:  
  101 --》壹佰零壹
  100010 --》拾万零壹拾

仰望高手【】

------解决方案--------------------
import java.util.Scanner;

public class ChNumber {

public String getInt(){
Scanner sc = new Scanner(System.in);
String str = null;
System.out.println("请输入一个整数:");
while(true){
str = sc.nextLine();
if(str.matches("\\d+")){
break;
}else{
System.out.println("你输入数的格式不正确,请重新输入一个整数:");
}
}
return String.valueOf(Long.parseLong(str));
}

public int get(char c){
String s = String.valueOf(c);
return Integer.parseInt(s);
}

public String change(String str){
char c[] = str.toCharArray();
String s = "";
int count = 0;
int temp;
String upper[] ={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
for(int i=c.length-1;i>=0;i--){
temp = get(c[i]);
count++;
if(count==2||(count%4)==2){
s = upper[temp]+"拾"+s;
}else if(count==3||(count%4)==3){
s = upper[temp]+"佰"+s;
}else if(count==4||(count%4)==0){
s = upper[temp]+"仟"+s;
}else if(count==5){
s = upper[temp]+"万"+s;
}else if(count==9){
s = upper[temp]+"亿"+s;
}else if(count==13){
s = upper[temp]+"兆"+s;
}else{
s = upper[temp]+s;
}
}
return s;
}

public static void main(String[] args) {
ChNumber cn = new ChNumber();
String s = cn.change(cn.getInt());
s = s.replaceAll("零[拾佰仟]", "零");
s = s.replaceAll("零{4}万", "零");
s = s.replaceAll("零{4}亿", "零");
s = s.replaceAll("零{4}兆", "零");
s = s.replaceAll("零{2,}", "零");
s = s.replaceAll("零万", "万");
s = s.replaceAll("零亿", "亿");
s = s.replaceAll("零兆", "兆");
s = s.replaceAll("零{2,}", "零");
if(s.endsWith("零")){
s = s.substring(0, s.length()-1);
}
System.out.println(s);
}

}

新手,乱写的,仅供参考
------解决方案--------------------
借鉴前人,写了个思路比较清晰的:
Java code
import java.math.BigDecimal;import java.text.DecimalFormat;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Convert_Number_Chinese {    public static final String[] Str_Number={"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };    public static final String[] Str_Location={ "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟" };          public static String getSign(String tempString) {//拿到符号        return tempString.indexOf("-")!=-1?"负":"";    }           public static String getInteger(String tempStr) {//拿到整数部分        String Str_Integer="";//去掉两端空格        tempStr=tempStr.trim();        if(tempStr.matches("^0+[.].+")) {            tempStr=tempStr.replaceFirst("^0+", "0");//e.g:  000.123改为0.123            return "零";        }        if(tempStr.matches("^0+")) {            return "零";//e.g:  00000        }        tempStr=tempStr.replaceFirst("^0+","");//e.g:  000233.3改为233.3        if(tempStr.indexOf(".")!=-1) {            Str_Integer=tempStr.substring(0,tempStr.indexOf("."));        }else {            Str_Integer=tempStr;        }        Str_Integer=new StringBuffer(Str_Integer).reverse().toString();        StringBuffer Str_Result=new StringBuffer();        for(int i=0;i<Str_Integer.length();i++) {            Str_Result.append(Str_Location[i]);            int index=Integer.parseInt(i==Str_Integer.length()-1?Str_Integer.substring(i):Str_Integer.substring(i,i+1));            Str_Result.append(Str_Number[index]);        }        Str_Result=Str_Result.reverse();        replace(Str_Result,"零仟零佰零拾零万","");        replace(Str_Result,"零拾","零");        replace(Str_Result,"零佰","零");        replace(Str_Result,"零仟","零");        replace(Str_Result,"零万","万");        replace(Str_Result,"零亿","亿");        replace(Str_Result,"零零","零");        //删除个位上的零        if(Str_Result.charAt(Str_Result.length()-1)=='零'&&Str_Result.length()!=1) {            Str_Result.deleteCharAt(Str_Result.length()-1);        }        if(Str_Result.toString().startsWith("壹拾")) {            replace(Str_Result,"壹拾","拾");//e.g:   14.23        }        return Str_Result.toString();    }    public static void replace(StringBuffer Str_Result,String Resource,String Replace_String) {//替换方法        if(Str_Result==null||Resource==null||Replace_String==null) {            return;        }        while(Str_Result.indexOf(Resource)!=-1) {            Str_Result=Str_Result.replace(Str_Result.indexOf(Resource),Str_Result.indexOf(Resource)+Resource.length(), Replace_String);        }    }                public static String getDecimal(String tempString) {//拿到小数部分        String Str_Decimal="";        if(tempString.indexOf(".")==-1||tempString.matches(".+[.]0+$")) {//e.g:  000123   000123.000            return "";        }        Pattern p=Pattern.compile("(.+[.][1-9]+)(0+)$");        Matcher m=p.matcher(tempString);        if(m.matches()) {            tempString=new StringBuffer(tempString).delete(m.group(1).length(),m.group(1).length()+m.group(2).length()).toString();        }//e.g:   000123.45000       tempString=tempString.replace("0+$","");        Str_Decimal=tempString.substring(tempString.indexOf(".")+1);        StringBuffer Str_Result_Dot=new StringBuffer(Str_Decimal.length());        for(int i=0;i<Str_Decimal.length();i++) {            int index=Integer.parseInt(i==Str_Decimal.length()-1?Str_Decimal.substring(i):Str_Decimal.substring(i,i+1));            Str_Result_Dot.append(Str_Number[index]);        }        return "点"+Str_Result_Dot.toString();    }            public static String Number_Chinese(double tempNumber) {//转换double类型的数        DecimalFormat df=new DecimalFormat("#.#########");        String Str_Temp=String.valueOf(df.format(tempNumber));        StringBuffer Str_End=new StringBuffer(getSign(Str_Temp)+getInteger(Str_Temp)+getDecimal(Str_Temp));        return Str_End.toString();    }            public static String Number_Chinese(BigDecimal tempNumber) {//转换BigDecimal类型的数        return Number_Chinese(tempNumber.doubleValue());    }            public static String Number_Chinese(String tempNumber) {//转换String类型的数        return new StringBuffer(getSign(tempNumber)+getInteger(tempNumber)+getDecimal(tempNumber)).toString();    }            public static void main(String[] args) {        System.out.println(Number_Chinese(0001230400567.893400));        System.out.println(Number_Chinese(300000000));        System.out.println(Number_Chinese("00300000000"));        System.out.println(Number_Chinese("0021000230100.003"));    }}
  相关解决方案