[UVA][JAVA] 10023 - Square root@Morris' Blog|PChome Online 人新台
2012-03-26 22:33:54| 人1,526| 回0 | 上一篇 | 下一篇

[UVA][JAVA] 10023 - Square root

0 收藏 0 0 站台

Square root  

The Problem

You are to determinate X by given Y, from expression

The Input

The first line is the number of test cases, followed by a blank line.

Each test case of the input contains a positive integer Y (1<=Y<=101000), with no blanks or leading zeroes in it.
It is guaranteed, that for given Y, X will be always an integer.

Each test case will be separated by a single line.

The Output

For each test case, your program should print X in the same format as Y was given in input.

Print a blank line between the outputs for two consecutive test cases.

Sample Input

1 7206604678144 

Sample Output

2684512

就算是 JAVA 也有大根, 只好用二分逼近,
不有不是完全平方,
for example
x = 1000 you should output y = 31
部份我了很多 ...

import java.util.Scanner;
import java.math.BigInteger;

public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String x;
BigInteger l, r, m, n;
int t = keyboard.nextInt();
while(t != 0) {
t--;
x = keyboard.next();
n = new BigInteger(x);
l = BigInteger.valueOf(1);
r = BigInteger.valueOf(10).pow(x.length()/2+1);
m = BigInteger.valueOf(0);
while(l.compareTo(r) <= 0) {
m = l.add(r).shiftRight(1);
if(m.multiply(m).compareTo(n) == 1) {
r = m.subtract(BigInteger.valueOf(1));
} else if(m.multiply(m).compareTo(n) == -1) {
l = m.add(BigInteger.valueOf(1));
} else
break;
}
if(m.multiply(m).compareTo(n) > 0)
m = l.subtract(BigInteger.valueOf(1));
System.out.println(m.toString());
if(t != 0)
System.out.println("");
}
}
}
 

台: Morris
人(1,526) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][LCS] 10192 - Vacation
此分上一篇:[UVA][JAVA] 10183 - How Many Fibs?

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86