yoyouhappy的秘密花园
欢迎来到我的秘密花园^^
posts - 16,comments - 33,trackbacks - 0

在做JOJ的DNA Sorting发现了这个问题,但想了想还是不明白为什么

其实我是想把一个整数a(<100)除以100,加到另一个整数b上去,处理以后 还想得到原来的整数a,但是我用的方法不太正确,下面是举的例子:

#include<iostream>
using namespace std;
int main()
{
int a,b;
cin
>>a>>b;
double t=a+double(b)/100;
cout
<<"t="<<t<<endl;
int index=(int)(t*100)%100;
cout
<<"index="<<index<<endl;
}

在VC6.0 或DEV-C++下输入
输入:

9

4

输出的是:

t=9.04

index=3
本来以为应该index=4 的

然后用下面的程序测了一下从1.00到10.99之间的数

 1#include<iostream>
 2using namespace std;
 3int main()
 4{
 5int a;
 6int b;
 7double t;
 8int num=0;
 9int index;
10for(a=1;a<11;a++)
11   for(b=0;b<100;b++)
12   {
13    t=(double)a+double(b)/100;
14    index=(int)(t*100)%100;
15    if(index!=b)
16    {
17     cout<<"a="<<a<<"    "<<"b="<<b<<"     "<<"t="<<t<<"      "<<"index="<<index<<endl;
18     num++;
19    }
20    
21   }
22   cout<<"num="<<num<<endl;
23
24}
25
26

下面是在VC6.0下运行的结果:
a=1   b=13    t=1.13     index=12
a=1   b=15    t=1.15     index=14
a=1   b=16    t=1.16     index=15
a=1   b=57    t=1.57     index=56
a=1   b=82    t=1.82     index=81
a=2   b=1    t=2.01     index=0
a=2   b=3    t=2.03     index=2
a=2   b=5    t=2.05     index=4
a=2   b=7    t=2.07     index=6
a=2   b=26    t=2.26     index=25
a=2   b=30    t=2.3     index=29
a=2   b=32    t=2.32     index=31
a=2   b=47    t=2.47     index=46
a=2   b=51    t=2.51     index=50
a=2   b=55    t=2.55     index=54
a=4   b=2    t=4.02     index=1
a=4   b=6    t=4.06     index=5
a=4   b=10    t=4.1     index=9
a=4   b=14    t=4.14     index=13
a=4   b=27    t=4.27     index=26
a=4   b=31    t=4.31     index=30
a=4   b=35    t=4.35     index=34
a=4   b=39    t=4.39     index=38
a=4   b=52    t=4.52     index=51
a=4   b=60    t=4.6     index=59
a=4   b=64    t=4.64     index=63
a=4   b=69    t=4.69     index=68
a=4   b=77    t=4.77     index=76
a=4   b=85    t=4.85     index=84
a=4   b=89    t=4.89     index=88
a=4   b=94    t=4.94     index=93
a=5   b=2    t=5.02     index=1
a=5   b=6    t=5.06     index=5
a=5   b=10    t=5.1     index=9
a=8   b=3    t=8.03     index=2
a=8   b=4    t=8.04     index=3
a=8   b=12    t=8.12     index=11
a=8   b=20    t=8.2     index=19
a=8   b=28    t=8.28     index=27
a=8   b=29    t=8.29     index=28
a=8   b=37    t=8.37     index=36
a=8   b=45    t=8.45     index=44
a=8   b=53    t=8.53     index=52
a=8   b=54    t=8.54     index=53
a=8   b=62    t=8.62     index=61
a=8   b=70    t=8.7     index=69
a=8   b=78    t=8.78     index=77
a=8   b=79    t=8.79     index=78
a=8   b=87    t=8.87     index=86
a=8   b=95    t=8.95     index=94
a=9   b=3    t=9.03     index=2
a=9   b=4    t=9.04     index=3
a=9   b=12    t=9.12     index=11
a=9   b=20    t=9.2     index=19
a=9   b=28    t=9.28     index=27
a=9   b=29    t=9.29     index=28
a=9   b=37    t=9.37     index=36
a=9   b=45    t=9.45     index=44
a=9   b=53    t=9.53     index=52
a=9   b=54    t=9.54     index=53
a=9   b=62    t=9.62     index=61
a=9   b=70    t=9.7     index=69
a=9   b=78    t=9.78     index=77
a=9   b=79    t=9.79     index=78
a=9   b=87    t=9.87     index=86
a=9   b=95    t=9.95     index=94
a=10   b=3    t=10.03     index=2
a=10   b=4    t=10.04     index=3
a=10   b=12    t=10.12     index=11
a=10   b=20    t=10.2     index=19
num=70
 
在DEV-C++下运行的结果是num=480;

把index定义为double型,结果还是一样的

觉得很诡异,精度应该够啊,也不知道是哪一步弄错了,诡异阿


posted on 2007-08-18 18:07 yoyouhappy 阅读(780) 评论(4)  编辑 收藏 引用 所属分类: yoyo的解题报告acm/icpc学习笔记

FeedBack:
# re: 诡异阿~
2007-08-18 19:10 | 过客
double型转int型时是截断取整,象2.04这样的数字在内存中实际上可能是表示为2.03999999999999999999999999... 所以×100取整时就变成了203而不是204。正确的浮点数转整数的方式是(int)(double + 0.5),如果要再精确的话就要用银行家算法了  回复  更多评论
  
# re: 诡异阿~
2007-08-18 20:59 | yoyouhappy
原来是这个原因。。。谢谢啦~
因为那些特殊的数没有规律,就没以为是没+0.5的原因。。。

改成int index=(int)(t*100+0.5)%100;以后就对了~
  回复  更多评论
  
# re: 诡异阿~
2007-08-19 01:21 | czxskell
何谓银行家算法?  回复  更多评论
  
# re: 诡异阿~
2007-08-19 18:45 | yoyouhappy
他的意思因该是更精确的算法吧~  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


Priceline Travel
Priceline Travel