完美世界第二题:模拟读数字

本文介绍了一个将数字转换为中文词语的算法实现,包括百位、十位和个位的处理逻辑,以及如何通过字符串匹配完成转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

测试用例:

102:yibailinger

11:shiyi

1:yi

111:yibaiyishiyi

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 /************************************************************************/
 5 /*                       模拟读数字                                       */
 6 /************************************************************************/
 7 void readNumber(const int &number)
 8 {
 9     string result="";
10     vector<string> match(10);
11     match[0] = "ling";match[1] = "yi";match[2] = "er";match[3] = "san";
12     match[4] = "si";match[5] = "wu";match[6] = "liu";match[7] = "qi";
13     match[8] = "ba";match[9] = "jiu";
14     int baiwei = number/100;
15     int shiwei = number%100/10;
16     int gewei = number%10;
17     if (baiwei != 0)
18     {
19         result = match[baiwei];
20         result+="bai";
21         if (shiwei != 0)
22         {
23             result+=match[shiwei];
24             result+="shi";
25         }
26         else
27             result+=match[shiwei];
28         if (gewei != 0)
29             result+=match[gewei];
30     }
31     else
32     {
33         if (shiwei != 0)
34         {
35             if(shiwei != 1)
36                 result = match[shiwei];
37             result+="shi";
38         }
39         if (gewei != 0)
40             result+=match[gewei];
41     }
42     for (int i=0;i<result.size();i++)
43         cout<<result[i];
44     cout<<endl;
45 }
46 int main()
47 {
48     int number;
49     while(cin>>number)
50         readNumber(number);
51     system("pause");
52     return 0;
53 }

 

转载于:https://www.cnblogs.com/keleshaoye/p/5432983.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值