1. 程式人生 > >計蒜客 x的平方根

計蒜客 x的平方根



設計函式int sqrt(int x),計算 xxx 的平方根。
輸入格式

輸入一個 整數 xxx,輸出它的平方根。直到碰到檔案結束符(EOF)為止。
輸出格式

對於每組輸入,輸出一行一個整數,表示輸入整數的平方根。
樣例輸入

1
2
3
4
5
6
7
8
9

樣例輸出

1
1
1
2
2
2
2
2
3



import java.util.Scanner;

public class Main {

	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int x;
		long an;
		while(sc.hasNext()) {
			x=sc.nextInt();
			an=fun(x);
			System.out.println(an);
		
		}
	}
	private static int fun(int x) {
		double a = Math.sqrt(x);
		int b = (int)a;
		return b;
	}

}