tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
64
Task/Number-names/Java/number-names-1.java
Normal file
64
Task/Number-names/Java/number-names-1.java
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
public class Int2Words {
|
||||
static String[] small = {"one", "two", "three", "four", "five", "six",
|
||||
"seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
|
||||
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
|
||||
static String[] tens = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty",
|
||||
"ninety"};
|
||||
static String[] big = {"thousand", "million", "billion", "trillion"};
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(int2Text(900000001));
|
||||
System.out.println(int2Text(1234567890));
|
||||
System.out.println(int2Text(-987654321));
|
||||
System.out.println(int2Text(0));
|
||||
}
|
||||
|
||||
public static String int2Text(long number) {
|
||||
long num = 0;
|
||||
String outP = "";
|
||||
int unit = 0;
|
||||
long tmpLng1 = 0;
|
||||
|
||||
if (number == 0) {
|
||||
return "zero";
|
||||
}
|
||||
|
||||
num = Math.abs(number);
|
||||
|
||||
for (;;) {
|
||||
tmpLng1 = num % 100;
|
||||
if (tmpLng1 >= 1 && tmpLng1 <= 19) {
|
||||
outP = small[(int) tmpLng1 - 1] + " " + outP;
|
||||
} else if (tmpLng1 >= 20 && tmpLng1 <= 99) {
|
||||
if (tmpLng1 % 10 == 0) {
|
||||
outP = tens[(int) (tmpLng1 / 10) - 2] + " " + outP;
|
||||
} else {
|
||||
outP = tens[(int) (tmpLng1 / 10) - 2] + "-"
|
||||
+ small[(int) (tmpLng1 % 10) - 1] + " " + outP;
|
||||
}
|
||||
}
|
||||
|
||||
tmpLng1 = (num % 1000) / 100;
|
||||
if (tmpLng1 != 0) {
|
||||
outP = small[(int) tmpLng1 - 1] + " hundred " + outP;
|
||||
}
|
||||
|
||||
num /= 1000;
|
||||
if (num == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
tmpLng1 = num % 1000;
|
||||
if (tmpLng1 != 0) {
|
||||
outP = big[unit] + " " + outP;
|
||||
}
|
||||
unit++;
|
||||
}
|
||||
|
||||
if (number < 0) {
|
||||
outP = "negative " + outP;
|
||||
}
|
||||
|
||||
return outP.trim();
|
||||
}
|
||||
}
|
||||
18
Task/Number-names/Java/number-names-2.java
Normal file
18
Task/Number-names/Java/number-names-2.java
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
public class NumberToWordsConverter { // works upto 9999999
|
||||
|
||||
final private static String[] units = {"Zero","One","Two","Three","Four",
|
||||
"Five","Six","Seven","Eight","Nine","Ten",
|
||||
"Eleven","Twelve","Thirteen","Fourteen","Fifteen",
|
||||
"Sixteen","Seventeen","Eighteen","Nineteen"};
|
||||
final private static String[] tens = {"","","Twenty","Thirty","Forty","Fifty",
|
||||
"Sixty","Seventy","Eighty","Ninety"};
|
||||
|
||||
public static String convert(Integer i) {
|
||||
//
|
||||
if( i < 20) return units[i];
|
||||
if( i < 100) return tens[i/10] + ((i % 10 > 0)? " " + convert(i % 10):"");
|
||||
if( i < 1000) return units[i/100] + " Hundred" + ((i % 100 > 0)?" and " + convert(i % 100):"");
|
||||
if( i < 1000000) return convert(i / 1000) + " Thousand " + ((i % 1000 > 0)? " " + convert(i % 1000):"") ;
|
||||
return convert(i / 1000000) + " Million " + ((i % 1000000 > 0)? " " + convert(i % 1000000):"") ;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue