码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-08-26 20:14:59      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:

水题 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;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:

原文地址:http://blog.csdn.net/lsgqjh/article/details/48007805

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!