Problem F
Bangla Numbers
Input: standard input
Output: standard output
Bangla numbers normally use 'kuti' (10000000), 'lakh' (100000), 'hajar' (1000), 'shata' (100) while expanding and converting to text. You are going to write a program to convert a given number to text with them.
Input
The input file may contain several test cases. Each case will contain a non-negative number <= 999999999999999.
Output
For each case of input, you have to output a line starting with the case number with four digits adjustment followed by the converted text.
Sample Input
23764 45897458973958
Sample Output
1. 23 hajar 7 shata 64 2. 45 lakh 89 hajar 7 shata 45 kuti 89 lakh 73 hajar 9 shata 58
大意: 給一串數字小於16位 求用題目規則轉換後的樣子 解法: 遞迴(需特別注意輸出空格)
import java.util.*;
public class UVA10101 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = 0;
while (sc.hasNext()) {
count++;
long number = sc.nextLong();
System.out.printf("%4d.", count);
if (number == 0)
System.out.print(" 0");
else
algorithm(number);
System.out.println();
}
}
public static void algorithm(long number) {
if (number >= 10000000) {
algorithm(number / 10000000);
System.out.print(" kuti");
number %= 10000000;
}
if (number >= 100000) {
algorithm(number / 100000);
System.out.print(" lakh");
number %= 100000;
}
if (number >= 1000) {
algorithm(number / 1000);
System.out.print(" hajar");
number %= 1000;
}
if (number >= 100) {
algorithm(number / 100);
System.out.print(" shata");
number %= 100;
}
if (number != 0) {
System.out.print(" " + number);
}
}
}
留言
張貼留言