import java.util.Scanner;
import java.util.HashMap;
import java.lang.Math;
import java.util.Map;
// Compiler version JDK 11.0.2
class Main
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
long N = scan.nextLong();
long log = N;
long digit = 0L;
while(log>=1L)
{
log = log/10L;
digit = digit 1L;
}
int i = 0;
long cmonPlease = (long) Math.pow(10,i 1);
int you = (int) Math.ceil(digit/3) 1;
int totally = (int) Math.floor(i/3);
long ruined = N%cmonPlease;
long my = (long) Math.pow(10,i);
long code = (long) Math.floor(ruined/my);
long[][] format = new long[you 1][3];
for(i=0; i<digit; i )
{
cmonPlease = (long) Math.pow(10,i 1);
totally = (int) Math.floor(i/3);
ruined = N%cmonPlease;
my = (long) Math.pow(10,i);
code = (long) Math.floor(ruined/my);
format[totally][i%3]=code;
}
/*for(i=0; i<format.length; i ) {
for(int j=0; j<format[i].length; j ) {
System.out.println("Values at arr[" i "][" j "] is " format[i][j]);
}
}*/
HashMap<Long,String> ones = new HashMap<Long,String>();
ones.put(0L,"zero");
ones.put(1L,"one");
ones.put(2L,"two");
ones.put(3L,"three");
ones.put(4L,"four");
ones.put(5L,"five");
ones.put(6L,"six");
ones.put(7L,"seven");
ones.put(8L,"eight");
ones.put(9L,"nine");
HashMap<Long,String> teens = new HashMap<Long,String>();
teens.put(0L,"ten");
teens.put(1L,"eleven");
teens.put(2L,"twelve");
teens.put(3L,"thirteen");
teens.put(4L,"fourteen");
teens.put(5L,"fifteen");
teens.put(6L,"sixteen");
teens.put(7L,"seventeen");
teens.put(8L,"eightteen");
teens.put(9L,"nineteen");
HashMap<Long,String> tens = new HashMap<Long,String>();
tens.put(2L,"twenty");
tens.put(3L,"thirty");
tens.put(4L,"fourty");
tens.put(5L,"fifty");
tens.put(6L,"sixty");
tens.put(7L,"seventy");
tens.put(8L,"eighty");
tens.put(9L,"ninety");
HashMap<Long,String> illions = new HashMap<Long,String>();
illions.put(format[0][0],"");
if(digit>3L)
{
illions.put(format[1][0],"thousand");
}
if(digit>6L)
{
illions.put(format[2][0],"million");
}
if(digit>9L)
{
illions.put(format[3][0],"billion");
}
Map<Long, Map<Long, String>> position = new HashMap<>();
for(i=0;i<you;i )
{
if(format[i][1]>1L)
{
position.put(format[i][1],tens);
position.put(format[i][0],ones);
}
else if(format[i][1]==1L)
{
position.put(format[i][1],teens);
}
else if(format[i][1]==0L)
{
position.put(format[i][0],ones);
}
position.put(format[i][2],ones);
}
String out = "";
for(i=you-1;i>=0;i--)
{
if(format[i][0]==0&format[i][1]==0&format[i][2]==0)
{
i=i-1;
}
if(format[i][2]!=0L)
{
out = out position.get(format[i][2]).get(format[i][2]);
out = out " hundred ";
}
if(format[i][1]==1L)
{
out = out position.get(format[i][1]).get(format[i][0]);
}
else if(format[i][1]>1L)
{
out = out position.get(format[i][1]).get(format[i][1]) " " position.get(format[i][0]).get(format[i][0]);
}
if(format[i][1]==0L&format[i][0]!=0L)
{
out= out position.get(format[i][0]).get(format[i][0]);
}
out = out " " illions.get(format[i][0]);
if(i!=0L)
{
out = out " ";
}
}
System.out.println(out);
}
}
Can somebody check what's wrong with my code? I just can't get my head over it.
I was solving a Dcoder challenge and had to go through a whole bunch of mental gymnastics to get to the point I'm at now. It mostly works, say if you put non repeated digits like 987654
987654 nine hundred eighty seven thousand six hundred fifty four
Process finished.
But in case of repeated digits two things happen:
- It repeats the illions' elements multiple times even though i is supposed to decrease.
- The tens' elements won't get added to out but ones' does
88888 eight eight thousand eight hundred eight eight thousand
Process finished.
The program is to take any integer N such that, 0≤N≤10¹⁰ and convert it in numerals.
I personally think the problem is somewhere in how java deals with division or something but I can be wrong.
This is my first StackOverflow question and also being a novice developer (as you can see from my code) please excuse any mistakes.
CodePudding user response:
You are using a map for position which will be overridden for repeated values: Consider 88:
position.put(format[i][1], tens);
position.put(format[i][0], ones);
The first line sets position[8] = tens
then the second line overwrites it with position[8] = ones
, so you are losing the tens
.
format
probably could just be a list of integers where the first value is the least significant digit, e.g. for 123 the list would be [3, 2, 1]
.
Then you wouldn't need all the complicated maps within maps, etc.