UVA 10023 - Square root(手算平方根)

题目:UVA 10023 - Square root(手算平方根)

题目链接

题目大意:求给定的一个数的平方根。

解题思路:用二分但是这个数太大了,就超时了。看题接后发现需要用一种手算平方根的算法。

算法:

先判断这个数是不是偶数位,是的话就第一次取前面的两位数,不是的话第一次就只取前面的一位数来作为被除数。接下来就是两位两位为一节来计算。

用前一次的计算结果乘上20+一个个位数a再乘上这个a,找到最大的a使得这个式子的结果不大于被除数。

被除数减去这个结果然后再在尾巴接上那个大数的接下来两位作为新的被除数。

除数就是在原本除数的尾巴接上这个a作为新的除数。

重复上面的步骤,直到计算完这个大数的最后两位。最后的除数就是开方数。

代码:

import java.util.*;
import java.math.*;
import java.io.*;

public class Main {

    public static BigInteger sqrt(BigInteger ans) {

        BigInteger num = BigInteger.ZERO;
        BigInteger res = BigInteger.ZERO;
        BigInteger div;
        String str = "0" + ans.toString();

        int len = str.length();
        int i = len % 2;

        for (; i < len; i += 2) {

            num = num.multiply(BigInteger.valueOf(100)).add(new BigInteger(str.substring(i, i + 2)));
            div = res.multiply(BigInteger.valueOf(20));
            for (int j = 0; j < 10; j++) {
                if (div.add(BigInteger.valueOf(j + 1)).multiply(BigInteger.valueOf(j + 1)).compareTo(num) > 0) {

                    num = num.subtract(div.add(BigInteger.valueOf(j)).multiply(BigInteger.valueOf(j)));
                    res = res.multiply(BigInteger.valueOf(10)).add(BigInteger.valueOf(j));
                    break;
                }
            }
        }
        return res;
    }

    public static void main(String args[]) {

        Scanner cin = new Scanner(System.in);
        int T = cin.nextInt();
        while (T > 0) {

            BigInteger n = cin.nextBigInteger();
            System.out.println(sqrt(n));
            T--;
            if (T > 0)
                System.out.println();
        }
    }
}
时间: 2024-08-01 18:54:06

UVA 10023 - Square root(手算平方根)的相关文章

UVA - 10023 - Square root (模拟手算开方)

题目传送: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

Project Euler 80:Square root digital expansion 平方根数字展开

Square root digital expansion It is well known that if the square root of a natural number is not an integer, then it is irrational. The decimal expansion of such square roots is infinite without any repeating pattern at all. The square root of two i

手算平方根的正确方法

手算平方根的「正确」方法,是什么方法?如果你认为是牛顿迭代法的话,你可以亲自试一下,看看效果如何: (原帖 kz3007407872, 鉴于百度贴吧的帖子是公开的,我就不打码了) 其实牛顿迭代法非常好,在电脑上快得飞起.但是手算就不行了. 那么「正确」的方法是什么呢?是这个: (原帖同上) 说得神神叨叨的,还能开无限小数,到底是什么方法?帖子里没说. 不过,幸运的是,我有一天翻 Wiki 的时候,碰巧翻到了这个方法.本文将详细介绍这个方法. \(2\) 的算术平方根是多少?是 \(\sqrt{2

平方根的C语言实现(二) —— 手算平方根的原理

一个函数从数学上来说可以有无数个函数列收敛于这个函数,那么程序逼近实现来说可以有无数种算法,平方根自然也不例外. 不知道有多少人还记得手算平方根,那是满足每次在结果上添加一位,也就是按位逼近运算结果的唯一算法.至于数学上如何证明这个唯一性我就不说了,数学证明不会有那么多人有兴趣.按位逼近更加适合手算,举个大家更熟悉的例子,那就是手算除法.我这里就采用按位逼近的手算方法. 要说手算平方根,原理其实非常简单, 一是平方根函数是严格单调增函数, 二就是以下这个恒等式满足 (a*N+b)2 ≡ (a*N

UVA 11461 Square Numbers解题报告

Discription A square number is an integer number whose square root is also an integer. For example 1, 4, 81 are some square numbers. Given two numbers a and b you will have to find out how many square numbers are there between a and b (inclusive). In

Square Root

Square RootWhen the square root functional configuration is selected, a simplified CORDIC algorithm isused to calculate the positive square root of the input. The input, X_IN, and the output,X_OUT, are always positive and are both expressed as either

UVA - 11542 Square (异或方程组)

Given n integers you cangenerate 2n-1 non-empty subsets from them. Determine for howmany of these subsets the product of all the integers in that is a perfectsquare. For example for the set {4,6,10,15} there are3 such subsets. {4}, {6,10,15} and {4,6

UVA 11542 - Square(高斯消元)

UVA 11542 - Square 题目链接 题意:给定一些数字,保证这些数字质因子不会超过500,求这些数字中选出几个,乘积为完全平方数,问有几种选法 思路:对每个数字分解成质因子后,发现如果要是完全平方数,选出来的数字的每个质因子个数都必然要是偶数,这样每个质因子可以列出一个异或的方程,如果数字包含质因子,就是有这个未知数,然后进行高斯消元,求出自由变量的个数,每个自由变量可以选或不选,这样的情况就是(2^个数),然后在扣掉什么都不选的1种就是答案了 代码: #include <cstdi

手算平方根

有些丧心病狂的数学题目要求平方根..大概值没给出做精确到某一位..于是我机智地用牛顿迭代233...(话说我pi背到一百多位真是蛋疼..) 例:求$\sqrt{18}$: Initial guess: 4 //4*4=16x = 4x = (4 + 18 / 4)/2 = 4.25x = (x + 18 / x)/2 ~ 4.242647x - (x + 18 / x)/2 ~ 4.242641 //差不多了233 -----------一个相当接近sqrt(17)的分数-----------8