|
I am also invulnerable, I think.
import java.io. *;
public class a
{
public static void main (String [] args)
{
String s = new String ();
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
while (true)
{
try {s = br.readLine ();} catch (Exception e) {}
Convert c = new Convert (s.toCharArray ());
System.out.println (c);
}
}
}
class Convert
{
final String [] str1 = {"", "ten", "hundred", "thousand"};
final String [] str2 = {"100 million", "10,000"};
final String [] number = {"", "One", "Two", "Three", "Wu", "Wu", "Lu", "柒", "捌", "玖"};
char [] num_char;
StringBuffer sb = new StringBuffer ();
int last;
public Convert (char [] num)
{
num_char = num;
}
public String toString ()
{
last = num_char.length% 4;
int pos = 0;
add (sb, new String (num_char, 0, last));
pos + = last;
for (int i = (int) (num_char.length / 4); i> 0; i--)
{
if (i% 2 == 0) sb.append (str2 [0]);
else sb.append (str2 [1]);
add (sb, new String (num_char, pos, 4));
pos + = 4;
}
if (last == 0) sb.deleteCharAt (0);
return sb.toString ();
}
private void add (StringBuffer sb, String temp)
{
char [] temp2 = temp.toCharArray ();
int length = temp.length ();
for (int i = 0; i <length; i ++)
{
if (temp2 [i] == '0')
if (sb.lastIndexOf ("zero")! = sb.length ()-1&&sb.length ()! = 0) sb.append ("zero"); else;
else
{
sb.append (number [temp2 [i]-'0']);
sb.append (str1 [length-i-1]);
}
}
try {
if (sb.lastIndexOf ("zero") == sb.length ()-1) sb.deleteCharAt (sb.lastIndexOf ("zero"));
} catch (Exception e) {}
}
}
The running result is:
1
one
10
One ten
100
One hundred
1000
One thousand
10000
Ten thousand
10000000
Ten million
102351225
One hundred and two hundred and thirty thirty one thousand one hundred and two hundred and ten
10250250210210
One hundred thousand two hundred thousand two hundred one hundred million one hundred thousand two hundred one thousand one hundred ten thousand two hundred one hundred ten
32135400213501232450123256903284230040
Three hundred and twenty-one thousand three hundred and five hundred million one hundred and thirty-nine thousand one hundred thirty-three thousand five hundred and one hundred million
Hundreds of thousands of people, ten thousand and ten thousand, three hundred and twenty million, two hundred and ten thousand, two hundred and thirty thousand, thirty thousand and ten |
|