Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:谁能告诉我怎么样除去重复的In Reply To:谁能告诉我怎么样除去重复的 Posted by:6233843 at 2006-07-17 18:13:52 > 求!! 哈希表 #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct hash { char s[1000]; struct hash*next; }hash; typedef hash* hashptr; hashptr ha[99983],cur[99983]; int elfhash(char *key) { unsigned long h=0,g; while (*key) { h=(h<<4)+*key++; g=h&0xf0000000L; if (g)h^=g>>24; h&=~g; } return h%99983; } void ins(char *key) { int r=elfhash(key); hashptr tmp=(hashptr)malloc(sizeof(hash)); strcpy(tmp->s,key); tmp->next=NULL; if (!ha[r]) { cur[r]=ha[r]=tmp; return; } cur[r]->next=tmp; cur[r]=tmp; } int find(char*key) { hashptr tmp=ha[elfhash(key)]; while (tmp) { if (strcmp(tmp->s,key)==0)return 1; tmp=tmp->next; } return 0; } int main() { int m,n,i,j,k,sum,a[200],s[200],p,f,pos; char str[1000],t[1000],b[1000][1000]; while (scanf("%d%d",&m,&n)!=EOF,n) { pos=f=0; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(ha,0,sizeof(ha)); memset(cur,0,sizeof(cur)); printf("Sums of %d:\n",m); for (i=0;i<n;i++)scanf("%d",a+i); for (i=0;i<n-1;i++) { for (j=0;j<n-1;j++) { if (a[j]<a[j+1]) { p=a[j];a[j]=a[j+1];a[j+1]=p; } } } for (i=0;i<(1<<n);i++) { p=sum=0; memset(s,0,sizeof(s)); memset(t,0,sizeof(t)); for (k=0;k<n;k++) { if (i&(1<<k)) { sum+=a[k]; s[p++]=a[k]; sprintf(str,"%d",a[k]); strcat(t,str); strcat(t,"+"); } } t[strlen(t)-1]=0; if (sum==m&&!find(t)) { f=1; ins(t); strcpy(b[pos++],t); } } if (!f)puts("NONE"); else { qsort(b,pos,sizeof(b[0]),(int(*)(const void*,const void*))strcmp); for (i=pos-1;i>=0;i--) { puts(b[i]); } } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator