离散题目4
来源:互联网 发布:企业数据架构设计 编辑:程序博客网 时间:2024/06/14 14:46
离散题目4
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
题目给出两个非空整数集,请写出程序求两个集合的交集。
Input
多组输入,每组输入包括两行,第一行为集合A的元素,第二行为集合B的元素。具体参考示例输入。 每个集合元素个数不大于3000,每个元素的绝对值不大于2^32 - 1。
Output
每组输入对应一行输出,为A、B的交集,如果交集为空输出”NULL”,否则交集的元素按递增顺序输出,每个元素之间用空格分割。
Example Input
1 2 3 4 5
1 5 3 6 7
1 2 4 5 3
6 7 8 9 10
Example Output
1 3 5
NULL
Think:首先将输入的字符串分离出来,分别放在两个数组中,之后将两个数组进行排序,之后判断是否有交集
#include <stdio.h>#include <string.h>void sort(int aa[],int left,int right);int main(){ int a[500000],b[500000],c[500000]; double q; int xx,yy; int n; double ww; char x[500000],y[500000]; while(gets(x)){ gets(y); q=0.0; xx=0; ww=1.0; n=strlen(x); for (int i=0;i<n;i++){ if (x[i]!=' '){ if (x[i]=='-'){ ww=-1.0; } else{ q=q*10+(x[i]-'0'); } } else if (x[i]==' '){ a[xx++]=q*ww; q=0; ww=1.0; } } a[xx++]=q*ww; q=0.0; yy=0; n=strlen(y); ww=1.0; for (int i=0;i<=n;i++){ if (y[i]!=' '){ if (y[i]=='-'){ ww=-1.0; } else{ q=q*10+(y[i]-'0'); } } else if (y[i]==' '){ b[yy++]=q*ww; q=0; ww=1.0; } } b[yy++]=q*ww; int flag=0; int qq=0; for (int i=0;i<xx;i++){ for (int j=0;j<yy;j++){ if (a[i]==b[j]){ c[qq++]=a[i]; flag=1; } } } if (flag){ sort(c,0,qq-1); for (int i=0;i<qq;i++){ printf("%d%c",c[i],i!=qq-1?' ':'\n'); } } else{ printf("NULL\n"); } } return 0; }void sort(int s[],int left,int right){ int x=s[left],i=left,j=right; if (left>=right){ return; } while(i<j){ while(i<j&&s[j]>=x)j--; s[i]=s[j]; while(i<j&&s[i]<=x)i++; s[j]=s[i]; } s[i]=x; sort(s,left,i-1); sort(s,i+1,right);}
阅读全文
0 0
- 离散题目4
- 离散题目4
- 离散题目4
- 离散题目4
- 离散题目4
- SDUT离散题目4
- sdut- 离散题目4
- 离散题目4
- sdut oj离散题目4
- 离散题目4 求交集
- 离散题目3
- 离散题目7
- sdut离散题目11
- sdut离散题目13
- sdut离散题目14
- 离散题目12
- 离散题目15
- 3799-->离散题目5
- HDU 1698 JUST A HOOK
- 没想到发表的第一个帖子是吐槽贴
- ajax异步请求 各项参数详解
- Ubuntu16.04中Android编译环境适配
- NYOJ-284坦克大战(优先队列)
- 离散题目4
- 基础技术篇 2 —— RFID技术(二)
- jsp页面input框中值或事件的操作方式
- Linux添加环境变量与GCC编译器添加INCLUDE与LIB环境变量
- Tomcat 自带的 JDBC 连接池官方文档中文版
- UVa834 Continued Fractions【连分数】
- jsp页面radio框中值操作方式
- 破解MyEclipse2015 stable3.0(亲测可用)
- pandas学习笔记