hrbust1287

来源:互联网 发布:国外类似于知乎的网站 编辑:程序博客网 时间:2024/06/05 11:52

数字去重和排序IITime Limit: 4000 MSMemory Limit: 65536 KTotal Submit: 555(131 users)Total Accepted: 234(116 users)Rating: Special Judge: NoDescription用计算机随机生成了N01000000000(包含01000000000)之间的随机整数(N≤5000000),对于其中重复的数字,只保留一个,把其余相同的数去掉。然后再把这些数从小到大排序。
请你完成去重排序的工作
Input输入有2行,第1行为1个正整数,表示所生成的随机数的个数:
N
2行有N个用空格隔开的正整数,为所产生的随机数。
Output输出也是2行,第1行为1个正整数M,表示不相同的随机数的个数。第2行为M个用空格隔开的正整数,为从小到大排好序的不相同的随机数。Sample Input10
20 40 32 67 40 20 89 300 400 15
Sample Output8
15 20 32 40 67 89 300 400
Author拂晓
#include <stdio.h>
#include <string.h>
#include <algorithm>


#define  MP 1007


using namespace std;


struct node
{
    int d;
    struct node *next;


}p[MP + 1];
node *pnt[MP + 1];
int a[1000 + 30];
int ans,n_cnt,c_cnt;


int main()
{
   int n=0,num=0;
   while( scanf("%d",&n)!=EOF,n)
   {
       memset(pnt,0,sizeof(pnt));
         n_cnt = 0;
         c_cnt = 0;
       for( int i = 0; i<n; i++)
       {


           scanf("%d",&num);
           ans = num%MP;
           bool flag = false;
           node *pt = pnt[ans];
           while(pt)
           {
               if(pt->d == num)
               {
                   flag = true;
                   break;
               }
              pt = pt->next;
           }


         if(!flag)
         {
             p[n_cnt].d = num;
             p[n_cnt].next = pnt[ans];
             pnt[ans] = &p[n_cnt];
               n_cnt++;
            a[c_cnt++] = num;
         }
       }


        sort(a,a+c_cnt);
        printf("%d\n%d",c_cnt,a[0]);
        for(int i=1;i<c_cnt;i++)
        printf(" %d",a[i]);
        printf("\n");
        n = 0;//如果没有n=0下次n会保留上次的值,while会一直循环,TAT错了那么多次竟然是这里。。。
   }
   return 0;
}


0 0
原创粉丝点击