当前位置: 首页 > news >正文

273. Integer to English Words

273. Integer to English Words

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.

将非负整数转换为英语表示方式。

分析:

在汉语中计数方式比较简单,各个位上的数字都是一样(一、二、。。。九),在将各位上的单位(十、百、千等)补上即可。英语略有不同,首先是0~20,30、40、。。。90均为不同的单词,且英语中没有汉语中的万和亿,取而代之的是million(百万)、billion(十亿)。

依次求出各位上的数(十亿、百万、千、百、十位(20~90)、十几(11~19)、个位(0~9)),用英语表示出来,并加上相应的单位即可。

代码如下:

string numberToWords(int num)
{
	map<long long, string> digit_to_english_map;
	digit_to_english_map.insert(make_pair(90, "Ninety"));
	digit_to_english_map.insert(make_pair(80, "Eighty"));
	digit_to_english_map.insert(make_pair(70, "Seventy"));
	digit_to_english_map.insert(make_pair(60, "Sixty"));
	digit_to_english_map.insert(make_pair(50, "Fifty"));
	digit_to_english_map.insert(make_pair(40, "Forty"));
	digit_to_english_map.insert(make_pair(30, "Thirty"));
	digit_to_english_map.insert(make_pair(20, "Twenty"));
	digit_to_english_map.insert(make_pair(19, "Nineteen"));
	digit_to_english_map.insert(make_pair(18, "Eighteen"));
	digit_to_english_map.insert(make_pair(17, "Seventeen"));
	digit_to_english_map.insert(make_pair(16, "Sixteen"));
	digit_to_english_map.insert(make_pair(15, "Fifteen"));
	digit_to_english_map.insert(make_pair(14, "Fourteen"));
	digit_to_english_map.insert(make_pair(13, "Thirteen"));
	digit_to_english_map.insert(make_pair(12, "Twelve"));
	digit_to_english_map.insert(make_pair(11, "Eleven"));
	digit_to_english_map.insert(make_pair(10, "Ten"));
	digit_to_english_map.insert(make_pair(9, "Nine"));
	digit_to_english_map.insert(make_pair(8, "Eight"));
	digit_to_english_map.insert(make_pair(7, "Seven"));
	digit_to_english_map.insert(make_pair(6, "Six"));
	digit_to_english_map.insert(make_pair(5, "Five"));
	digit_to_english_map.insert(make_pair(4, "Four"));
	digit_to_english_map.insert(make_pair(3, "Three"));
	digit_to_english_map.insert(make_pair(2, "Two"));
	digit_to_english_map.insert(make_pair(1, "One"));
	digit_to_english_map.insert(make_pair(0, "Zero"));

	long long _num = num;
	int coef = 0;
	string result;
	if (_num >= 1000000000)
	{
		coef = int(_num / 1000000000);
		result += numberToWords(coef) + " Billion";
		_num %= 1000000000;
		if (_num == 0)
			return result;
		result += " ";
	}
	if (_num < 1000000000 && _num >= 1000000)
	{
		coef = int(_num / 1000000);
		result += numberToWords(coef) + " Million";
		_num %= 1000000;
		if (_num == 0)
			return result;
		result += " ";
	}
	if (_num < 1000000 && _num >= 1000)
	{
		coef = int(_num / 1000);
		result += numberToWords(coef) + " Thousand";
		_num %= 1000;
		if (_num == 0)
			return result;
		result += " ";
	}
	if (_num < 1000 && _num >= 100)
	{
		coef = int(_num / 100);
		result += digit_to_english_map[coef] + " Hundred";
		_num %= 100;
		if (_num == 0)
			return result;
		result += " ";
	}
	if (_num < 100 && _num > 20)
	{
		coef = int(_num / 10);
		coef *= 10;
		result += digit_to_english_map[coef];
		_num %= coef;
		if (_num == 0)
			return result;
		result += " ";
	}
	if (_num <= 20)
		result += digit_to_english_map[_num];
	return result;
}

  

posted on 2018-03-19 12:56 bigpotato 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/big-potato/p/8601343.html

相关文章:

  • DevOps是一种文化,不是角色!
  • 00004、python+selenium 的三种时间等待方式
  • SQL SERVER存储过程一
  • HTML中nbsp; ensp; emsp; thinsp;等6种空白空格的区别
  • P1280 尼克的任务
  • 动态代理和AOP
  • php 实现SFTP上传文件
  • WordCount项目总结
  • 2017校招真题在线编程- 网易 合唱团
  • BZOJ 1260: [CQOI2007]涂色paint【区间DP】
  • Java浅克隆和深克隆
  • C#注册OCX控件
  • Hibernate 一对一关联映射,mappedBy参数解析
  • range.FormulaR1C1属性
  • java学习--基础知识进阶第十天--笔记
  • Google 是如何开发 Web 框架的
  • JavaScript对象详解
  • Java精华积累:初学者都应该搞懂的问题
  • js中forEach回调同异步问题
  • Node 版本管理
  • PV统计优化设计
  • react-native 安卓真机环境搭建
  • vagrant 添加本地 box 安装 laravel homestead
  • vue.js框架原理浅析
  • vue-router的history模式发布配置
  • 阿里云前端周刊 - 第 26 期
  • 爱情 北京女病人
  • 构建工具 - 收藏集 - 掘金
  • 回顾2016
  • 批量截取pdf文件
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 深入浏览器事件循环的本质
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • Salesforce和SAP Netweaver里数据库表的元数据设计
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • ​什么是bug?bug的源头在哪里?
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (14)学习笔记:动手深度学习(Pytorch神经网络基础)
  • (39)STM32——FLASH闪存
  • (C#)获取字符编码的类
  • (rabbitmq的高级特性)消息可靠性
  • (TOJ2804)Even? Odd?
  • (规划)24届春招和25届暑假实习路线准备规划
  • (解决办法)ASP.NET导出Excel,打开时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致
  • (六)软件测试分工
  • (切换多语言)vantUI+vue-i18n进行国际化配置及新增没有的语言包
  • (十二)springboot实战——SSE服务推送事件案例实现
  • (学习日记)2024.04.10:UCOSIII第三十八节:事件实验
  • **登录+JWT+异常处理+拦截器+ThreadLocal-开发思想与代码实现**
  • .bat文件调用java类的main方法
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .NET MVC 验证码
  • .NET 常见的偏门问题
  • .Net(C#)常用转换byte转uint32、byte转float等