http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2719&cid=1203

题意 :意思就是哈希来的,具体大意就是说有两个哈希表,然后有这样一组数据,让你把这组数据存到这两个哈希表里,然后不能重复,先让数据往表1里存,就是对表1的长度进行取余,如果余数这个位置没有数就存上,如果有的话,就存上这个数,让原来的数再去表2里存,也是按照这个方式。就是来回踢。。。我觉得。。。。

思路:两个哈希表,一个循环找即可。。。当时做的时候把自己绕进去了。。。。

#include <stdio.h>
#include <string.h>
#include <map>
#include <iostream>

using namespace std ;
int ch[1100];
int sh[1100];
int main()
{
    int n1,n2,k ;
    int t = 1 ;
    while(scanf("%d%d%d",&n1,&n2,&k)!=EOF)
    {

        memset(ch,-1,sizeof(ch));
        memset(sh,-1,sizeof(sh)) ;
        if(n1 == 0&&n2==0&&k==0) break;
        int x ;
        for(int i = 1 ; i <= k ; i++)
        {
            scanf("%d",&x);
            while(1)
            {
                int s=x%n1;
                if(ch[s] == -1)
                {
                    ch[s]=x;
                    break;
                }
                else
                {
                    int temp=ch[s];
                    ch[s]=x;
                    int tt=temp%n2;
                    if(sh[tt]==-1)
                    {
                        sh[tt]=temp;
                        break;
                    }
                    else
                    {
                        x=sh[tt];
                        sh[tt]=temp;
                    }
                }
            }
        }
        printf("Case %d:\n",t);
        t++;
        int flag = 0 ;
        for(int i = 0 ; i < n1 ; i++)
        {
            if(ch[i] != -1)
            {
                flag = 1 ;
                break ;
            }
        }
        if(flag)
        {
            printf("Table 1\n");
            for(int i = 0 ; i < n1 ; i++)
            {
                if(ch[i] != -1)
                {
                    printf("%d:%d\n",i,ch[i]) ;
                }
            }
        }
        flag = 0 ;
        for(int i = 0 ; i < n2 ; i++)
        {
            if(sh[i] != -1)
            {
                flag = 1 ;
                break ;
            }
        }
        if(flag)
        {
            printf("Table 2\n");
            for(int i = 0 ; i < n2 ; i++)
            {
                if(sh[i] != -1)
                {
                    printf("%d:%d\n",i,sh[i]) ;
                }
            }
        }
    }
    return 0 ;
}
View Code

相关文章:

  • 2022-01-01
  • 2022-01-16
  • 2021-08-25
  • 2021-12-03
  • 2022-01-18
  • 2022-12-23
  • 2021-10-02
  • 2022-02-12
猜你喜欢
  • 2021-07-18
  • 2021-08-05
  • 2022-12-23
  • 2021-07-01
  • 2021-12-19
  • 2022-12-23
相关资源
相似解决方案