快速切题 sgu 111.Very simple problem 大数 开平方 难度:0 非java:1
111.Very simple problem
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
You are given natural number X. Find such maximum integer number that it square is not greater than X.
Input
Input file contains number X (1≤X≤101000).
Output
Write answer in output file.
Sample Input
16
Sample Output
4
开平方的方法懒得写,索性二分,可能的话将来回来写写
实际用时:11min
import java.io.*; import java.math.BigInteger; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String []args) throws IOException{ BigInteger X; Scanner scanner=new Scanner(System.in); X=scanner.nextBigInteger(); if(X.compareTo(BigInteger.ONE)==0){ System.out.println("1"); return ; } BigInteger l=BigInteger.ZERO,r=X; while(r.compareTo(l.add(BigInteger.ONE))==1){ BigInteger mid=l.add(r).shiftRight(1); int fl=X.compareTo(mid.multiply(mid)); if(fl==0){ l=mid; break; } else if(fl==-1){ r=mid; } else { l=mid; } } System.out.println(l); } }