• <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發(fā)現(xiàn)了這個問題,但想了想還是不明白為什么

            其實(shí)我是想把一個整數(shù)a(<100)除以100,加到另一個整數(shù)b上去,處理以后 還想得到原來的整數(shù)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
            本來以為應(yīng)該index=4 的

            然后用下面的程序測了一下從1.00到10.99之間的數(shù)

             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下運(yùn)行的結(jié)果:
            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++下運(yùn)行的結(jié)果是num=480;

            把index定義為double型,結(jié)果還是一樣的

            覺得很詭異,精度應(yīng)該夠啊,也不知道是哪一步弄錯了,詭異阿


            posted on 2007-08-18 18:07 yoyouhappy 閱讀(811) 評論(4)  編輯 收藏 引用 所屬分類: yoyo的解題報告acm/icpc學(xué)習(xí)筆記

            FeedBack:
            # re: 詭異阿~
            2007-08-18 19:10 | 過客
            double型轉(zhuǎn)int型時是截斷取整,象2.04這樣的數(shù)字在內(nèi)存中實(shí)際上可能是表示為2.03999999999999999999999999... 所以×100取整時就變成了203而不是204。正確的浮點(diǎn)數(shù)轉(zhuǎn)整數(shù)的方式是(int)(double + 0.5),如果要再精確的話就要用銀行家算法了  回復(fù)  更多評論
              
            # re: 詭異阿~
            2007-08-18 20:59 | yoyouhappy
            原來是這個原因。。。謝謝啦~
            因?yàn)槟切┨厥獾臄?shù)沒有規(guī)律,就沒以為是沒+0.5的原因。。。

            改成int index=(int)(t*100+0.5)%100;以后就對了~
              回復(fù)  更多評論
              
            # re: 詭異阿~
            2007-08-19 01:21 | czxskell
            何謂銀行家算法?  回復(fù)  更多評論
              
            # re: 詭異阿~
            2007-08-19 18:45 | yoyouhappy
            他的意思因該是更精確的算法吧~  回復(fù)  更多評論
              
            Priceline Travel
            Priceline Travel
            中文字幕精品无码久久久久久3D日动漫| 人妻丰满?V无码久久不卡| 精品久久久久久中文字幕大豆网| 精品久久久一二三区| 久久夜色精品国产网站| 国产91色综合久久免费| 久久人人爽人人爽AV片| 国产偷久久久精品专区| 久久九九青青国产精品| 思思久久99热只有频精品66| 久久久久高潮毛片免费全部播放| 久久久亚洲精品蜜桃臀| 久久综合狠狠综合久久| 久久综合九色综合欧美就去吻| 亚洲国产精品久久久天堂| 久久久这里有精品中文字幕| 国产精品对白刺激久久久| 国产精品久久久久久久久久影院| 精品久久久久久中文字幕| 久久热这里只有精品在线观看| 国产精品成人无码久久久久久| 精品一二三区久久aaa片| 秋霞久久国产精品电影院| 亚洲AV无一区二区三区久久| 久久精品无码一区二区三区免费 | 午夜人妻久久久久久久久| 欧洲精品久久久av无码电影| 一本伊大人香蕉久久网手机| 亚洲欧美日韩中文久久| 久久人与动人物a级毛片| 久久久久久久久久免免费精品 | 伊人精品久久久久7777| 久久久精品久久久久特色影视| 亚洲AV无码久久| 久久综合久久自在自线精品自| 漂亮人妻被中出中文字幕久久| 久久强奷乱码老熟女| 久久天天躁狠狠躁夜夜av浪潮 | 亚洲AV日韩精品久久久久久 | 亚洲中文字幕久久精品无码APP | 精品熟女少妇AV免费久久 |