水题 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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-03 13:46:31