解题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 ...
随机推荐
- 4、申请开发(Development)证书和描述文件
开发(Development)证书用于测试环境下使用,可以直接安装到手机上(不用提交到Appstore),但一个描述文件最多只能绑定100台设备(因此通过这种证书正式发布应用是行不通的). 申请开发( ...
- js-杂记
js可计算传值 <p>点击按钮计算 x 的值.</p> <button onclick="myFunction()">点击这里</butt ...
- java中的排序(自定义数据排序)--使用Collections的sort方法
排序:将一组数据按相应的规则 排列 顺序 1.规则: 基本数据类型:日常的大小排序. 引用类型: 内置引用类型(String,Integer..),内部已经指定规则,直接使用即可.---- ...
- 《算法》BEYOND 部分程序 part 3
▶ 书中第六章部分程序,加上自己补充的代码,包括 Graham 扫描生成凸包,计算最远点对 ● Graham 扫描生成凸包 package package01; import java.util.Ar ...
- C++学习基础十四——基础类型vector
一.vector的使用 1. #include <vector> 2. 初始化的四种方法 vector<T> v1; vector<T> v2(v1); vecto ...
- 域控场景下windows安全日志的分析--审计认证行为和命令的历史记录
https://www.cnblogs.com/KevinGeorge/p/8563458.html 一.域控windows安全日志基本操作 1.打开powershell或者cmd 1 #gpedit ...
- fb 4.7英文版 修改字体大小
windows-preferences-general-appearance-colors and fonts-basic-text font
- springboot 停止
因springboot内嵌tomcat或jetty使得我们没法去操作服务: 因此,常常是服务起来后,要重启时会端口占用,我们只能无情的kill掉端口. 不过spring也设置有配置停止的请求: App ...
- Others-阿里专家强琦:流式计算的系统设计和实现
阿里专家强琦:流式计算的系统设计和实现 更多深度文章,请关注云计算频道:https://yq.aliyun.com/cloud 阿里云数据事业部强琦为大家带来题为“流式计算的系统设计与实现”的演讲,本 ...
- js相关文章
1.js获取网页屏幕可见区域高度 2.JS组件系列——BootstrapTable 行内编辑解决方案:x-editable 3.Bootstrap table 服务器端分页示例