Problem K URAl 1001. Reverse Root

本文介绍了解决1001.ReverseRoot问题的方法,该问题是要求计算一系列大整数的平方根,并按输入逆序输出。文章详细解释了如何通过二分查找法高效地求得平方根。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1001. Reverse Root

Time limit: 2.0 second
Memory limit: 64 MB
The problem is so easy, that the authors were lazy to write a statement for it!

Input

The input stream contains a set of integer numbers Ai (0 ≤ Ai ≤ 1018). The numbers are separated by any number of spaces and line breaks. A size of the input stream does not exceed 256 KB.

Output

For each number Ai from the last one till the first one you should output its square root. Each square root should be printed in a separate line with at least four digits after decimal point.

Sample

input output
 1427  0   

   876652098643267843 
5276538
  
   
2297.0716
936297014.1164
0.0000
37.7757

ps:再次感谢莫莫的解题报告

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1001

题目的意思很好懂,主要是处理平方根的问题,数据的范围是10^18。显然不能使用sqrt来求出平方根。我们的思想则用二分把平方根找出来。

题意:

     输入若干个__int64型的数据,输出他们的平方根。需要注意的是题目要求输出的顺序是逆序的。即最先输入的最后输出平方根,最后输入的最先输出。

解题思路:

     开一个double型的数组。先读入一个__int64型的数据,然后二分返回一个double型即为它的平方根,然后保存到double数组里面。处理到文件尾,则将double数组逆序输出即可。


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
double res[1000004];

double calsr(__int64 x)                       //二分返回数据的平方根
{
  double left=0,right=1000000000,mid;         
          //right可以是活动的,但是由于二分的效率很高,所以直接定为了1000000000。                      
  while(left+0.000001<right)
  {
     mid=(left+right)/2.0;
     if(mid*mid>x)
         right=mid;
      else
          left=mid;
  }
  return mid;
}

int main()
{
    __int64 p;
    int t=-1,i;
    while(scanf("%I64d",&p)!=EOF)
      res[++t]=calsr(p);                   //用res数组保存每个数据的平方根
    for(i=t;i>=0;i--)
        printf("%.4f\n",res[i]);
    return 0;
}
/*
1427 
0   
876652098643267843 
5276538
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值