题目描述

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. java se 随机数。生成

    public class test { public static void main(String[] args) { getRandomNum1(); getRandomNum2(); getRa ...

  2. uva-10422-骑士-搜索题

    题意: 给你一个5X5的图,棋盘上骑士的走法自己去百度,问能不能在10步内走到目标图, 解题思路: 从目标图开始往前走10步,保存所有能走到的图,然后输入,查找是否存在这个图,存在就是可以走到,不存在 ...

  3. 44.scrapy爬取链家网站二手房信息-2

    全面采集二手房数据: 网站二手房总数据量为27650条,但有的参数字段会出现一些问题,因为只给返回100页数据,具体查看就需要去细分请求url参数去请求网站数据.我这里大概的获取了一下筛选条件参数,一 ...

  4. 代码: html 页面小效果 (集合,待补充)

    标签切换(下部内容区跟着切换): 2016-6-2 <script type="text/javascript" src="http://cdn.bootcss.c ...

  5. Oracle数据库基础教程

    Oracle基础 简介 数据库实例 表空间 登录身份和角色 用户和授权 数据类型 数据操作 导入数据库 一.Oracle基础: 1.简介 Oracle创建数据库不能像SQL Server那样用一个简单 ...

  6. XML 试题

    六. XML 部 分 1 .xml 有 哪 些 解 析 技 术? 区 别 是 什 么? 答:有 DOM,SAX,STAX 等 DOM:处理大型文件时其性能下降的非常厉害.这个问题是由 DOM 的树结构 ...

  7. Callable和Future 多线程

    参考:https://www.cnblogs.com/fengsehng/p/6048609.html

  8. MemberShip的 链接字符串的使用

    1.运行asp.net Sql Server注册工具:aspnet_regsql.exe,详细参见:http://msdn.microsoft.com/zh-cn/library/ms229862(v ...

  9. eclipse gradle插件 org.gradle.tooling.GradleConnectionException: Could not install Gradle distribution from 'https://services.gradle.org/distributions/gradle-3.4-bin.zip'.

    eclipse安装gradle后出现如下异常: org.gradle.tooling.GradleConnectionException: Could not install Gradle distr ...

  10. 20165304实验一java开发环境熟悉

    实验报告封面 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:李松杨 学号:20165304 指导教师:娄嘉鹏 实验日期:2018年4月2日 实验时间:13:45 - 15:25 实 ...