题解 | #链表排序#
人民币转换
http://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String numStr=in.nextLine();
int splitResult[]=splitDoubleNum(numStr);
int intValue=splitResult[0];
int dotFenValue=splitResult[1];
StringBuilder resultBuilder=new StringBuilder();
resultBuilder.append("人民币");
int wanValue=10000;
int yiValue=wanValue*wanValue;
int qianValue=1000;
if(intValue>=yiValue){
int curValue=intValue/yiValue;
resultBuilder.append(getChineseWhenIsSmallTenThousand(curValue)+"亿");
curValue=(intValue-curValue*yiValue)/wanValue;
resultBuilder.append(getChineseWhenIsSmallTenThousand(curValue)+"万");
}
if(intValue>=wanValue){
int curValue=intValue/yiValue;
if(intValue>=yiValue&&curValue<qianValue){
resultBuilder.append(getChineseByBasicNum(0));
}
curValue=(intValue-curValue*yiValue)/wanValue;
resultBuilder.append(getChineseWhenIsSmallTenThousand(curValue)+"万");
}
int curValue=intValue%wanValue;
if(intValue>=wanValue&&curValue<qianValue){
resultBuilder.append(getChineseByBasicNum(0));
}
resultBuilder.append(getChineseWhenIsSmallTenThousand(curValue));
if(intValue>0){
resultBuilder.append("元");
}
//如果有小数
if(dotFenValue>0){
resultBuilder.append(getLastDotToChinese(dotFenValue));
}else{
resultBuilder.append("整");
}
System.out.println(resultBuilder.toString());
}
}
public static String getLastDotToChinese(int dotFenValue){
String result="";
if(dotFenValue>=10){
int curValue=dotFenValue/10;
result=result+getChineseByBasicNum(curValue)+"角";
}
int curValue=dotFenValue%10;
if(curValue>0){
result=result+getChineseByBasicNum(curValue)+"分";
}
return result;
}
public static String getChineseWhenIsSmallTenThousand(int value){
//将万以内的四位数,转化为中文
String eachValue[]={"","拾","佰","仟"};
StringBuilder stringBuilder=new StringBuilder();
int numArr[]=splitTenThoundsNum(value);
for(int i=0;i<numArr.length;i++){
int eachIndex=numArr.length-i-1;
if(eachIndex==1&&numArr[i]==1){
stringBuilder.append(eachValue[eachIndex]);
}else{
if(numArr[i]!=0) {
stringBuilder.append(getChineseByBasicNum(numArr[i]) + eachValue[eachIndex]);
}else{
//遇到0的时候,判断是否读0 ,则判断后面的是否都是0,如果都是0,则读0
//如果包含非零的则不读0
boolean hasNotZeroNum=false;
for(int j=i+1;j<numArr.length;j++){
if(numArr[j]!=0){
hasNotZeroNum=true;
break;
}
}
//如果遇到了0,并且后面的数字包含非零的数字,并且之前没加零,则读0
//否则其他情况一概不读
if(hasNotZeroNum){
if(stringBuilder.indexOf("零")==-1){
stringBuilder.append("零");
}
}
}
}
}
return stringBuilder.toString();
}
public static String getChineseByBasicNum(int num){
String chineseBasicStrArr[]={"零","壹","贰","叁","肆",
"伍","陆","柒","捌","玖"};
return chineseBasicStrArr[num];
}
public static int[] splitDoubleNum(String numStr){
int result[]={0,0};
//说明有小数
if(numStr.indexOf(".")>-1){
String numSplitArr[]=numStr.split("[.]");
result[0]=Integer.parseInt(numSplitArr[0]);
result[1]=Integer.parseInt(numSplitArr[1]);
if(numSplitArr[1].length()==1){
result[1]=result[1]*10;
}
}else{
result[0]=Integer.parseInt(numStr);
}
return result;
}
public static int[] splitTenThoundsNum(int num){
String numStr=String.valueOf(num);
int result[]=new int[numStr.length()];
for(int i=0;i<result.length;i++){
result[i]=numStr.charAt(i)-'0';
}
return result;
}
}