解题4(NumberToEnglish )
题目描述
Jessi初学英语,为了快速读出一串数字,编写程序将数字转换成英文:
如22:twenty two,123:one hundred and twenty three。
说明:
数字为正整数,长度不超过九位,不考虑小数,转化结果为英文小写;
输出格式为twenty two;
非法数据请返回“error”;
关键字提示:and,billion,million,thousand,hundred。
方法原型:public static String parse(long num)
输入描述:
输入一个long型整数
输出描述:
输出相应的英文写法
输入
2356
输出
two thousand three hundred and fifty six
代码如下:
package com.yzh.hehe;
import java.util.Scanner;
public class NumberToEnglish {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
while (scanner.hasNext()) {
long num=scanner.nextLong();
System.out.println(parse(num));
}
scanner.close();
}
public static String parse(long num){
String[] geweiArr={"one","two","three","four","five","six","seven","eight","nine" };
String[] shiweiArr={"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
String[] shiduoArr={"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
String numString=String.valueOf(num);
// 要求最多不超过九位数,不够九位的前面填0
for (int i = numString.length(); i <9; i++) {
numString="0"+numString;
}
StringBuilder stringBuilder=new StringBuilder();
String million=numString.substring(0,3);//以1000(thousand)每三位为一个部分(个十百)来处理
int miBaiInt=Integer.parseInt(million.substring(0, 1));
boolean flag=false;//判断是否加and的标志
//处理三位组成的数当中的百位
if (miBaiInt!=0) {
flag=true;
stringBuilder.append(geweiArr[miBaiInt-1]+" hundred");
}
int miShiInt=Integer.parseInt(million.substring(1,2));//三位组成的数当中的十位
int migeInt=Integer.parseInt(million.substring(2));//三位组成的数当中的个位
if ((miShiInt!=0||migeInt!=0)&&flag) {
stringBuilder.append(" and ");
}
//十位为1时十位和个位可同时处理,否则,先处理十位,在处理个位
if (miShiInt==1) {
int temp=Integer.parseInt(million.substring(1));//十位个位同事处理
stringBuilder.append(shiduoArr[temp-10]);
}
//十位,个位分别处理
else {
flag=false;
if(miShiInt!=0){
flag=true;
stringBuilder.append(shiweiArr[miShiInt-2]);
}
if (flag&&migeInt!=0) {
stringBuilder.append(" "+geweiArr[migeInt-1]);
}else if(migeInt!=0){
stringBuilder.append(geweiArr[migeInt-1]);
}
}
int length=stringBuilder.length();//判断是否存在million和thousand
if (length!=0) {
stringBuilder.append(" million ");
}
String thousand=numString.substring(3,6);//处理第二个三位组成的部分
int thBaiInt=Integer.parseInt(thousand.substring(0,1));
flag=false;
if (thBaiInt!=0) {
flag=true;
stringBuilder.append(geweiArr[thBaiInt-1]+" hundred");
}
int thShiInt=Integer.parseInt(thousand.substring(1,2));
int thGeInt=Integer.parseInt(thousand.substring(2));
if (flag&&(thShiInt!=0||thGeInt!=0)) {
stringBuilder.append(" and ");
}
if (thShiInt==1) {
int temp=Integer.parseInt(thousand.substring(1));
stringBuilder.append(shiduoArr[temp-10]);
}else {
flag=false;
if (thShiInt!=0) {
flag=true;
stringBuilder.append(shiweiArr[thShiInt-2]);
}
if (flag&&thGeInt!=0) {
stringBuilder.append(" "+geweiArr[thGeInt-1]);
}else if (thGeInt!=0) {
stringBuilder.append(geweiArr[thGeInt-1]);
}
}
if (stringBuilder.length()>length) {
stringBuilder.append(" thousand ");
}
String bsg=numString.substring(6);//处理第三个三位组成的部分
int bsgBaiInt=Integer.parseInt(bsg.substring(0,1));
flag=false;
if (bsgBaiInt!=0) {
flag=true;
stringBuilder.append(geweiArr[bsgBaiInt-1]+" hundred");
}
int bsgShiInt=Integer.parseInt(bsg.substring(1,2));
int bsgGeInt=Integer.parseInt(bsg.substring(2));
if (flag&&(bsgShiInt!=0||bsgGeInt!=0)) {
stringBuilder.append(" and ");
}
if (bsgShiInt==1) {
int temp=Integer.parseInt(bsg.substring(1));
stringBuilder.append(shiduoArr[temp-10]);
}else {
flag=false;
if (bsgShiInt!=0) {
flag=true;
stringBuilder.append(shiweiArr[bsgShiInt-2]);
}
if (flag&&(bsgGeInt!=0)) {
stringBuilder.append(" "+geweiArr[bsgGeInt-1]);
}else if (bsgGeInt!=0) {
stringBuilder.append(geweiArr[bsgGeInt-1]);
}
}
return stringBuilder.toString();
}
}
解题4(NumberToEnglish )的更多相关文章
- SCNU ACM 2016新生赛决赛 解题报告
新生初赛题目.解题思路.参考代码一览 A. 拒绝虐狗 Problem Description CZJ 去排队打饭的时候看到前面有几对情侣秀恩爱,作为单身狗的 CZJ 表示很难受. 现在给出一个字符串代 ...
- SCNU ACM 2016新生赛初赛 解题报告
新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划
[BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...
- CH Round #56 - 国庆节欢乐赛解题报告
最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...
- wechall.net/stegano 解题心得
/* 转载请注明出处:http://www.cnblogs.com/Martinium/p/wechall_stegano.html */ 最近迷上了 www.wechall.net 网站,里面都是些 ...
- Mountains(CVTE面试题)解题报告
题目大意: 用一个数组代表群山的高度.高度大的地方代表山峰,小的地方代表山谷.山谷可以容水.假设有一天下了大雨,求群山中总共可以容纳多少水? 如图所示情况,a代表该数组,总共可以容纳5个水. 解题思路 ...
- timus 1180. Stone Game 解题报告
1.题目: 1180. Stone Game Time limit: 1.0 secondMemory limit: 64 MB Two Nikifors play a funny game. The ...
- timus 1175. Strange Sequence 解题报告
1.题目描述: 1175. Strange Sequence Time limit: 1.0 secondMemory limit: 2 MB You have been asked to disco ...
随机推荐
- JS 变量是否有值的判断
var node; …… 判断 node 是否有值,是否为 undefine,是否 null,直接使用两个!!,否定之否定: if (!!node){ .... }else{ .... } 这个条件判 ...
- yii表单输入元素
InputElement http://www.yiichina.com/api/CFormInputElement CFormInputElement 可以代表以下基于type属性的表单输入类型: ...
- zabbix监控java日志文件
zabbix监控日志文件 https://blog.csdn.net/workdsz/article/details/78439230?utm_source=blogxgwz2
- 电脑IP设置
方法一: echo offecho 修改[本地连接]IP......netsh interface IP set address "本地连接" static 138.8.8.111 ...
- PC浏览器播放m3u8
HLS(HTTP Live Streaming)是苹果公司针对iPhone.iPod.iTouch和iPad等移动设备而开发的基于HTTP协议的流媒体解决方案.在 HLS 技术中 Web 服务器向客户 ...
- 机器学习进阶-图像形态学操作-开运算与闭运算 1.cv2.morphologyEx(进行各类形态学变化) 2.op=cv2.MORPH_OPEN(先腐蚀后膨胀) 3.op=cv2.MORPH_CLOSE(先膨胀后腐蚀)
1.cv2.morphologyEx(src, op, kernel) 进行各类形态学的变化 参数说明:src传入的图片,op进行变化的方式, kernel表示方框的大小 2.op = cv2.MO ...
- 页面中onclick事件引号问题
第一种:html中onclick调用事件 <p id="txt" onclick="changeSize()">加括弧的changeSize()&l ...
- copyOnWriteArray 并发包下的不安全(数组)集合
copyOnWriteArray 记录一下 package java.util.concurrent;//你没有看错,是这个包 private transient volatile Object[] ...
- vue:图片切换动态显示
<img :src="切换条件 ? require('xxx.png') : require('xxx.png')" />
- 文件操作 freopen函数
转自http://blog.csdn.net/zhuyi2654715/article/details/6963673 当我们求解acm题目时,通常在设计好算法和程序后,要在调试环境(例如VC等)中运 ...