计算机考研机试指南(三)

编程日记 cha2-3 Hash的应用

统计同成绩学生人数
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
using namespace std;
/*
    题目:统计同成绩学生人数
    用时:
    思路:hash数组统计

*/


int main()
{
    int n;
    int hs[101]={0};
    int grade ;
    int x;
    while (scanf("%d",&n)!=EOF && n!=0)
    {
        for (int i=0;i<n;i++)
        {
            cin >> grade ;
            hs[grade]++;
        }
        cin >> x;
        cout<<hs[x]<<endl;
    }

    return 0;
}

Sort(牛客没有)
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
using namespace std;
/*
    题目:Sort
    用时:* tomato
    思路:hash数组统计
    1.排序:N属于[0,1000000],O(nlogn)的快排也会使时间复杂度达到千万级以上
    2.输入的数字属于[-500000,500000]的区间,创建数组a,把数字对应序号存进去,
    时间复杂度则为遍历一边整个100万级的数组而已

*/
int a[1000001]={0};

int main()
{
    int n,m;
    int num;
    while (scanf("%d %d",&n,&m)!=EOF)
    {
        for (int i=0;i<n;i++)
        {
            cin>>num;
            a[num+500000] ++;
        }
        int i = 1000000;
        while (i-- && m>0)
        {
            if (a[i])
            {
                m--;
                cout<<i-500000<<' ';
            }
        }
    }
    return 0;
}

谁是你的潜在朋友
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
using namespace std;
/*
    题目:谁是你的潜在朋友
    用时:* tomato
    思路:hash数组统计 书的编号是1-200
    但是需要根据学生的编号和书号决定他的潜在朋友个数,
    通过一个数组stu解决此问题,
    因为N是动态输入,我们可以用malloc动态分配


*/


int main()
{
    int n,m,num;
    int book[201]={0};
    int *stu ;
    while (scanf("%d %d",&n,&m)!=EOF)
    {
        stu = (int *)malloc(n*sizeof(int));
        for (int i=0;i<n;i++)
        {
            cin >> num ;
            book[num] ++ ;
            stu[i]=num;
        }
        for (int i=0;i<n;i++)
        {
            if (book[stu[i]]>1)
                cout <<book[stu[i]]-1<<endl;
            else
                cout<<"BeiJu"<<endl;
        }


    }

    return 0;
}

剩下的树
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
using namespace std;
/*
    题目:剩下的树
    用时:* tomato
    思路:hash数组统计


*/

int road[10001]={0};
int main()
{

    int l;// 1<= l <=10000
    int m ;//1 <= m <= 100
    int i,j;
    int left,right;
    int c=0;
    for (i=0;i<10001;i++)
        road[i]=1;
    while (scanf("%d %d",&l,&m)!=EOF)
    {
        for (i=0;i<m;i++)
        {
            cin >>left >> right;
            for (j=left;j<right+1;j++)
            {
                if (road[j]>0)
                    road[j]--; // 有树才需要砍树
            }

        }
        for (i=0;i<=l;i++)
            if (road[i]>0)
                c++;
        cout << c <<endl;

    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值