题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
private static Map<Integer,String> map;
private static Map<Integer,String> map1;
public static void main(String[] args) throws IOException {
map=new HashMap<>();
map1=new HashMap<>();
map.put(2,"拾");
map.put(3,"佰");
map.put(4,"仟");
map.put(5,"万");
map.put(6,"拾万");
map.put(7,"佰万");
map.put(8,"仟万");
map.put(9,"亿");
map.put(10,"拾亿");
map.put(11,"佰亿");
map.put(12,"仟亿");
map1.put(1,"壹");
map1.put(2,"贰");
map1.put(3,"叁");
map1.put(4,"肆");
map1.put(5,"伍");
map1.put(6,"陆");
map1.put(7,"柒");
map1.put(8,"捌");
map1.put(9,"玖");
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String res="人民币";
String money=sc.nextLine();
String[] arr=money.split("\\.");
String pre=read(arr[0])+"元";
if(pre.startsWith("壹")&&pre.length()>1&&pre.charAt(1)=='拾'){
pre=pre.substring(1);
}
if(pre.length()==1){
pre="";
}
if(arr.length==1){
System.out.println(res+pre+"整");
}
else{
String s=arr[1];
String next="";
if(map1.containsKey(Integer.valueOf(s.substring(0,1)))){
next+=map1.get(Integer.valueOf(s.substring(0,1)))+"角";
}
if(map1.containsKey(Integer.valueOf(s.substring(1,2)))){
next+=map1.get(Integer.valueOf(s.substring(1,2)))+"分";
}
System.out.println(res+pre+next);
}
}
}
private static String read(String money){
if(!map.containsKey(money.length())){
if(money.equals("0")){
return "";
}
else{
return map1.get(Integer.valueOf(money));
}
}
else if(money.charAt(0)=='0'){
int index=0;
for(int i=1;i<=money.length()-1;i++){
if(money.charAt(i)!='0'){
index=i;
break;
}
}
return "零"+read(money.substring(index));
}
String danwei=map.get(money.length());
int count=Integer.valueOf(money.substring(0,1));
int len=1;
if(danwei.length()>1){
if(danwei.startsWith("仟")){
len=4;
}
else if(danwei.startsWith("佰")){
len=3;
}
else if(danwei.startsWith("拾")){
len=2;
}
}
String diff=money.substring(len);
boolean b=true;
for(int i=0;i<=diff.length()-1;i++){
if(diff.charAt(i)!='0'){
b=false;
}
}
if(b){
return read(money.substring(0,len))+danwei.substring(danwei.length()-1,danwei.length());
}
String pre=read(money.substring(0,len))+danwei.substring(danwei.length()-1,danwei.length());
String next=read(diff);
return pre+next;
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
private static Map<Integer,String> map;
private static Map<Integer,String> map1;
public static void main(String[] args) throws IOException {
map=new HashMap<>();
map1=new HashMap<>();
map.put(2,"拾");
map.put(3,"佰");
map.put(4,"仟");
map.put(5,"万");
map.put(6,"拾万");
map.put(7,"佰万");
map.put(8,"仟万");
map.put(9,"亿");
map.put(10,"拾亿");
map.put(11,"佰亿");
map.put(12,"仟亿");
map1.put(1,"壹");
map1.put(2,"贰");
map1.put(3,"叁");
map1.put(4,"肆");
map1.put(5,"伍");
map1.put(6,"陆");
map1.put(7,"柒");
map1.put(8,"捌");
map1.put(9,"玖");
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String res="人民币";
String money=sc.nextLine();
String[] arr=money.split("\\.");
String pre=read(arr[0])+"元";
if(pre.startsWith("壹")&&pre.length()>1&&pre.charAt(1)=='拾'){
pre=pre.substring(1);
}
if(pre.length()==1){
pre="";
}
if(arr.length==1){
System.out.println(res+pre+"整");
}
else{
String s=arr[1];
String next="";
if(map1.containsKey(Integer.valueOf(s.substring(0,1)))){
next+=map1.get(Integer.valueOf(s.substring(0,1)))+"角";
}
if(map1.containsKey(Integer.valueOf(s.substring(1,2)))){
next+=map1.get(Integer.valueOf(s.substring(1,2)))+"分";
}
System.out.println(res+pre+next);
}
}
}
private static String read(String money){
if(!map.containsKey(money.length())){
if(money.equals("0")){
return "";
}
else{
return map1.get(Integer.valueOf(money));
}
}
else if(money.charAt(0)=='0'){
int index=0;
for(int i=1;i<=money.length()-1;i++){
if(money.charAt(i)!='0'){
index=i;
break;
}
}
return "零"+read(money.substring(index));
}
String danwei=map.get(money.length());
int count=Integer.valueOf(money.substring(0,1));
int len=1;
if(danwei.length()>1){
if(danwei.startsWith("仟")){
len=4;
}
else if(danwei.startsWith("佰")){
len=3;
}
else if(danwei.startsWith("拾")){
len=2;
}
}
String diff=money.substring(len);
boolean b=true;
for(int i=0;i<=diff.length()-1;i++){
if(diff.charAt(i)!='0'){
b=false;
}
}
if(b){
return read(money.substring(0,len))+danwei.substring(danwei.length()-1,danwei.length());
}
String pre=read(money.substring(0,len))+danwei.substring(danwei.length()-1,danwei.length());
String next=read(diff);
return pre+next;
}
}