码迷,mamicode.com
首页 > 其他好文 > 详细

完美世界在线笔试编程题2

时间:2016-04-26 00:21:02      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

将一个四位数的整数,输出它的拼音形式。如1001,输出“yiqianlingyi”,10,输出“10”

 

  

// wamie2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<map>
#include<string>
#include<iostream>
using namespace std;

int main()
{
    int number = 0;
    map<int, string> ismap1;
    ismap1[0] = "ling";
    ismap1[1] = "yi";
    ismap1[2] = "er";
    ismap1[3] = "san";
    ismap1[4] = "si";
    ismap1[5] = "wu";
    ismap1[6] = "liu";
    ismap1[7] = "qi";
    ismap1[8] = "ba";
    ismap1[9] = "jiu";
    ismap1[10] = "shi";
    map<int, string> ismap2;
    ismap2[0] = "";
    ismap2[1] = "shi";
    ismap2[2] = "bai";
    ismap2[3] = "qian";
    string temp, ret;
    while (cin >> number)
    {
        int cnt = -1;
        ret = "";
        bool tag = true;
        if (number < 20)
        {
            if (0 == number)
                cout << "ling" << endl;
            else
            {
                while (0 != number)
                {
                    ++cnt;
                    temp = "";
                    int i = number % 10;
                    if (1 == i && 1 == cnt)
                        temp = ismap1[10];
                    else
                        temp = ismap1[i] + ismap2[cnt];
                    ret = temp.append(ret);
                    number /= 10;
                }
                if (ret.size() >= 5 && "ling" == ret.substr(ret.size() - 4, 4))
                    ret = ret.substr(0, ret.size() - 4);
                cout << ret << endl;
            }
        }
        else
        {
            while (0 != number)
            {
                ++cnt;
                temp = "";
                int i = number % 10;
                if (true == tag && 0 == i)
                {
                    temp = ismap1[i];
                    tag = false;
                }
                else
                {
                    if (0 != i)
                    {
                        temp = ismap1[i] + ismap2[cnt];
                        tag = true;
                    }
                }
                ret = temp.append(ret);
                number /= 10;
            }
            if (ret.size() >= 5 && "ling" == ret.substr(ret.size() - 4, 4))
                ret = ret.substr(0, ret.size() - 4);
            cout << ret << endl;
        }
        
    }
    return 0;
}

 

完美世界在线笔试编程题2

标签:

原文地址:http://www.cnblogs.com/csudanli/p/5433212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!