题解 | #人民币转换#
人民币转换
http://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
import java.util.*;
public class Main{
static String[] dx = {"壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
static String[] dw = {"拾", "佰", "仟"};
static String[] fs = {"角", "分"};
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String str = in.next();
String[] money = str.split("\\.");
String res = "人民币";
int len = money[0].length()/8;
int index = money[0].length()%8;
if(!money[0].equals("0")){
for(int i=0; i<=len; i++){
if(i == 0){
res = res + yyx(money[0].substring(0, index));
} else {
res = res + yyx(money[0].substring(index+8*(i-1), index+8*i));
}
if(len>0&&i<len){
res = res + "亿";
} else {
res = res + "元";
}
}
}
res = res + xsd(money[1]);
System.out.println(res);
}
}
public static String qyx(String str){
if(str.isEmpty()) return null;
int len = str.length();
String res = "";
for(int i=0; i<len; i++){
int tem = Integer.parseInt(str.substring(i, i+1));
if(tem == 0){
if(i == 0 || (str.charAt(i-1)!='0' && i!=len-1)){
res = res + "零";
}
}else{
if(tem == 1 && ((len > 2 && i==len - 2 && str.charAt(i-1)=='0') || (len == 2&&i==0))){
res = res + dw[len - i - 2];
} else if(i == len -1){
res = res + dx[tem -1];
} else {
res = res + dx[tem -1] + dw[len - i - 2];
}
}
}
return res;
}
public static String yyx(String str){
String res = "";
if(str.length() > 4){
String wys = str.substring(0, str.length() - 4);
String wyx = str.substring(str.length() - 4);
res = res + qyx(wys) + "万" + qyx(wyx);
} else {
res = qyx(str);
}
return res;
}
public static String xsd(String str){
int len = str.length();
String res ="";
for(int i=0; i<len; i++){
int tem = Integer.parseInt(str.substring(i, i+1));
if(tem != 0){
res = res + dx[tem-1] + fs[i];
}
}
if(res.isEmpty()) res = res + "整";
return res;
}
}
public class Main{
static String[] dx = {"壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
static String[] dw = {"拾", "佰", "仟"};
static String[] fs = {"角", "分"};
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String str = in.next();
String[] money = str.split("\\.");
String res = "人民币";
int len = money[0].length()/8;
int index = money[0].length()%8;
if(!money[0].equals("0")){
for(int i=0; i<=len; i++){
if(i == 0){
res = res + yyx(money[0].substring(0, index));
} else {
res = res + yyx(money[0].substring(index+8*(i-1), index+8*i));
}
if(len>0&&i<len){
res = res + "亿";
} else {
res = res + "元";
}
}
}
res = res + xsd(money[1]);
System.out.println(res);
}
}
public static String qyx(String str){
if(str.isEmpty()) return null;
int len = str.length();
String res = "";
for(int i=0; i<len; i++){
int tem = Integer.parseInt(str.substring(i, i+1));
if(tem == 0){
if(i == 0 || (str.charAt(i-1)!='0' && i!=len-1)){
res = res + "零";
}
}else{
if(tem == 1 && ((len > 2 && i==len - 2 && str.charAt(i-1)=='0') || (len == 2&&i==0))){
res = res + dw[len - i - 2];
} else if(i == len -1){
res = res + dx[tem -1];
} else {
res = res + dx[tem -1] + dw[len - i - 2];
}
}
}
return res;
}
public static String yyx(String str){
String res = "";
if(str.length() > 4){
String wys = str.substring(0, str.length() - 4);
String wyx = str.substring(str.length() - 4);
res = res + qyx(wys) + "万" + qyx(wyx);
} else {
res = qyx(str);
}
return res;
}
public static String xsd(String str){
int len = str.length();
String res ="";
for(int i=0; i<len; i++){
int tem = Integer.parseInt(str.substring(i, i+1));
if(tem != 0){
res = res + dx[tem-1] + fs[i];
}
}
if(res.isEmpty()) res = res + "整";
return res;
}
}