【JAVA】UVA 10101 - Bangla Numbers

題目原文:https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1042

題目大意:把輸入的阿拉伯數字改成孟加拉單位輸出
kuti = 10,000,000 (千萬)
lakh = 100,000 (十萬)
hajar = 1000 (千)
shata = 100 (百)

Sample Input
23764
45897458973958
0

Sample Output

  1. 23 hajar 7 shata 64
    1. 45 lakh 89 hajar 7 shata 45 kuti 89 lakh 73 hajar 9 shata 58
  2. 0

超過7位數就重新編排,透過kuti連接。

import java.util.*;

public class Main {
	public static void calculate(long num) {
		if (num == 0) 
			return;
		
		if (num >= 10000000) {	
			calculate(num/10000000);
			System.out.print(" kuti");
			calculate(num%10000000);
		}
    
		else if (num >= 100000) {
			System.out.print(" " + num/100000 + " lakh");
			calculate(num%100000);
		}
    
		else if (num >= 1000) {
			System.out.print(" " + num/1000 + " hajar");
			calculate(num%1000);
		}
    
		else if (num >= 100) {
			System.out.print(" " + num/100 + " shata");
			calculate(num%100);
		}
    
		else if (num % 100 != 0) 
			System.out.print(" " + num);
	}
	
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int count = 0;
		while (sc.hasNextLine()) {
			count++;
			long num = Long.parseLong(sc.nextLine());
			System.out.printf("%4d.", count);
      
			if (num == 0)
				System.out.printf(" 0");
			else
				calculate(num);
        
			System.out.println();
		}
	}
}