UVA - 10023 - Square root (模擬手算開方)
來源:程序員人生 發布時間:2015-08-19 07:41:48 閱讀次數:3462次
題目傳送:UVA - 10023
思路:摹擬手算開方,不想用c/c++,感覺太麻煩了,就直接用的java里的BigInteger類來寫的,寫了好久......Java還是得看看書呀,手算開方參考的這里

AC代碼:
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
static void fun(BigInteger x) {
String str;
str = x.toString();
str = "0" + str;
int len = str.length();
int i = len % 2;
BigInteger ans = new BigInteger("0");
BigInteger cur = new BigInteger("0");
for(; i < len; i += 2) {
cur = cur.multiply(BigInteger.valueOf(100)).add(new BigInteger(str.substring(i, i+2)));
for(int j = 0; j < 10; j ++) {
if(cur.compareTo(BigInteger.valueOf(20).multiply(ans).add(BigInteger.valueOf(j+1)).multiply(BigInteger.valueOf(j+1))) == ⑴) {
cur = cur.subtract(BigInteger.valueOf(20).multiply(ans).add(BigInteger.valueOf(j)).multiply(BigInteger.valueOf(j)));
ans = ans.multiply(BigInteger.valueOf(10)).add(BigInteger.valueOf(j));
break;
}
}
}
System.out.println(ans);
}
public static void main(String args[]) {
int T;
Scanner cin = new Scanner(System.in);
T = cin.nextInt();
for(int i = 0; i < T; i ++) {
if(i != 0) System.out.println();
fun(cin.nextBigInteger());
}
}
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