UVa 10023 Square root:高精度及开平方公式

10023 - Square root

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=964

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

使用开平方公式可破,牛顿法居然会TLE..(Java差点TLE)

完整代码:

/*0.562s*/

#include <cstdio>
#include <cstring>
const int MAXN = 1010;  

char s[MAXN], re[MAXN];  

int big(char s1[], char s2[])
{
    int len1, len2, i, q;
    q = 0;
    while (s1[q] == '0') q++;
    strcpy(s1, s1 + q);
    if (strlen(s1) == 0)
    {
        s1[0] = '0';
        s1[1] = 0;
    }
    q = 0;
    while (s2[q] == '0') q++;
    strcpy(s2, s2 + q);
    if (strlen(s2) == 0)
    {
        s2[0] = '0';
        s2[1] = 0;
    }
    len1 = strlen(s1);
    len2 = strlen(s2);
    if (len1 > len2)
        return 1;
    else if (len1 < len2)
        return 0;
    else
    {
        for (i = 0; i < len1; i++)
        {
            if (s1[i] > s2[i])
                return 1;
            else if (s1[i] < s2[i])
                return 0;
        }
    }
    return 0;
}  

void mul(char s[], int t, char re[]) //乘
{
    int left, i, j, k, len;
    char c;
    left = 0;
    j = 0;
    for (i = strlen(s) - 1; i >= 0; i--)
    {
        k = t * (s[i] - '0') + left;
        re[j++] = (k % 10) + '0';
        left = k / 10;
    }
    while (left > 0)
    {
        re[j++] = (left % 10) + '0';
        left /= 10;
    }
    re[j] = 0;
    len = strlen(re);
    for (i = 0; i < len / 2; i++)
    {
        c = re[i];
        re[i] = re[len - 1 - i];
        re[len - 1 - i] = c;
    }
    return;
}  

void sub(char a[], char b[])  //减
{
    int left, len1, len2, temp, j;
    len1 = strlen(a) - 1;
    len2 = strlen(b) - 1;
    left = 0;
    while (len2 >= 0)
    {
        temp = a[len1] - b[len2] + left;
        if (temp < 0)
        {
            temp += 10;
            left = -1;
        }
        else
            left = 0;
        a[len1] = temp + '0';
        len1--;
        len2--;
    }
    while (len1 >= 0)
    {
        temp = a[len1] - '0' + left;
        if (temp < 0)
        {
            temp += 10;
            left = -1;
        }
        else
            left = 0;
        a[len1] = temp + '0';
        len1--;
    }
    j = 0;
    while (a[j] == '0') j++;
    strcpy(a, a + j);
    if (strlen(a) == 0)
    {
        a[0] = '0';
        a[1] = 0;
    }
    return;
}  

void sqr(char s[], char re[])  //开方
{
    char temp[MAXN];
    char left[MAXN];
    char p[MAXN];
    int i, j, len1, len2, q;
    len1 = strlen(s);
    if (len1 % 2 == 0)
    {
        left[0] = s[0];
        left[1] = s[1];
        left[2] = 0;
        j = 2;
    }
    else
    {
        left[0] = s[0];
        left[1] = 0;
        j = 1;
    }
    re[0] = '0';
    re[1] = 0;
    q = 0;
    while (j <= len1)
    {
        mul(re, 20, temp);
        len2 = strlen(temp);
        for (i = 9; i >= 0; i--)
        {
            temp[len2 - 1] = i + '0';
            mul(temp, i, p);
            if (!big(p, left))
                break;
        }
        re[q++] = i + '0';
        re[q] = 0;
        sub(left, p);
        len2 = strlen(left);
        left[len2] = s[j];
        left[len2 + 1] = s[j + 1];
        left[len2 + 2] = 0;
        j += 2;
    }
}  

int main(void)
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%s", s);
        sqr(s, re);
        int i = 0;
        while (re[i] == '0') i++;
        strcpy(re, re + i);
        printf("%s\n", re);
        if (t) putchar('\n');
    }
    return 0;
}

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

/*2.818s*/

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

