| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
解题思路,有兴趣的同学来讨论一下——解析double型数据对于这道题,我实在想不通,用double型求出总和的做法怎么能过,于是研究了一下。
如果0.1 * 0.1 * 0.1 …… 一直乘下去,会怎么样呢?我得到的结果是:
1. 0.1的309次方能够在小数点后309位(第309位才是有效数字,下同)得到一个1,310就不行了。
2. 在小数点后323位,double型就变成绝对的0了。
3. 取17位有效数字的话,0.1就变成了0.10000000000000001。
4. 如果一个小数的实际有效数字超过了17位,比如0.1234567891011121314151617,double型看起来似乎只存储17位, 变成了0.12345678910111213000(用printf输出小数点后20位)。
5. 但是,经过处理,会发现double能保存不止17位有效数字。0.1的300次方(包含误差),经过处理,有效数字如下:10000000000000175415237890774733386933803558349609375(53位有效数字)。
有兴趣的同学一起研究一下。
关于这个问题的研究,代码如下:
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string result;
double n = 1;
freopen("a.out", "w", stdout);
for(int i = 1; i <= 300; i++) {
n *= 0.1;
printf("%d %.350f\n", i, n);
}
result.clear();
while(n)
{
n *= 10;
int m = (int)n;
result.push_back(m+48);
n -= m;
}
while(1)
{
if(result[0] != '0')
break;
else
result = result.substr(1);
}
cout << result << endl;
cout << result.length() << endl;
return 0;
}
这样看来,这道题貌似是水题,直接用double型不用高精度就可以解决了。
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator