zoukankan      html  css  js  c++  java
  • 计算机考研机试指南(三)——hash

    编程日记 cha2-3 Hash的应用

    统计同成绩学生人数

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string.h>
     4 #include <stdio.h>
     5 using namespace std;
     6 /*
     7     题目:统计同成绩学生人数
     8     用时:
     9     思路:hash数组统计
    10 
    11 */
    12 
    13 
    14 int main()
    15 {
    16     int n;
    17     int hs[101]={0};
    18     int grade ;
    19     int x;
    20     while (scanf("%d",&n)!=EOF && n!=0)
    21     {
    22         for (int i=0;i<n;i++)
    23         {
    24             cin >> grade ;
    25             hs[grade]++;
    26         }
    27         cin >> x;
    28         cout<<hs[x]<<endl;
    29     }
    30 
    31     return 0;
    32 }

    Sort(牛客没有)

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string.h>
     4 #include <stdio.h>
     5 using namespace std;
     6 /*
     7     题目:Sort
     8     用时:* tomato
     9     思路:hash数组统计
    10     1.排序:N属于[0,1000000],O(nlogn)的快排也会使时间复杂度达到千万级以上
    11     2.输入的数字属于[-500000,500000]的区间,创建数组a,把数字对应序号存进去,
    12     时间复杂度则为遍历一边整个100万级的数组而已
    13 
    14 */
    15 int a[1000001]={0};
    16 
    17 int main()
    18 {
    19     int n,m;
    20     int num;
    21     while (scanf("%d %d",&n,&m)!=EOF)
    22     {
    23         for (int i=0;i<n;i++)
    24         {
    25             cin>>num;
    26             a[num+500000] ++;
    27         }
    28         int i = 1000000;
    29         while (i-- && m>0)
    30         {
    31             if (a[i])
    32             {
    33                 m--;
    34                 cout<<i-500000<<' ';
    35             }
    36         }
    37     }
    38     return 0;
    39 }

    谁是你的潜在朋友

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string.h>
     4 #include <stdio.h>
     5 using namespace std;
     6 /*
     7     题目:谁是你的潜在朋友
     8     用时:* tomato
     9     思路:hash数组统计 书的编号是1-200
    10     但是需要根据学生的编号和书号决定他的潜在朋友个数,
    11     通过一个数组stu解决此问题,
    12     因为N是动态输入,我们可以用malloc动态分配
    13 
    14 
    15 */
    16 
    17 
    18 int main()
    19 {
    20     int n,m,num;
    21     int book[201]={0};
    22     int *stu ;
    23     while (scanf("%d %d",&n,&m)!=EOF)
    24     {
    25         stu = (int *)malloc(n*sizeof(int));
    26         for (int i=0;i<n;i++)
    27         {
    28             cin >> num ;
    29             book[num] ++ ;
    30             stu[i]=num;
    31         }
    32         for (int i=0;i<n;i++)
    33         {
    34             if (book[stu[i]]>1)
    35                 cout <<book[stu[i]]-1<<endl;
    36             else
    37                 cout<<"BeiJu"<<endl;
    38         }
    39 
    40 
    41     }
    42 
    43     return 0;
    44 }

    剩下的树

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string.h>
     4 #include <stdio.h>
     5 using namespace std;
     6 /*
     7     题目:剩下的树
     8     用时:* tomato
     9     思路:hash数组统计
    10 
    11 
    12 */
    13 
    14 int road[10001]={0};
    15 int main()
    16 {
    17 
    18     int l;// 1<= l <=10000
    19     int m ;//1 <= m <= 100
    20     int i,j;
    21     int left,right;
    22     int c=0;
    23     for (i=0;i<10001;i++)
    24         road[i]=1;
    25     while (scanf("%d %d",&l,&m)!=EOF)
    26     {
    27         for (i=0;i<m;i++)
    28         {
    29             cin >>left >> right;
    30             for (j=left;j<right+1;j++)
    31             {
    32                 if (road[j]>0)
    33                     road[j]--; // 有树才需要砍树
    34             }
    35 
    36         }
    37         for (i=0;i<=l;i++)
    38             if (road[i]>0)
    39                 c++;
    40         cout << c <<endl;
    41 
    42     }
    43     return 0;
    44 }
  • 相关阅读:
    hdu 1290 献给杭电五十周年校庆的礼物 (DP)
    hdu 3123 GCC (数学)
    hdu 1207 汉诺塔II (DP)
    hdu 1267 下沙的沙子有几粒? (DP)
    hdu 1249 三角形 (DP)
    hdu 2132 An easy problem (递推)
    hdu 2139 Calculate the formula (递推)
    hdu 1284 钱币兑换问题 (DP)
    hdu 4151 The Special Number (DP)
    hdu 1143 Tri Tiling (DP)
  • 原文地址:https://www.cnblogs.com/twomeng/p/9509438.html
Copyright © 2011-2022 走看看