1001. Reverse Root
Time limit: 2.0 second
Memory limit: 64 MB
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
*/