Operation of sorted set对集合的操作
来源:互联网 发布:淘宝店开不下去了 编辑:程序博客网 时间:2024/06/05 11:01
Description:
There are two groups of some numbers(0~50). each group should be input in a set(remove the duplicate numbers).
And then output the two set, intersection and union of the two set.Input format:
first line: a couple of numbers and end of -1.
second line: a couple of numbers and end of -1.
Output format:
first line:Output the first set.
second line: Output the second set.
third line: Output the intersection of the two set.
fourth line:Output the union of the two set.All the numbers in the set should be output by ascending oder.
Each line behind the numbers, there is a space ’ ‘.For example:
[Input]
1 2 3 6 5 4 1 2 3 -1
3 2 3 2 1 0 -1
[Output]
1 2 3 4 5 6
0 1 2 3
1 2 3
0 1 2 3 4 5 6
其实就是对集合排序,然后求两个集合的交集和并集●ω●
#include<stdio.h>int set(int *a) { int i, j, temp, count = 1; scanf("%d", &temp); a[0] = temp; while (1) { scanf("%d", &temp); if (temp == -1) break; for (i = 0; i < count; i++) { if (a[i] == temp) break; //判断该数是否已存在 } if (i == count) { a[count] = temp; count++; } } //完成集合的构造 for (i = 0; i < count; i++) { for (j = 0; j < count - i - 1; j++) { if (a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } //用冒泡排序对集合元素排序 return count;}int main() { int a[50], b[50], count1, count2; count1 = set(a); count2 = set(b); int i, j, k = 0; for (i = 0; i < count1; i++) { printf("%d ", a[i]); } //输出集合A printf("\n"); for (i = 0; i < count2; i++) { printf("%d ", b[i]); } //输出集合B printf("\n"); for (i = 0; i < count1; i++) { for (j = k; j < count2; j++) { if (a[i] == b[j]) { printf("%d ", b[j]); //输出相同元素 k = j + 1; } } } //输出A∩B printf("\n"); i = 0; j = 0; //由于set是排好序的,所以可以将两个set的元素逐个进行比较 while (1) { if (a[i] < b[j]) { //如果,A的第一个比B的第一个小,输出A的第一个数,A的下标加1 printf("%d ", a[i]); i++; } else if (a[i] > b[j]) { //如果,A的第一个比B的第一个大,输出B的第一个数,B的下标加1 printf("%d ", b[j]); j++; } else { //如果,相等,输出任何一个,AB下标加1 printf("%d ", a[i]); i++; j++; } if (i >= count1 || j >= count2) break; //继续比较,直到一个下标等于其长度为止,输出另外一个set的剩余元素 } if (i >= count1 && j < count2) { for (j; j < count2; j++) printf("%d ", b[j]); //若B还有未输出的元素继续输出 } else if (i < count1 && j >= count2) { for (i; i < count1; i++) printf("%d ", a[i]); //若A还有未输出的元素继续输出 } printf("\n"); return 0;}
- Operation of sorted set对集合的操作
- Operation of sorted set
- NoSQL之Redis对set(集合)数据类型的操作之一
- Redis的有序集合(sorted set)
- 对 set集合 的遍历
- Redis初探06——Redis的有序集合sorted set类型及操作
- Hibernate对集合属性的操作之Set集合篇 6
- Delphi的set集合操作
- Set集合的基本操作
- python:集合set的操作
- Redis的Sorted Set的操作
- scala对Set的操作
- Redis 有序集合(sorted set)
- Redis 有序集合(sorted set)
- Redis 有序集合(sorted set)
- Redis 有序集合(sorted set)
- Redis 有序集合(sorted set)
- Redis 有序集合(sorted set)
- 《Unix环境高级编程》之popen函数使用
- 相似性的比较的总结
- iOS开发之UITextView,设置textView的行间距及placeholder
- 关于android stuido 与 Eclipse 的android项目结构对比
- HTTP的请求头标签 If-Modified-Since
- Operation of sorted set对集合的操作
- Android中Service 详解
- Linux下/proc目录简介
- spring 注解@Resource 和 @Autowired的区别
- Python的 startswith 和endswith
- Oracle安装详解(Linux)
- Java设计模式中类的关系
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器