Hash的使用

计算两个日期的差值

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define ISYEAR(x) (x%100!=0 && x%4==0)|| x%400?0:1
int dayofmonth[13][2]{
    0,0,
    31,31,
    28,29,
    31,31,
    30,30,
    31,31,
    30,30,
    31,31,
    31,31,
    30,30,
    31,31,
    30,30,
    31,31,
};
struct date{
    int day;
    int month;
    int year;
    void nextday(){
        day++;
        if(day>dayofmonth[month][ISYEAR(year)]){
            day=1;
            if(month>12) {
                month=1;
                year++;
            }
        }
    }
};
int buf[5001][13][32];
int main()
{
    date tmp;
    int cnt=0;
    tmp.year=0;
    tmp.month=1;
    tmp.day=1;
    while(tmp.year!=5001){
        buf[tmp.year][tmp.month][tmp.day]=cnt;
        tmp.nextday();
        cnt++;
    }
    int d1,m1,y1,d2,y2,m2;
    while(scanf("%4d%2d%2d",&d1,&m1,&y1)!=EOF){
        scanf("%4d%2d%2d",&d2,&m2,&y2);
        printf("%d",abs(buf[d1][m1][y1]-buf[d2][m2][y2]));
    }
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF&&n!=0){
        int Hash[101]={0};
        for(int i=0;i<n;i++){
                int x;
            scanf("%d",&x);
            Hash[x]++;
        }
        int x;
        scanf("%d",&x);
        printf("%d\n",Hash[x]);
    }
    return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define OFFSET 500000
int Hash[1000001];

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(Hash,0,sizeof(Hash));
        for(int i=0;i<n;i++){
            int x;
            scanf("%d",&x);
            Hash[x+OFFSET]=1;
        }
        for(int i=50000;i<=-500000;i--){
            if(Hash[i+OFFSET]){
                printf("%d",i);
                m--;
                if(m) printf(" ");
                else {
                        printf("\n");
                        break;
                }
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值