#include <stdio.h> #include <stdlib.h> int main() { int n; scanf("%d", &n); // 读取关系R int r[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { r[i][j] = 0; } } char c; int a, b; scanf(" %c", &c); // 读取左括号 while (scanf("<%d,%d>", &a, &b) == 2) { r[a-1][b-1] = 1; scanf(" %c", &c); if (c == '}') break; } // 计算传递闭包 int r_trans[n][n]; for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (r[i][k] > 0 && r[k][j] > 0) { r_trans[i][j] = 1; } } } } // 输出传递闭包 printf("{"); int cnt = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (r[i][j] > 0 || r_trans[i][j] > 0) { if (cnt > 0) { printf(","); } printf("<%d,%d>", i+1, j+1); cnt++; } } } printf("}"); return 0; } 修改代码使其读入3 {<1,1>,<2,1>,<3,3>,<2,3>,<3,2>}时输出{<1,1>,<2,1>,<2,2>,<2,3>,<3,1>,<3,2>,<3,3>}
时间: 2024-03-25 17:41:19 浏览: 29
修改后的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
// 读取关系R
int r[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
r[i][j] = 0;
}
}
char c;
int a, b;
scanf(" %c", &c); // 读取左括号
while (scanf("<%d,%d>", &a, &b) == 2) {
r[a-1][b-1] = 1;
scanf(" %c", &c);
if (c == '}') break;
}
// 计算传递闭包
int r_trans[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
r_trans[i][j] = r[i][j];
}
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (r_trans[i][k] > 0 && r_trans[k][j] > 0) {
r_trans[i][j] = 1;
}
}
}
}
// 输出传递闭包
printf("{");
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (r_trans[i][j] > 0) {
if (cnt > 0) {
printf(",");
}
printf("<%d,%d>", i+1, j+1);
cnt++;
}
}
}
printf("}");
return 0;
}
```
主要的修改在于计算传递闭包的部分,需要先将 `r_trans` 初始化为 `r`,然后再进行传递闭包计算。另外,在输出传递闭包时,只需要输出 `r_trans` 中值为 1 的位置即可。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)