哈理工oj Hrbustacm 1287 数字去重和排序II(STL 或着 hash 拉链法)

水题 STL map也能过,但是为了练习拉链hash

/*=============================================================================
#
#      Author: liangshu - cbam 
#
#      QQ : 756029571 
#
#      School : 哈尔滨理工大学 
#
#      Last modified: 2015-08-26 18:36
#
#     Filename: C.cpp
#
#     Description: 
#        The people who are crazy enough to think they can change the world, are the ones who do ! 
=============================================================================*/

#include<iostream>    
#include<sstream>    
#include<algorithm>    
#include<cstdio>    
#include<string.h>    
#include<cctype>    
#include<string>    
#include<cmath>    
#include<vector>    
#include<stack>    
#include<queue>    
#include<map>    
#include<set>    
using namespace std;
const int M = 1007;
struct Node{
    int d;
    Node * next ;
};
Node * pnd[M + 1];
Node nd[M +1];
int n_cnt;
int a[1000 + 18];
int a_cnt;

int main(){
    int n, d, p;
    while(scanf("%d",&n) != EOF){
         memset(pnd, 0, sizeof(pnd));
 n_cnt = 0;
a_cnt = 0;
for(int i = 0; i < n; i++){
    scanf("%d",&d);
    p = d % M;
    bool found = false ;
    Node *pt = pnd[p];
    while(pt){
        if(pt -> d == d){
            found = true;
            break;
        }
        pt = pt -> next;
    }
    if(!found){
        nd[n_cnt].d = d;
        nd[n_cnt].next = pnd[p];
        pnd[p] = &nd[n_cnt];
        n_cnt ++;
        a[a_cnt++] = d;
    }
}
sort(a, a + a_cnt);
printf("%d\n%d", a_cnt, a[0]);
for(int
        i = 1; i < a_cnt; ++i){
    printf(" %d",a[i]);
}
printf("\n");
    }
return 0;
}
AC2:
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
int main(){
    int n;
    set<long long > cnt;
    while(scanf("%d",&n) != EOF){
        for(int i = 1; i <= n; i++){
          int x;
          scanf("%lld",&x);
            cnt.insert(x);
        }
        set<long long > ::iterator it = cnt.begin();
    cout<<cnt.size()<<endl;
    
        printf("%lld",*it);
        it++;
        for(;  it != cnt.end(); it++){
            cout<<" "<<*it;
        }
           cout<<endl; 
           cnt.clear();
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值