描述 | |
---|---|
知识点 | 字符串,循环,函数,指针,枚举,位运算,结构体,联合体,文件操作,递归 |
运行时间限制 | 10M |
内存限制 | 128 |
输入 | 输入一个long型整数 |
输出 | 输出相应的英文写法 |
样例输入 | 2356 |
样例输出 | two thousand three hundred and fifty six |
import java.util.Scanner;
public class Main{
public static void main(String arg[]) {
Scanner scan = new Scanner(System.in);
long num = scan.nextLong();
scan.close();
String str = getStr(num);
System.out.println(str);
}
public static String getStr(long num) {
String str = "";
String flag = "";
long l = num / 1000000000;
if (l != 0) {
str += getStr(l) + " " + "billion";
// flag = " ";
}
long millon = num / 1000000 % 1000;
long th = num / 1000 % 1000;
long h = num % 1000;
if (millon != 0) {
str += flag + getHStr(millon) + " " + "million";
flag = " ";
}
if (th != 0) {
str += flag + getHStr(th) + " " + "thousand";
flag = " ";
}
if (h != 0) {
str += flag + getHStr(h);
}
return str;
}
public static String getHStr(long num) {
String str = "";
if (num == 0)
return str;
int h = (int) (num / 100);
int t = (int) (num / 10 % 10);
int g = (int) (num % 10);
String flag = "";
if (h != 0) {
str += flag + name[h] + " " + "hundred";
flag = " and ";
}
if (t != 0) {
str += flag + name2[t];
flag = " ";
}
if (g != 0) {
str += flag + name[g];
}
return str;
}
static String[] name = { "zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine" };
static String[] name2 = { "zero", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety" };
}