题解 | #人民币转换# 递归
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
#include <iostream>
using namespace std;
string arr[]={"","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
#define ll long long
string deal(ll x) //整数部分
{
if(x>=1e8) return deal(x/(ll)(1e8))+"亿"+((x%(ll)(1e8)<(ll)1e7&&x%(ll)(1e8)>0)?"零":"")+deal(x%(ll)(1e8));
if(x>=1e4) return deal(x/(ll)(1e4))+"万"+((x%(ll)(1e4)<(ll)1e3&&x%(ll)(1e4)>0)?"零":"")+deal(x%(ll)(1e4));
if(x>=1e3) return deal(x/(ll)(1e3))+"仟"+((x%(ll)(1e3)<(ll)1e2&&x%(ll)(1e3)>0)?"零":"")+deal(x%(ll)(1e3));
if(x>=1e2) return deal(x/(ll)(1e2))+"佰"+((x%(ll)(1e2)<(ll)1e1&&x%(ll)(1e2)>0)?"零":"")+deal(x%(ll)(1e2));
if(x>=1e1) return ((x/(ll)(1e1))>1?deal(x/(ll)(1e1)):"")+"拾"+deal(x%(ll)(1e1));
return arr[x];
}
string deal_x(ll x) //小数部分
{
if(x==0) return "整";
return arr[x/10]+(x/10>0?"角":"")+((x%10)>0?(arr[x%10]+"分"):"");
}
int main() {
string x;
cin>>x;
ll zheng=stoi(x.substr(0,x.find("."))); //提取整数
ll xiao=stoi(x.substr(x.find(".")+1)); //提取小数
cout<<"人民币"+deal(zheng)+(zheng>0?"元":"")+deal_x(xiao);
return 0;
}
// 64 位输出请用 printf("%lld")
