题目描述

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型整数

输出描述:

输出相应的英文写法

示例1

输入

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 )的更多相关文章

  1. SCNU ACM 2016新生赛决赛 解题报告

    新生初赛题目.解题思路.参考代码一览 A. 拒绝虐狗 Problem Description CZJ 去排队打饭的时候看到前面有几对情侣秀恩爱,作为单身狗的 CZJ 表示很难受. 现在给出一个字符串代 ...

  2. SCNU ACM 2016新生赛初赛 解题报告

    新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...

  3. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  4. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  5. CH Round #56 - 国庆节欢乐赛解题报告

    最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...

  6. wechall.net/stegano 解题心得

    /* 转载请注明出处:http://www.cnblogs.com/Martinium/p/wechall_stegano.html */ 最近迷上了 www.wechall.net 网站,里面都是些 ...

  7. Mountains(CVTE面试题)解题报告

    题目大意: 用一个数组代表群山的高度.高度大的地方代表山峰,小的地方代表山谷.山谷可以容水.假设有一天下了大雨,求群山中总共可以容纳多少水? 如图所示情况,a代表该数组,总共可以容纳5个水. 解题思路 ...

  8. timus 1180. Stone Game 解题报告

    1.题目: 1180. Stone Game Time limit: 1.0 secondMemory limit: 64 MB Two Nikifors play a funny game. The ...

  9. timus 1175. Strange Sequence 解题报告

    1.题目描述: 1175. Strange Sequence Time limit: 1.0 secondMemory limit: 2 MB You have been asked to disco ...

随机推荐

  1. mysql 动态拼接表字段,值 mybatis 动态获取表字段

    -- 取表所有字段,自动用逗号分开 select GROUP_CONCAT(DISTINCT COLUMN_NAME) from information_schema.columns where ta ...

  2. python中赋值-浅拷贝-深拷贝之间的关系

    赋值: 变量的引用,没有拷贝空间 对象之间赋值本质上 是对象之间的引用传递而已.也就是多个对象指向同一个数据空间. 拷贝的对象分两种类型: . 拷贝可变类型 浅拷贝: 只拷贝第一层数据,不关心里面的第 ...

  3. web前端基本开发手册

    --------------------------------- 一.概况 1.1  WEB 标准 二.实现WEB标准 2.1  XHTML.CSS介绍 2.2  XHTML书写规范 2.2.1 X ...

  4. jdk src 学习 Threadlocal

    示例: import java.io.Serializable; public class TestThreadLocal implements Serializable { /** * */ pri ...

  5. hive-client heap内存大小的配置优先级

    hive-client Heap大小的配置优先级 其实主要解决,hive作为数据仓库(hive -e "select ····") 如果是分区表且分区较多可能导致hive 堆内存溢 ...

  6. 1. 配置win7下odbc数据源找不到数据库驱动的问题

    win7下ODBC数据源DB2的链接 直接在控制面板---管理工具----数据源(ODBC) 打开数据源配置,发现只有SQLServer的驱动,其他的都没有了. 解决方法是C:\Windows\Sys ...

  7. Nginx 服务器搭建

    什么是Nginx ? Nginx与Apache IIS等软件一样,是一款服务器软件,为web站点提供服务 除此之外,Nginx 还是一款反向代理服务器,我们可以利用Nginx实现负载均衡 所谓负载均衡 ...

  8. self, super理解

    self是方法参数列表中的第一个参数,是运行时决定的. super是编译器符号,是编译时决定的.super的含义为从父类开始寻找相应的方法,父类在编译的时候就已经决定了. 一个关键点:super并不代 ...

  9. css-选择器性能

    ID选择器 比如#header 类选择器 比如.promo 元素选择器 比如 div 兄弟选择器 比如 h2 + p 子选择器 比如 li > ul 后代选择器 比如 ul a 7. 通用选择器 ...

  10. sublime text3:下载代码格式化插件和汉化插件

    1.从官网下载sublime text3 2.下载插件工具 A.使用Ctrl+`(Esc键下方)快捷键或者通过View->Show Console菜单打开命令行 将以下代码复制后粘贴,然后按En ...