解题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 ...
随机推荐
- Eclipse svn 项目 星号
上图: 明明没有文件修改过就是无脑显示有提交的文件,有强迫症受不了. 而且每次提交你都点开看有没有漏掉啥文件.很麻烦.也很难受 .很容易漏提交 把本地文件跟SVN同步对比一下. 果然有文件. 添加规则 ...
- Android DBFlow学习及示例
项目地址:Kotlin-DBflow-example Kotlin-DBflow-example DBFlow是一个功能强大的,非常简单的,带有注解处理的ORM Android数据库.github地址 ...
- leetcode1023
class Solution(object): def getGroup(self,que): group = list() temp = '' for i in range(len(que)): c ...
- MVC基于角色权限控制--数据库设计
在网站后台设计过程中都会遇上权限控制这一问题 当前较为流行的解决方案是基于角色的权限管理 基本思路如下 分别建立 用户信息表.角色信息表.权限信息表 让用户和角色关联,角色和权限关联,当用户访问时,通 ...
- Java并发:线程间数据传递和交换
转自:https://www.cnblogs.com/java-zzl/p/9741288.html 一.通过SynchronousQueue方式实现线程间数据传递: 线程A与线程B共同持有一个Syn ...
- Java安全编码标准
Java安全编码标准 具体参考Rules 输入验证和数据净化(IDS)规则风险评估概要 IDS00-J净化穿越受信边界的非受信数据 IDS01-J验证前标准化字符串 IDS02-J在验证之前标准化路径 ...
- compute by 的使用
GROUP BY子句有个缺点,就是返回的结果集中只有合计数据,而没有原始的详细记录.如果想在SQL SERVER中完成这项工作,可以使用COMPUTE BY子句.COMPTE生成合计作为附加的汇总列出 ...
- 深度学习原理与框架-Tensorflow基本操作-实现线性拟合
代码:使用tensorflow进行数据点的线性拟合操作 第一步:使用np.random.normal生成正态分布的数据 第二步:将数据分为X_data 和 y_data 第三步:对参数W和b, 使用t ...
- 【JEECG技术文档】JEECG 接口权限开发及配置使用说明
1.功能介绍 通过接口配置实现,对接口的访问权限控制和数据权限控制,接口时REST接口,接口权限认证机制使用Json web token (JWT) 接口权限调用流程: (1)通过接口用户的用户名 ...
- redis如何清除所有的key
redis比memcache好的地方之一,如果memcache,恐怕就得关掉重启了. 1 使用cli FLUSHDB 清除一个数据库,FLUSHALL清除整个redis数据. 2 使用shell re ...