[C] uva 350 →→→→→进入此内容的聊天室

来自 , 2019-11-06, 写在 C, 查看 179 次.
URL http://www.code666.cn/view/dcf6070a
  1. #include <cstdio>
  2. using namespace std;
  3. const int maxn = 65535;
  4. struct hash_tag
  5. {
  6.     int key, num, next;
  7. }hash[maxn<<1];
  8. int flag[maxn], idx, top;
  9. void insert(int n, int num)
  10. {
  11.     int k = (n&65535);
  12.     if (flag[k]!=idx)
  13.     {
  14.         flag[k] = idx;
  15.         hash[k].key = n;
  16.         hash[k].num = num;
  17.         hash[k].next = -1;
  18.         return;
  19.     }
  20.     while (hash[k].next != -1)
  21.     {
  22.         if (hash[k].key == n) return;
  23.         k = hash[k].next;
  24.     }
  25.     hash[k].next = ++top;
  26.     hash[top].key = n;
  27.     hash[top].num = num;
  28.     hash[top].next = -1;
  29. }
  30.  
  31. int find(int n)
  32. {
  33.     int k = (n&65535);
  34.     if (flag[k]!=idx)
  35.     {
  36.         return -1;
  37.     }
  38.     while (k!=-1)
  39.     {
  40.         if (hash[k].key == n) return hash[k].num;
  41.         k = hash[k].next;
  42.     }
  43.     return -1;
  44. }
  45. //map, set, hash_set, hash_map
  46. int main()
  47. {
  48.     int Z = 0, I = 0, M = 0, L = 0;
  49.     int t = 0;
  50.     while (scanf("%d%d%d%d", &Z, &I, &M, &L)==4)
  51.     {
  52.         if (Z == 0&&I == 0&&M== 0&&L == 0) break;
  53.         int count = 0, temp;
  54.         top = maxn;idx++;
  55.         t++;
  56.         L = (Z*L+I)%M;
  57.         while ((temp=find(L)) == -1)
  58.         {
  59.             insert(L, ++count);
  60.             L = (Z*L+I)%M;
  61.         }
  62.         printf("Case %d: %d\n", t, count-temp+1);
  63.     }
  64.     return 0;
  65. }
  66.  

回复 "uva 350"

这儿你可以回复上面这条便签

captcha