• <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>

            no_rain

            hdoj1496(hash)

            這道題在a,b,c,d未知的情況下,是有200萬的可能的結(jié)果,但是當(dāng)a,b,c,d給定則變成2萬了,當(dāng)然這2萬是不連續(xù)的,那么為了節(jié)省空間,可以用線性探測,數(shù)組要開的比2萬大。
            無線性探測代碼
             1 #include<cstdio>
             2 #include<cstring>
             3 int hash[2000001] ;
             4 int hash_fun(int num){
             5   return num + 1000000;
             6 }
             7 int main(){
             8   int a,b,c,d;
             9   int sq[101={0};
            10   for(int i = 1; i <= 100; i++)
            11     sq[i] = i*i;
            12   while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF){
            13     if((a<0 && b<0 && c<0 && d<0|| 
            14        (a>0 && b>0 && c>0 && d>0) ){
            15       printf("0\n");
            16       continue;
            17     }
            18     memset(hash,0,sizeof(hash));
            19     for(int i = 1; i <= 100; i++)
            20       for(int j = 1; j <= 100; j++)
            21     hash[hash_fun(-sq[i]*- sq[j]*b)]++;
            22     int ans = 0;
            23     for(int i = 1; i <= 100; i++)
            24       for(int j = 1; j <= 100; j++)
            25     ans += hash[hash_fun(sq[i]*+ sq[j]*d)];
            26     printf("%d\n",ans*16);
            27   }
            28 }
            29       
            線性探測:h(k)%S被占用,則用(h(k)+i)%S,直到找到?jīng)]有被占用的,因為使用線性探測就使h(k)存在不確定性,故在h(k)處必須保存k的值,即該數(shù)組是一結(jié)構(gòu)體的數(shù)組。
            先說一下一段錯誤的代碼
             1 #include<cstdio>
             2 #include<cstring>
             3 const int maxn = 21000;
             4 struct hashn{
             5   int k;
             6   int c;
             7 };
             8 hashn hash[100000] ;
             9 int hash_fun(int num){
            10   int t = num % maxn;
            11   if(t < 0)t += maxn;
            12   while(hash[t].k != 0 ){
            13     if(hash[t].k == num)return t;
            14     t = (t+1)%maxn;
            15     if(t<0) t+= maxn;
            16   }
            17   hash[t].k = num;
            18   return t;
            19 }
            20 int hash_fun2(int num){
            21   int t = num % maxn;
            22   if(t < 0)t += maxn;
            23   while(hash[t].k != 0 ){
            24     if(hash[t].k == num)return t;
            25     t = (t+1)%maxn;
            26     if(t<0) t+ maxn;
            27   }
            28   return t;
            29 }
            30 int main(){
            31   int a,b,c,d;
            32   int sq[101={0};
            33   for(int i = 1; i <= 100; i++)
            34     sq[i] = i*i;
            35   while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF){
            36     if((a<0 && b<0 && c<0 && d<0|| 
            37        (a>0 && b>0 && c>0 && d>0) ){
            38       printf("0\n");
            39       continue;
            40     }
            41     for(int i = 1; i <= 100000;i++)
            42       hash[i].c = hash[i].k = 0;
            43     for(int i = 1; i <= 100; i++)
            44       for(int j = 1; j <= 100; j++)
            45     hash[hash_fun(-sq[i]*- sq[j]*b)].c++;
            46     int ans = 0;
            47     for(int i = 1; i <= 100; i++)
            48       for(int j = 1; j <= 100; j++){
            49     int temp = hash_fun(sq[i]*+ sq[j]*d);
            50     ans += hash[temp].c;
            51       }
            52     printf("%d\n",ans*16);
            53   }
            54 }
            這段代碼有一個比較明顯的錯誤,還有一個不明顯但是很容易錯的地方。
            maxn定為21000過小了,即使數(shù)組足夠大,但是這也只用到了21000。
            另一個就是:初始化hash表的時候的值在所有可能的取值中出現(xiàn)了!(k初始化為0)
            改正后的代碼:
             1 #include<cstdio>
             2 #include<cstring>
             3 const int maxn = 51000;
             4 struct hashn{
             5   int k;
             6   int c;
             7 };
             8 hashn hash[100000] ;
             9 int hash_fun(int num){
            10   int t = num % maxn;
            11   if(t < 0)t += maxn;
            12   while(hash[t].k != -1){
            13     if(hash[t].k == num)return t;
            14     t = (t+1)%maxn;
            15   }
            16   hash[t].k = num;
            17   return t;
            18 }
            19 int main(){
            20   int a,b,c,d;
            21   int sq[101={0};
            22   for(int i = 1; i <= 100; i++)
            23     sq[i] = i*i;
            24   while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF){
            25     if((a<0 && b<0 && c<0 && d<0|| 
            26        (a>0 && b>0 && c>0 && d>0) ){
            27       printf("0\n");
            28       continue;
            29     }
            30     for(int i = 0; i <= 100000;i++){
            31       hash[i].c = 0;
            32       hash[i].k = -1;
            33     }
            34     for(int i = 1; i <= 100; i++)
            35       for(int j = 1; j <= 100; j++)
            36     hash[hash_fun(-sq[i]*- sq[j]*b)].c++;
            37     int ans = 0;
            38     for(int i = 1; i <= 100; i++)
            39       for(int j = 1; j <= 100; j++){
            40     int temp = hash_fun(sq[i]*+ sq[j]*d);
            41     ans += hash[temp].c;
            42       }
            43     printf("%d\n",ans*16);
            44   }
            45 }

            posted on 2011-11-16 18:10 is-programmer 閱讀(158) 評論(0)  編輯 收藏 引用 所屬分類: 算法與數(shù)據(jù)結(jié)構(gòu)


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            導(dǎo)航

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統(tǒng)計

            常用鏈接

            留言簿

            隨筆檔案

            文章分類

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            天天做夜夜做久久做狠狠| 久久99国产综合精品| 国产成人久久777777| 久久综合色之久久综合| 色综合久久最新中文字幕| 狠狠色婷婷久久综合频道日韩| Xx性欧美肥妇精品久久久久久| 2022年国产精品久久久久| 久久狠狠爱亚洲综合影院| 色99久久久久高潮综合影院| 国产精品99久久不卡| 国产精品99久久久久久宅男| 99久久国产综合精品成人影院| 狠狠色丁香婷婷久久综合不卡| 91视频国产91久久久| 狠狠久久亚洲欧美专区| 中文字幕一区二区三区久久网站| 亚洲国产成人久久综合一| 免费精品99久久国产综合精品| 91亚洲国产成人久久精品网址| 欧美日韩精品久久久久| 中文字幕精品久久| 久久青青草原精品国产| 久久66热人妻偷产精品9| 国产成人99久久亚洲综合精品| 久久精品中文字幕第23页| 亚洲综合久久夜AV | 久久久久人妻一区精品色| 国产精品99久久久久久人| 久久精品99无色码中文字幕| 久久精品中文字幕一区| 香蕉久久av一区二区三区| 国产韩国精品一区二区三区久久| 久久久精品久久久久久| 亚洲色大成网站www久久九| 久久99热狠狠色精品一区| 噜噜噜色噜噜噜久久| 精品午夜久久福利大片| 伊人久久成人成综合网222| 久久不见久久见免费视频7| 久久九九免费高清视频|