fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12.  
  13. try {
  14.  
  15. int nrOfTests = Integer.parseInt(br.readLine());
  16.  
  17. for( int i =0; i< nrOfTests; i++) {
  18. String[] tab = br.readLine().split(" ");
  19. System.out.println( numberInWords( tab[0] ) );
  20. }
  21.  
  22. br.close();
  23. } catch (Exception e) { System.out.println("jakis blad"); }
  24.  
  25. }
  26.  
  27.  
  28. public static StringBuffer numberInWords( String str1 ) {
  29.  
  30. StringBuffer res1= new StringBuffer("");
  31.  
  32. int leng1 = str1.length();
  33.  
  34. if ( leng1<=3 ) res1.append(
  35. from1to999( Integer.parseInt(str1) ) );
  36.  
  37. if ( leng1>3 && leng1<=6 ) res1.append(
  38. from1to999( Integer.parseInt( str1.substring(0, leng1-3) ) ) + "tys. " +
  39. from1to999( Integer.parseInt( str1.substring( leng1-3) ) ) );
  40.  
  41. if ( leng1>6 && leng1<=9 ) res1.append(
  42. from1to999( Integer.parseInt( str1.substring(0, leng1-6) ) ) + "mil. "+
  43. from1to999( Integer.parseInt( str1.substring( leng1-6, leng1-3) ) ) + "tys. "+
  44. from1to999( Integer.parseInt( str1.substring( leng1-3) ) ) );
  45.  
  46. if ( leng1>9 && leng1<=12 ) res1.append(
  47. from1to999( Integer.parseInt( str1.substring(0, leng1-9) ) ) + "mld. "+
  48. from1to999( Integer.parseInt( str1.substring(leng1-9, leng1-6) ) ) + "mil. "+
  49. from1to999( Integer.parseInt( str1.substring( leng1-6, leng1-3) ) ) + "tys. "+
  50. from1to999( Integer.parseInt( str1.substring( leng1-3) ) ) );
  51.  
  52. if ( leng1>12 && leng1<=15 ) res1.append(
  53. from1to999( Integer.parseInt( str1.substring(0, leng1-12) ) ) + "bln. "+
  54. from1to999( Integer.parseInt( str1.substring(leng1-12, leng1-9) ) ) + "mld. "+
  55. from1to999( Integer.parseInt( str1.substring(leng1-9, leng1-6) ) ) + "mil. "+
  56. from1to999( Integer.parseInt( str1.substring( leng1-6, leng1-3) ) ) + "tys. "+
  57. from1to999( Integer.parseInt( str1.substring( leng1-3) ) ) ) ;
  58.  
  59. return res1;
  60. }
  61.  
  62.  
  63.  
  64. public static StringBuffer from1to999(int n) {
  65. int n100 = n/100;
  66. int n10 = (n - n100 *100) /10;
  67. int n1 = (n - n100*100 - n10*10);
  68.  
  69. String[] w100_900 = { "", "sto ", "dwiescie ", "trzysta ", "czterysta ", "piecset ",
  70. "szescset ", "siedemset ", "osiemset ", "dziewiećset "};
  71.  
  72. String[] w20_90 = { "", "", "dwadziescia ", "trzydziesci ", "czterdziesci ", "piecdziesiat ",
  73. "szescdziesiat ", "siedemdziesiat ", "osiemdziesiat ", "dziewiecdziesiat "};
  74.  
  75. String[] w10_19 = { "dziesiec ", "jedynascie ","dwanascie ","trzynascie ","czternascie ","pietnascie ",
  76. "szesnascie "," siedemnascie ","osiemnascie ","dziewietnascie "};
  77.  
  78. String[] w1_9= {"", "jeden ","dwa ","trzy ","cztery ", "piec ",
  79. "szesc ","siedem ","osiem ","dziewiec "};
  80.  
  81. StringBuffer res1 = new StringBuffer( w100_900[ n100 ] );
  82.  
  83. if ( n10>1 ) res1.append( w20_90[n10] );
  84.  
  85. if ( n10==1 ) res1.append( w10_19[n1] );
  86.  
  87. if ( n10 != 1 ) res1.append( w1_9[n1] );
  88.  
  89. return res1;
  90. }
  91.  
  92. }
  93.  
Success #stdin #stdout 0.14s 37936KB
stdin
5
1
12
345
1459
123456789
stdout
jeden 
dwanascie 
trzysta czterdziesci piec 
jeden tys. czterysta piecdziesiat dziewiec 
sto dwadziescia trzy mil. czterysta piecdziesiat szesc tys. siedemset osiemdziesiat dziewiec