c_cpp http://www.zhihu.com/question/27229082/answer/35757023

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp http://www.zhihu.com/question/27229082/answer/35757023相关的知识,希望对你有一定的参考价值。

#include <map>
#include <string>
#include <fstream> 
 
std::string num2Chinese(int num, std::map<int, std::string> &map) {
    std::string ret;
    bool twenty{num > 20};
    bool remnant{num > 100 && num/10%10 == 0};
    for (auto it=map.crbegin(); it != map.crend(); ++it) {
        int count=0;
        for (; num >= it->first; num -= it->first)
            ++count;
        if (!count) continue;
        if (it->first >= 10 && (count != 1 || twenty))
            ret += map[count] + map[it->first];
        else 
            ret += remnant ? "零" + map[it->first] : map[it->first];
    } 
    return ret;
} 
 
int main()
{
    std::map<int, std::string> map = {{1, "一"},{2, "二"},{3, "三"},{4, "四"},{5, "五"},{6, "六"},{7, "七"},
    {8, "八"},{9, "九"},{10, "十"},{100, "百"},{1000, "千"},{10000, "万"}};
    
    std::ofstream ofs("sorry.txt");
    for (int i=1; i<=100000; ++i)
        ofs << "对不起 第" << num2Chinese(i, map) << "遍" << std::endl;
}

以上是关于c_cpp http://www.zhihu.com/question/27229082/answer/35757023的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 127.单词阶梯

c_cpp MOFSET

c_cpp MOFSET

c_cpp 31.下一个排列

c_cpp string→char *

c_cpp 54.螺旋矩阵