SGU 111 Very simple problem
2015-03-16 20:25
246 查看
给一个数X (1≤X≤101000).求出平方后不超过X的最大整数。
法一:二分区间[1,10500],寻找符合的数。
法二:手工开方。。这里有相关证明:http://www.cnblogs.com/Rinyo/archive/2012/12/16/2820450.html
div、ans、temp分别用来存储当前除数、当前所得商(结果)、当前被除数
法一:二分区间[1,10500],寻找符合的数。
import java.math.BigInteger; import java.util.Scanner; public class Solution { public static void main(String[] args) { BigInteger l = new BigInteger("1"),r = new BigInteger("10"),x,mid,ans = new BigInteger("0"); r = r.pow(500); Scanner in = new Scanner(System.in); x = in.nextBigInteger(); while(l.compareTo(r)<=0){ mid = (l.add(r)).shiftRight(1); if(mid.pow(2).compareTo(x)<=0){ ans=l; l=mid.add(BigInteger.valueOf(1)); } else r=mid.subtract(BigInteger.valueOf(1)); } System.out.println(ans); in.close(); } }
法二:手工开方。。这里有相关证明:http://www.cnblogs.com/Rinyo/archive/2012/12/16/2820450.html
div、ans、temp分别用来存储当前除数、当前所得商(结果)、当前被除数
import java.math.BigInteger; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); BigInteger ans=new BigInteger("0"),div,temp=new BigInteger("0"),n; int i; n=in.nextBigInteger(); String s="0"+n.toString(); if(s.length()%2==0) i=1; else i=2; for(;i<s.length();i+=2) { div=ans.multiply(BigInteger.valueOf(20)); temp=temp.multiply(BigInteger.valueOf(100)); temp=temp.add(new BigInteger(s.substring(i-1,i+1))); int k; for(k=0;k<10;++k) if(div.add(BigInteger.valueOf(k+1)).multiply(BigInteger.valueOf(k+1)).compareTo(temp)==1) break; ans=ans.multiply(BigInteger.valueOf(10)).add(BigInteger.valueOf(k)); temp=temp.subtract(div.add(BigInteger.valueOf(k)).multiply(BigInteger.valueOf(k))); } System.out.println(ans); in.close(); } }
相关文章推荐
- SGU 111 Very simple problem 翻译 题解
- SGU 111 Very simple problem
- SGU111 Very simple problem
- sgu 111 Very simple problem 高精开平方
- SGU 111 Very simple problem
- SGU111 Very simple problem(大数开方)
- SGU 111 Very simple problem(高精度)
- SGU 111 Very simple problem(高精度+二分)
- Very simple problem - SGU 111(大数开方)
- SGU 111 Very simple problem
- SGU111 Very simple problem
- 数学/手开平方/sgu111 Very simple problem
- 111. Very simple problem
- 111. Very simple problem
- hdu 3483 A Very Simple Problem 矩阵
- sgu111:Very simple problem
- I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum o
- POJ 2535 Very Simple Problem
- POJ 2535 Very Simple Problem(水~)
- POJ 2535 Very Simple Problem