public class Main {
    static final BigInteger TWO = new BigInteger("2");
    static Scanner cin = new Scanner(new BufferedInputStream(System.in));  

    public static void main(String[] args) {
        int n = cin.nextInt();
        while (n-- > 0) {
            BigInteger y = cin.nextBigInteger();
            BigInteger c = y, temp;
            do {
                temp = y;
                y = temp.add(c.divide(y)).divide(TWO);
            }
            while (y.compareTo(temp) == -1);
            System.out.println(y);
            if (n > 0)
                System.out.println();
        }
    }
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索close() scanner java
, re
, char
, while
, len
, temp
, left
开平方
10023、mswd 10023演员、mswd 10023 先锋影音、0x10023、mswd10023是av吗,以便于您获取更多的相关知识。

时间: 2024-09-22 15:56:18

UVa 10023 Square root:高精度及开平方公式的相关文章

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.

UVa 424 Integer Inquiry (高精度)

424 - Integer Inquiry Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=97&page=show_problem&problem=365 One of the first users of BIT's new supercomputer was Chip Diller. He extended h

UVa 10106 Product:高精度

10106 - Product Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=97&page=show_problem&problem=1047 The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The In

算法题:UVA 10006

Carmichael Numbers An important topic nowadays in computer science is cryptography. Some people even think that cryptography is the only important field in computer science, and that life would not matter at all without cryptography. Alvaro is one of

【LeetCode从零单排】No221.Maximal Square

题目 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. 解法很巧妙,我也是看了discuss,首先列出最左一列和最上面一列,剩下的解法可以看

ParallaxOcclusionMapping( POM ) DX9

ParallaxOcclusionMapping(后面成为POM)是一个不错的高级技术,在我看来它是至今让我印象最深刻的技术.与其说是视差,不如说准确的视线与高度图交点算法.在<Real-time Rendering>中也见过他的前身-ParallaxMapping,但是POM的精确度更高,对于斜视效果也很不错,这将让它在未来的重要技术中占有一席之地.闲话少说,还是重在实质技术.   在DX9的samples中,由ATI提高的POM案例,确实是一个不错的实践机会.看过2006的Dynamic

《从问题到程序:用Python学编程和计算》——2.8 重复计算和循环

2.8 重复计算和循环 在前面几节,我们首先看到如何通过语句的顺序组合构造最简单的程序,这种程序是直线型程序,就是简单的一系列语句.这样的程序中只有一条执行路径(一种可能执行方式):Python解释器顺序执行程序里的语句,每个语句执行一次,当语句序列中最后一条语句的执行结束时,整个程序的执行就结束了. 增加了if复合语句,能写出的程序更多,程序的形式也更丰富,其中出现了选择和分支.这样得到的程序可称为分支程序.在分支程序里,每条基本语句最多执行一次,如果实际条件导致的执行没进入某个分支,该分支里

ORALC的STDDEV、STDDEV_POP、STDDEV_SAMP等函数

今天一个同事碰到一个问题:用SQL求一个指标的计算公式:其中Xi即指标,X-指标均值,N是指标个数,看到这样的计算公式确实比较发愁.在处理问题前,先去恶补了下数理统计方面的知识(数理统计的知识基本上都还给老师了):方差.标准差.平均值..... 随机变量是指变量的值无法预先确定仅以一定的可能性(概率)取值的量.它是由于随机而获得的非确定值,是概率中的一个基本概念.   样本方差 :样本中各数据与样本平均数的差的平方和的平均数叫样本方差.   样本标准差:样本方差的算术平方根叫做样本标准差.  

Pratical Cljr – loop/recur

Programming Clojure这块写的过于简单, Pratical Clojure写的还不错, 这本书在某些章节写的深度不错, 讨论why, 而不是仅仅how, 故摘录 首先, Clojure是不提供直接的looping语法的!!! 这个理解很重要 因为looping是imperative的概念, 对于FP, 需要改变思维的方式, 用递归来解决同样的问题, 所以下面也说对于从imperative 转来的程序员, 习惯从递归(recursive)的角度思考问题, 是很大的挑战. It wi