• <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 閱讀(814) 評論(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
            大美女久久久久久j久久| 久久综合给合久久狠狠狠97色| 久久国产乱子伦精品免费强| 久久精品九九亚洲精品天堂| 国产精品久久亚洲不卡动漫| 国产激情久久久久影院小草| 久久亚洲AV成人无码软件| 久久精品亚洲日本波多野结衣| 国产精品内射久久久久欢欢| 久久综合亚洲色一区二区三区| 国产成人精品久久一区二区三区| 久久se精品一区二区影院| 国色天香久久久久久久小说| 国产日韩欧美久久| 久久人做人爽一区二区三区| 品成人欧美大片久久国产欧美| 久久精品国产亚洲AV香蕉| 久久久久无码国产精品不卡| 77777亚洲午夜久久多喷| 久久精品视频一| 久久精品亚洲乱码伦伦中文 | 无码八A片人妻少妇久久| av无码久久久久久不卡网站| 久久人妻无码中文字幕| 久久久人妻精品无码一区| 国产精品久久久久乳精品爆| 国产精品久久久久AV福利动漫| 亚洲精品无码久久久久| 色婷婷噜噜久久国产精品12p | 91精品免费久久久久久久久| 亚洲精品tv久久久久久久久 | 亚洲国产精品久久久久网站| 久久精品国产亚洲AV无码娇色| 中文字幕日本人妻久久久免费| 青春久久| 青青草原综合久久大伊人| 久久无码国产| 久久无码中文字幕东京热 | 久久99精品久久久久子伦| 久久WWW免费人成一看片| 亚洲中文字幕久久精品无码喷水|