• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            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 閱讀(812) 評論(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
            他的意思因該是更精確的算法吧~  回復  更多評論
              
            Priceline Travel
            Priceline Travel
            成人久久精品一区二区三区| 久久最近最新中文字幕大全| 久久久精品久久久久久| 久久黄视频| 日本精品一区二区久久久| 精品久久香蕉国产线看观看亚洲 | 理论片午午伦夜理片久久 | 国内精品九九久久精品| 国内精品久久人妻互换| 人人狠狠综合久久亚洲高清| 四虎影视久久久免费观看| 久久精品国产99久久久| 丁香色欲久久久久久综合网| 久久99精品国产99久久6男男| 日韩美女18网站久久精品| 99久久99这里只有免费的精品| 国产亚州精品女人久久久久久| 日韩精品无码久久久久久| 一级A毛片免费观看久久精品| 久久精品成人免费观看97| 男女久久久国产一区二区三区 | 久久亚洲精品中文字幕| 青青草国产97免久久费观看| 久久这里只有精品首页| 亚洲精品乱码久久久久久中文字幕| 久久青青草原亚洲av无码| 狠狠狠色丁香婷婷综合久久俺| 国产精品久久久香蕉| 亚洲精品无码久久不卡| 青青热久久综合网伊人| 久久99精品国产自在现线小黄鸭| 久久综合亚洲鲁鲁五月天| 久久久国产99久久国产一| 久久精品国产亚洲av麻豆图片| 久久久久久精品无码人妻| 久久久久亚洲精品无码网址| 久久免费视频观看| 青青青伊人色综合久久| 久久综合欧美成人| 国产成人精品久久| 久久精品aⅴ无码中文字字幕不卡|