题解 | #人民币转换#
人民币转换
http://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
C++解题
#include<iostream> #include<string> #include<vector> using namespace std; string arr[]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖","拾","佰","仟","元","万","亿","角","分"}; string Func(int a,int b,vector<int> &v){ string str=""; int index=b-a; int index1=0; if(index==0){ if(v[a]==0) return str; return arr[v[a]]; }else{ for(int i=0;i<=index;++i){ if(index1==4) return str; if(index1!=0&&v[a+i]!=0){ str+="零"; index1=0; } if(v[a+i]!=0){ if((9+index-i)==10&&v[a+i]==1) str+="拾"; else if(index==i) str+=arr[v[a+i]]; else str+=arr[v[a+i]]+arr[9+index-i]; }else index1++; } } return str; } int main() { string str; while(cin>>str){ int len=str.length(); int pos=0; vector<int> v; for(int i=0;i<len;++i){ if(str[i]!='.') v.push_back(str[i]-'0'); else pos=i; } string str1; string str2; if(v[len-3]!=0){ str1=arr[v[len-3]]+"角"; if(v[len-2]!=0) str1+=arr[v[len-2]]+"分"; }else{ if(v[len-2]!=0) str1+=arr[v[len-2]]+"分"; else str1="整"; } if(v[0]!=0){ int len1=len-4; //0-len1为小数点前面的数 for(int i=len1/4;i>=0;--i){ if(i==len1/4) str2+=Func(0,len1-4*i,v)+arr[13+i]; else str2+=Func(len1-4*i-3,len1-4*i,v)+arr[13+i]; } } cout<<"人民币"+str2+str1<<endl; } return 0; }