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用计算机随机生成了N个0到1000000000(包含0和1000000000)之间的随机整数(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 15Sample Output8
15 20 32 40 67 89 300 400Author拂晓
#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;
}
请你完成“去重”与“排序”的工作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 15Sample Output8
15 20 32 40 67 89 300 400Author拂晓
#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
- hrbust1287
- 几道简单算法
- c语言的困惑
- mysql创建用户操作
- 搜狐云景客户端工具评测之WordPress的搭建
- 物流管理在电商中的重要意义
- hrbust1287
- 第2次实验——算法基本功与综合思考
- 京东周辉:借力O2O模式 线上购物可15分钟送达
- nyoj 37 回文字符串 【DP】
- git 查看远程分支、本地分支、删除本地分支
- 码农札记--2014年5月
- php普通用户创建数据库失败
- 《二》CC2540和CC2541上实现ANCS(Apple Notification Center Service)
- [LeetCode17]Letter Combinations of a Phone Number