close
題目概要:
kuti代替10000000、 lakh代替100000、hajar代替1000、shata代替100,換算所輸入的值。
解題方向:
使用遞迴取出kuti、lakh、hajar、shata的值。如果不使用遞迴,程式碼會很冗長。
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Java | |
import java.util.Scanner; | |
class uva10101{ | |
public static void main(String args[]){ | |
Scanner sc=new Scanner(System.in); | |
int count=1; | |
while(sc.hasNext()){ | |
System.out.printf("%4d.",count); | |
long num=sc.nextLong(); | |
if(num==0){ | |
System.out.print(" 0"); | |
}else{ | |
change(num); | |
} | |
count++; | |
System.out.println(""); | |
} | |
} | |
public static void change(long num){ | |
//'kuti' (10000000), 'lakh' (100000), 'hajar' (1000), 'shata' (100) | |
if(num>=10000000){ | |
change(num/10000000); | |
System.out.print(" kuti"); | |
num=num%10000000; | |
} | |
if(num>=100000){ | |
change(num/100000); | |
System.out.print(" lakh"); | |
num=num%100000; | |
} | |
if(num>=1000){ | |
change(num/1000); | |
System.out.print(" hajar"); | |
num=num%1000; | |
} | |
if(num>=100){ | |
change(num/100); | |
System.out.print(" shata"); | |
num=num%100; | |
} | |
if(num>0){ | |
System.out.print(" "+num); | |
} | |
} | |
} |
文章標籤
全站熱搜