習題2-5 分數化小數 (decimal) (java版本)
阿新 • • 發佈:2019-02-06
- 習題2-5 分數化小數 (decimal)
- 輸入正整數a,b,c,輸出a/b的小數形式,精確到小數點後c位。a,b<=10^6,c<=100.
- 輸入應該包含多組資料,結束標記為a=b=c=0.
- 樣例輸入:
- 1 6 4
- 0 0 0
- 樣例輸出:
- Case 1:0.1667
import java.util.Scanner; public class decimal { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a,b,c; String s=""; int count=0; do{ a=in.nextInt(); b=in.nextInt(); c=in.nextInt(); if(a>0&&b>0&&c>0&&a<=100000&&b<=1000000&&c<=100){ double p=(double)a/b; double m=Math.pow(10, c); p=Math.round(p*m)/m; count++; s+="Case "+count+": "+p+"\n"; } } while(a!=0||b!=0||c!=0); System.out.println(s); } }