1. 程式人生 > >習題2-5 分數化小數 (decimal) (java版本)

習題2-5 分數化小數 (decimal) (java版本)

  1. 習題2-5 分數化小數 (decimal)
  2. 輸入正整數a,b,c,輸出a/b的小數形式,精確到小數點後c位。a,b<=10^6,c<=100.
  3. 輸入應該包含多組資料,結束標記為a=b=c=0.
  4. 樣例輸入:
  5. 1 6 4
  6. 0 0 0
  7. 樣例輸出:
  8. 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);
	}

}