题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
unordered_map<int,string> dict={
{1,"壹"},
{2,"贰"},
{3,"叁"},
{4,"肆"},
{5,"伍"},
{6,"陆"},
{7,"柒"},
{8,"捌"},
{9,"玖"},
{10,"拾"},
{11,"拾壹"},
{12,"拾贰"},
{13,"拾叁"},
{14,"拾肆"},
{15,"拾伍"},
{16,"拾陆"},
{17,"拾柒"},
{18,"拾捌"},
{19,"拾玖"}
};
string func1(int num)
{
if(num<20)
{
return dict[num];
}
else if(num<100)
{
if(num%10==0) //个位数为0,即是10的整数倍
return dict[num/10]+dict[10];
else //个位数不是0
return dict[num/10]+dict[10]+func1(num%10);
}
else if(num<1000)
{
if(num%100==0) //即百位数上不为0,但是十位数和个位数上都为0
return dict[num/100]+"佰";
else if(num%100 >= 10) //即十位上的数字不为0
return func1(num/100)+"佰"+func1(num%100);
else //即十位为零,且各位不为零
return func1(num/100)+"佰零"+func1(num%100);
}
else if(num<10000)
{
if(num%1000==0)
return func1(num/1000)+"仟";
else if(num%1000 >= 100) //即百位不为零
return func1(num/1000)+"仟"+func1(num%1000);
else
return func1(num/1000)+"仟零"+func1(num%1000);
}
else if(num<100000000)
{
if(num%10000==0)
return func1(num/10000)+"万";
else if(num%10000 >= 1000)
return func1(num/10000)+"万"+func1(num%10000);
else
return func1(num/10000)+"万零"+func1(num%10000);
}
else
{
if(num%100000000==0)
return func1(num/100000000)+"亿";
else if(num%100000000 >= 10000000)
return func1(num/100000000)+"亿"+func1(num%100000000);
else
return func1(num/100000000)+"亿零"+func1(num%100000000);
}
}
void func2(string str)
{
if(str == "00")
{
cout << "整";
return;
}
if (str[0] > '0')
{
cout << dict[str[0]-'0'] << "角";
}
if (str[1] > '0')
{
cout << dict[str[1]-'0'] << "分";
}
return;
}
int main()
{
double num;
cin>>num;
string str=to_string(num);
int index=str.find('.');
string str1=str.substr(0,index),str2=str.substr(index+1);
//输出人民币
cout << "人民币";
//输出元钱
if(str[0]!='0')
{
cout<<func1(stoi(str))<<"元";
}
//输出角分零钱
func2(str2);
return 0;
}
#include <string>
#include <unordered_map>
using namespace std;
unordered_map<int,string> dict={
{1,"壹"},
{2,"贰"},
{3,"叁"},
{4,"肆"},
{5,"伍"},
{6,"陆"},
{7,"柒"},
{8,"捌"},
{9,"玖"},
{10,"拾"},
{11,"拾壹"},
{12,"拾贰"},
{13,"拾叁"},
{14,"拾肆"},
{15,"拾伍"},
{16,"拾陆"},
{17,"拾柒"},
{18,"拾捌"},
{19,"拾玖"}
};
string func1(int num)
{
if(num<20)
{
return dict[num];
}
else if(num<100)
{
if(num%10==0) //个位数为0,即是10的整数倍
return dict[num/10]+dict[10];
else //个位数不是0
return dict[num/10]+dict[10]+func1(num%10);
}
else if(num<1000)
{
if(num%100==0) //即百位数上不为0,但是十位数和个位数上都为0
return dict[num/100]+"佰";
else if(num%100 >= 10) //即十位上的数字不为0
return func1(num/100)+"佰"+func1(num%100);
else //即十位为零,且各位不为零
return func1(num/100)+"佰零"+func1(num%100);
}
else if(num<10000)
{
if(num%1000==0)
return func1(num/1000)+"仟";
else if(num%1000 >= 100) //即百位不为零
return func1(num/1000)+"仟"+func1(num%1000);
else
return func1(num/1000)+"仟零"+func1(num%1000);
}
else if(num<100000000)
{
if(num%10000==0)
return func1(num/10000)+"万";
else if(num%10000 >= 1000)
return func1(num/10000)+"万"+func1(num%10000);
else
return func1(num/10000)+"万零"+func1(num%10000);
}
else
{
if(num%100000000==0)
return func1(num/100000000)+"亿";
else if(num%100000000 >= 10000000)
return func1(num/100000000)+"亿"+func1(num%100000000);
else
return func1(num/100000000)+"亿零"+func1(num%100000000);
}
}
void func2(string str)
{
if(str == "00")
{
cout << "整";
return;
}
if (str[0] > '0')
{
cout << dict[str[0]-'0'] << "角";
}
if (str[1] > '0')
{
cout << dict[str[1]-'0'] << "分";
}
return;
}
int main()
{
double num;
cin>>num;
string str=to_string(num);
int index=str.find('.');
string str1=str.substr(0,index),str2=str.substr(index+1);
//输出人民币
cout << "人民币";
//输出元钱
if(str[0]!='0')
{
cout<<func1(stoi(str))<<"元";
}
//输出角分零钱
func2(str2);
return 0;
}