SDUT 3811 离散题目17
来源:互联网 发布:js json map 遍历 编辑:程序博客网 时间:2024/06/05 15:10
Problem Description
给出集合X和X上的关系R,求关系R在X上的对称闭包s(R)。
例如:
X={1,2,3,4,5} , R={<1,1>,<2,1>,<3,3>,<2,3>,<3,2>,<4,5>}
s(R)= {<1,1>,<1,2>,<2,1>,<2,3>,<3,2>,<3,3>,<4,5>,<5,4>}
Input
多组输入,每组输入第一行为集合X的元素;第二行为一个整数n ( n > 0 ),代表X上的关系R中序偶的个数;接下来n行用来描述X上的关系R,每行两个数字,表示关系R中的一个序偶。细节参考示例输入。
非空集合X的元素个数不大于500,每个元素的绝对值不大于2^32 - 1。
Output
每组输入对应一行输出,为X上关系R的对称闭包s(R),s(R)中的序偶根据序偶中的第一个值升序排列,如果第一个值相同则根据第二个值升序排列;具体输出格式见样例(注意:样例每个逗号后有一个空格)。
Example Input
1 2 3 4 5
6
1 1
2 1
3 3
2 3
3 2
4 5
Example Output
[(1, 1), (1, 2), (2, 1), (2, 3), (3, 2), (3, 2), (3, 3), (4, 5), (5, 4)]
代码: 求对称闭包,然而这个输出样例是有问题的
正确的应该是:[(1, 1), (1, 2), (2, 1), (2, 3), (2, 3), (3, 2), (3, 2), (3, 3), (4, 5), (5, 4)]
#include<bits/stdc++.h>using namespace std;struct node{ int x, y; bool operator < (const node &b) const { if(x == b.x) return y < b.y; else return x < b.x; }};vector<node> a;int main(){ string s; int n, i, u, v; while(getline(cin, s)) { scanf("%d", &n); for(i = 0; i < n; i++) { scanf("%d %d", &u, &v); a.push_back((node){u, v});//存起来 } //getchar(); sort(a.begin(), a.end());//排序 n = a.size(); for(i = 0; i < n; i++) { if(a[i].x != a[i].y)//如果不相等,交换一下存入 { a.push_back((node){a[i].y, a[i].x}); } } sort(a.begin(), a.end());//排序,然后输出 printf("[(%d, %d)", a[0].x, a[0].y); for(i = 1; i < a.size(); i++) { printf(", (%d, %d)", a[i].x, a[i].y); } printf("]\n"); a.clear(); getline(cin, s);//坑点,数据后面有多余的空格 } return 0;}
阅读全文
0 0
- SDUT 3811 离散题目17
- SDUT-离散题目17
- sdut离散题目11
- sdut离散题目13
- sdut离散题目14
- SDUT-离散题目13
- SDUT-离散题目7
- SDUT离散题目4
- sdut-离散题目3
- sdut- 离散题目4
- sdut-离散题目5
- sdut-离散题目6
- sdut-离散题目7
- sdut-离散题目8
- sdut-离散题目9
- sdut-离散题目10
- sdut-离散题目11
- sdut-离散题目12
- Sql Server 中常用日期转换Convert(Datetime)
- Android Studio 原生常用快捷键
- websocket和ajax轮询区别
- mysql5.5 mysql5.6 mysql5.7官方手册
- C++之获取网卡物理地址(MAC)
- SDUT 3811 离散题目17
- php中time()与$_SERVER[REQUEST_TIME]用法区别
- c#委托相关
- StrongReference,SoftReference, WeakReference的使用总结
- Python+Apache+CGI完全配置
- 打印一个等腰三角形
- 关于CentOS 6.6忘记密码和输入正确密码依旧无法登录的解决办法
- Android studio 3.0预览版中kotlin简单使用及配置
- 瞎扯淡 量产型炮灰工程师